tinythread

tiny threading library for linux
git clone git://git.2f30.org/tinythread.git
Log | Files | Refs | README | LICENSE

commit d5cc436d0ed8ad6e9e5b4abfff6482c094868a28
parent e4ba02507b3f9246d57988ebc779dad495f93fa1
Author: sin <sin@2f30.org>
Date:   Mon Sep 23 11:24:48 +0100

Fix makefile and add config.mk

Diffstat:
COPYING | 14--------------
LICENSE | 21+++++++++++++++++++++
Makefile | 42++++++++++++++++++++++++++++++------------
config.mk | 9+++++++++
test.c | 56--------------------------------------------------------
thread.c | 2--
thread.h | 23++++++++++++-----------
threadtest.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 128 insertions(+), 95 deletions(-)
diff --git a/COPYING b/COPYING @@ -1,14 +0,0 @@ - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - Version 2, December 2004 - - Copyright (C) 2004 Sam Hocevar - 14 rue de Plaisance, 75014 Paris, France - Everyone is permitted to copy and distribute verbatim or modified - copies of this license document, and changing it is allowed as long - as the name is changed. - - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. You just DO WHAT THE FUCK YOU WANT TO. - diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +Copyright (c) 2013 sin <sin@2f30.org> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -1,17 +1,35 @@ -bin = thread -ver = 0.1 -src = thread.c test.c +# See LICENSE file for copyright and license details. -CC = gcc -CFLAGS += -Wall -Wextra -Wformat-security -Wshadow \ - -Wpointer-arith -ggdb +include config.mk -all: $(bin) +SRC = thread.c +OBJ = $(SRC:.c=.o) -%: %.c - $(CC) -o $(bin) $(src) $(CFLAGS) +LIB = libthread.a +INC = utf.h -clean: - @rm -rf $(bin) +all: $(LIB) threadtest + +$(LIB): $(OBJ) + $(AR) -rcs $@ $(OBJ) + +threadtest: threadtest.o $(LIB) + $(CC) $(LDFLAGS) -o $@ threadtest.o $(LIB) + +.c.o: + $(CC) $(CFLAGS) -c $< -.PHONY: all clean +install: $(LIB) $(INC) $(MAN) + @echo @ install libthread to $(DESTDIR)$(PREFIX) + @mkdir -p $(DESTDIR)$(PREFIX)/lib + @cp $(LIB) $(DESTDIR)$(PREFIX)/lib/$(LIB) + @mkdir -p $(DESTDIR)$(PREFIX)/include + @cp $(INC) $(DESTDIR)$(PREFIX)/include/$(INC) + +uninstall: + @echo @ uninstall libthread from $(DESTDIR)$(PREFIX) + @rm -f $(DESTDIR)$(PREFIX)/lib/$(LIB) + @rm -f $(DESTDIR)$(PREFIX)/include/$(INC) + +clean: + rm -f $(LIB) threadtest threadtest.o $(OBJ) diff --git a/config.mk b/config.mk @@ -0,0 +1,9 @@ +VERSION = 0.1 + +# paths +PREFIX = /usr/local +CC = cc +# flags +CPPFLAGS = -D_GNU_SOURCE +CFLAGS = -fPIC -Wall -O3 ${CPPFLAGS} +LDFLAGS = -static diff --git a/test.c b/test.c @@ -1,56 +0,0 @@ -#include <unistd.h> - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "thread.h" - -enum { - THREADS_SPAWN_NR = 64 -}; - -/* This gets incremented once by each thread */ -static int counter; -/* Lock that protects `counter' */ -static spinlock_t lock; -/* Per thread arguments */ -static struct thread_args { - int id; -} thread_args[THREADS_SPAWN_NR]; - -static int -foo(void *arg) -{ - struct thread_args *args = arg; - - acquire(&lock); - printf("Entered thread with id %d\n", args->id); - counter++; - release(&lock); - - return 0; -} - -int -main(void) -{ - int i; - struct thread_ctx *tctx; - - setbuf(stdout, NULL); - - tctx = thread_init(NULL, NULL); - spinlock_init(&lock); - - for (i = 0; i < THREADS_SPAWN_NR; i++) { - thread_args[i].id = i; - thread_register(tctx, foo, &thread_args[i]); - } - - thread_wait_all_blocking(tctx); - - thread_exit(tctx); - - return EXIT_SUCCESS; -} diff --git a/thread.c b/thread.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE - #include <sys/wait.h> #include <sys/mman.h> #include <sched.h> diff --git a/thread.h b/thread.h @@ -12,17 +12,18 @@ struct thread_config { typedef int spinlock_t; struct thread_ctx; -extern struct thread_ctx *thread_init(const struct thread_config *tc, int *rval); -extern pid_t thread_register(struct thread_ctx *tctx, int (*fn)(void *), void *arg); -extern int thread_wait(struct thread_ctx *tctx, pid_t pid); -extern int thread_wait_blocking(struct thread_ctx *tctx, pid_t pid); -extern int thread_wait_all_blocking(struct thread_ctx *tctx); -extern void thread_exit(struct thread_ctx *tctx); -extern pid_t thread_id(void); -extern int thread_get_sched_param(struct thread_ctx *tctx, pid_t pid, int *policy, - struct sched_param *param); -extern int thread_set_sched_param(struct thread_ctx *tctx, pid_t pid, int policy, - const struct sched_param *param); + +struct thread_ctx *thread_init(const struct thread_config *tc, int *rval); +pid_t thread_register(struct thread_ctx *tctx, int (*fn)(void *), void *arg); +int thread_wait(struct thread_ctx *tctx, pid_t pid); +int thread_wait_blocking(struct thread_ctx *tctx, pid_t pid); +int thread_wait_all_blocking(struct thread_ctx *tctx); +void thread_exit(struct thread_ctx *tctx); +pid_t thread_id(void); +int thread_get_sched_param(struct thread_ctx *tctx, pid_t pid, int *policy, + struct sched_param *param); +int thread_set_sched_param(struct thread_ctx *tctx, pid_t pid, int policy, + const struct sched_param *param); static inline void spinlock_init(spinlock_t *spinlock) diff --git a/threadtest.c b/threadtest.c @@ -0,0 +1,56 @@ +#include <unistd.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "thread.h" + +enum { + THREADS_SPAWN_NR = 64 +}; + +/* This gets incremented once by each thread */ +static int counter; +/* Lock that protects `counter' */ +static spinlock_t lock; +/* Per thread arguments */ +static struct thread_args { + int id; +} thread_args[THREADS_SPAWN_NR]; + +static int +foo(void *arg) +{ + struct thread_args *args = arg; + + acquire(&lock); + printf("Entered thread with id %d\n", args->id); + counter++; + release(&lock); + + return 0; +} + +int +main(void) +{ + int i; + struct thread_ctx *tctx; + + setbuf(stdout, NULL); + + tctx = thread_init(NULL, NULL); + spinlock_init(&lock); + + for (i = 0; i < THREADS_SPAWN_NR; i++) { + thread_args[i].id = i; + thread_register(tctx, foo, &thread_args[i]); + } + + thread_wait_all_blocking(tctx); + + thread_exit(tctx); + + return EXIT_SUCCESS; +}