diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2009-03-31 18:46:23 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2009-03-31 18:46:23 +0200 |
commit | 80fabedc72b56664a4f6d77c253f9b3fe3939707 (patch) | |
tree | 35edd37dfb4bf34507826c78cf354bdc314d7fe2 | |
parent | d2b0696d8fa0ce47e0bc525581b6b9254637984b (diff) | |
download | wolfbones-80fabedc72b56664a4f6d77c253f9b3fe3939707.tar.gz wolfbones-80fabedc72b56664a4f6d77c253f9b3fe3939707.tar.bz2 |
started to add thread support (POSIX)
-rw-r--r-- | include/wolf/threads/threads.h | 72 | ||||
-rw-r--r-- | src/GNUmakefile | 11 | ||||
-rw-r--r-- | src/Makefile.W32 | 11 | ||||
-rw-r--r-- | src/threads/mutex.c | 1 | ||||
-rw-r--r-- | src/threads/threads.c | 69 | ||||
-rw-r--r-- | tests/GNUmakefile | 2 | ||||
-rw-r--r-- | tests/Makefile.W32 | 2 | ||||
-rw-r--r-- | tests/threads/GNUmakefile | 22 | ||||
-rw-r--r-- | tests/threads/Makefile.W32 | 29 | ||||
-rw-r--r-- | tests/threads/test_create_join.c | 7 |
10 files changed, 213 insertions, 13 deletions
diff --git a/include/wolf/threads/threads.h b/include/wolf/threads/threads.h new file mode 100644 index 0000000..ff1e645 --- /dev/null +++ b/include/wolf/threads/threads.h @@ -0,0 +1,72 @@ +/* + Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef WOLF_THREADS_H +#define WOLF_THREADS_H + +/** + * @addtogroup wolf_threading Threading support + * @{ + */ + +/** + * @file mutex.h + * @brief Portable mutexes for thread synchronization + * @author Andreas Baumann <abaumann@yahoo.com> + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "port/sys.h" + +#include "errors.h" + +#ifdef HAVE_PTHREADS + +#include <pthread.h> /* for thread functions */ + +typedef pthread_t wolf_thread_t; + +typedef void *( *wolf_thread_func_t )( void * ); + +wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ); +wolf_error_t wolf_thread_join( wolf_thread_t *thread ); + +#endif /* HAVE_PTHREADS */ + +#ifdef _WIN32 + +#include <windows.h> + +typedef HANDLE wolf_thread_t; + +typedef LPTHREAD_START_ROUTINE wolf_thread_func_t; + +wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ); +wolf_error_t wolf_thread_join( wolf_thread_t *thread ); + +#endif /* _WIN32 */ + +#ifdef __cplusplus +} +#endif + +/** @} */ /* @addtogroup wolf_threading */ + +#endif /* ifndef WOLF_THREADS_H */ diff --git a/src/GNUmakefile b/src/GNUmakefile index 64e2394..f8a4707 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile @@ -6,6 +6,10 @@ INCLUDE_DIRS = -I$(TOPDIR)/include/wolf -I. BINS = +THREADING_OBJS = \ + threads/mutex.o \ + threads/threads.o + PORT_OBJS = \ port/string.o \ port/unistd.o \ @@ -26,14 +30,11 @@ DAEMON_OBJS = \ SERVICE_OBJS = \ service/service.o -THREADING_OBJS = \ - threads/mutex.o - OBJS = \ + $(THREADING_OBJS) \ $(PORT_OBJS) \ $(LOG_OBJS) \ - $(DAEMON_OBJS) \ - $(THREADING_OBJS) + $(DAEMON_OBJS) CATALOG_NAME = libwolf diff --git a/src/Makefile.W32 b/src/Makefile.W32 index 10d9295..b321eeb 100644 --- a/src/Makefile.W32 +++ b/src/Makefile.W32 @@ -12,6 +12,10 @@ LIBRARIES = \ wolf.lib \ log\wolfmsg.dll +THREADING_OBJS = \ + threads\mutex.obj \ + threads\threads.obj + PORT_OBJS = \ port\string.obj \ port\stdio.obj \ @@ -24,14 +28,11 @@ LOG_OBJS = \ SERVICE_OBJS = \ service\service.obj -THREADING_OBJS = \ - threads\mutex.obj - OBJS = \ + $(THREADING_OBJS) \ $(PORT_OBJS) \ $(LOG_OBJS) \ - $(SERVICE_OBJS) \ - $(THREADING_OBJS) + $(SERVICE_OBJS) local_all: $(LIBRARIES) diff --git a/src/threads/mutex.c b/src/threads/mutex.c index 509eb51..e710981 100644 --- a/src/threads/mutex.c +++ b/src/threads/mutex.c @@ -15,7 +15,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "errors.h" #include "threads/mutex.h" #if defined HAVE_PTHREADS diff --git a/src/threads/threads.c b/src/threads/threads.c new file mode 100644 index 0000000..79cf4ba --- /dev/null +++ b/src/threads/threads.c @@ -0,0 +1,69 @@ +/* + Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "threads/threads.h" + +#if defined HAVE_PTHREADS + +#include <errno.h> /* for EXXX values */ +#include <assert.h> /* for assertions */ +#include "port/unused.h" + +wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ) { + pthread_attr_t attr; + int res; + + /* intialize thread attributes with defaults */ + res = pthread_attr_init( &attr ); + if( res == ENOMEM ) { + return WOLF_ERR_OUT_OF_MEMORY; + } else if( res != 0 ) { + return WOLF_ERR_INTERNAL; + } + + /* create the thread */ + res = pthread_create( thread, &attr, func, NULL ); + if( res == EINVAL ) { + return WOLF_ERR_INVALID_VALUE; + } else if( res != 0 ) { + return WOLF_ERR_INTERNAL; + } + + /* destroy the thread attributes */ + res = pthread_attr_destroy( &attr ); + assert( res == 0 ); + + return WOLF_OK; +} + +wolf_error_t wolf_thread_join( wolf_thread_t *thread ) { + int res; + void *result; + + res = pthread_join( *thread, &result ); + if( res != 0 ) { + return WOLF_ERR_INTERNAL; + } + + return WOLF_OK; +} + +#endif /* defined HAVE_PTHREADS */ + +#if defined _WIN32 + +#endif /* defined _WIN32 */ diff --git a/tests/GNUmakefile b/tests/GNUmakefile index 6b3b47a..960755c 100644 --- a/tests/GNUmakefile +++ b/tests/GNUmakefile @@ -1,6 +1,6 @@ TOPDIR = .. -SUBDIRS = port log gettext daemon service +SUBDIRS = threads port log gettext daemon service -include $(TOPDIR)/makefiles/gmake/sub.mk diff --git a/tests/Makefile.W32 b/tests/Makefile.W32 index 3795cae..407eb13 100644 --- a/tests/Makefile.W32 +++ b/tests/Makefile.W32 @@ -1,6 +1,6 @@ TOPDIR = .. -SUBDIRS = port log service +SUBDIRS = threads port log service !INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk diff --git a/tests/threads/GNUmakefile b/tests/threads/GNUmakefile new file mode 100644 index 0000000..62c5b8a --- /dev/null +++ b/tests/threads/GNUmakefile @@ -0,0 +1,22 @@ +TOPDIR = ../.. + +INCLUDE_DIRS = \ + -I$(TOPDIR)/include/wolf -I. -I$(TOPDIR)/src + +INCLUDE_LIBS = \ + $(TOPDIR)/src/libwolf.a + +TEST_BINS = \ + test_create_join$(EXE) + +-include $(TOPDIR)/makefiles/gmake/sub.mk + +local_all: + +local_clean: + +local_distclean: + +local_test: + @echo "Testing creating, joining of threads.." + @./test_create_join >/dev/null diff --git a/tests/threads/Makefile.W32 b/tests/threads/Makefile.W32 new file mode 100644 index 0000000..8f30fe6 --- /dev/null +++ b/tests/threads/Makefile.W32 @@ -0,0 +1,29 @@ +TOPDIR = ..\.. + +INCLUDE_DIRS = \ + /I$(TOPDIR)\include\wolf /I. /I$(TOPDIR)\src \ + /D_WIN32_WINNT=0x400 /I"$(PLATFORM_SDK_DIR)\Include" + +INCLUDE_LDFLAGS = \ + /LIBPATH:"$(PLATFORM_SDK_DIR)\lib" + +INCLUDE_LIBS = \ + $(TOPDIR)\src\wolf.lib + +TEST_BINS = \ + test_create_join.exe + +!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk + +# TODO: what is the autogeneration rule for NMAKE? +test_create_join.exe: test_create_join.obj + +local_all: + +local_clean: + +local_distclean: + +local_test: + @echo Testing creating,joining of threads.. + @test_create_join >NUL 2>NUL diff --git a/tests/threads/test_create_join.c b/tests/threads/test_create_join.c new file mode 100644 index 0000000..70fee97 --- /dev/null +++ b/tests/threads/test_create_join.c @@ -0,0 +1,7 @@ +#include "threads/threads.h" + +#include <stdlib.h> /* for exit, EXIT_SUCCESS, free */ + +int main( void ) { + return EXIT_SUCCESS; +} |