From 9f9d937905ae906858dfa47ed60535b411895efd Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 16 Mar 2009 11:16:34 +0000 Subject: [PATCH] now all compiles under linux...need replacement for CONDITION_VARIABLE for win32 --- configure.ac | 2 ++ include/scot/posix/thread.h | 4 ++-- include/scot/stream.h | 2 +- include/scot/win32/scot_types.h | 2 +- include/scot/win32/thread.h | 6 +++++- src/Makefile.am | 9 ++++++++- src/event_listener.c | 2 +- src/exception.c | 2 +- src/posix/thread.c | 14 +++++++++----- src/stream.c | 4 ++-- src/stream_pool_base.c | 10 +++++----- src/win32/stream_ctl.c | 4 ++-- src/win32/thread.c | 34 +++++++++++++++++++++++++++++++-- test/Makefile.am | 10 ++++++---- 14 files changed, 77 insertions(+), 28 deletions(-) diff --git a/configure.ac b/configure.ac index 6cb4712..d54cbd3 100644 --- a/configure.ac +++ b/configure.ac @@ -54,6 +54,8 @@ AC_SUBST(SOCK_LIB) AC_SUBST(THREAD_LIB) AC_SUBST(THREAD_CFLAGS) +AM_PROG_CC_C_O + AM_CONDITIONAL(USE_THREADS, test "x$thread" != "x") AM_CONDITIONAL(PTHREAD, test "x$thread" == "xPTHREAD") AM_CONDITIONAL(WTHREAD, test "x$thread" == "xWTHREAD") diff --git a/include/scot/posix/thread.h b/include/scot/posix/thread.h index 4038a10..3964d42 100644 --- a/include/scot/posix/thread.h +++ b/include/scot/posix/thread.h @@ -47,7 +47,7 @@ #define THREAD_ID pthread_self () #define SELF_THREAD pthread_self () #define THREAD_EQUAL pthread_equal -#define NEW_THREAD(th,f, a) thread_new ((th), (f), (a)) +#define NEW_THREAD(f, a) thread_new ((f), (a)) #define END_THREAD thread_end #define DETACH_THREAD(thread) pthread_detach ((thread)) #define CANCEL_THREAD(thread) pthread_cancel ((thread)) @@ -89,7 +89,7 @@ typedef T_PROC_RET (*scot_thread_entry_fptr) (void *); -void thread_new (THREAD_T *, T_PROC_RET (*)(void *), void*); +THREAD_T thread_new (T_PROC_RET (*)(void *), void*); int thread_join (THREAD_T, uint32_t); void thread_end (THREAD_T, uint32_t); void thread_mutex_new (THREAD_MUTEX_T *); diff --git a/include/scot/stream.h b/include/scot/stream.h index a436a7e..7bfcf86 100644 --- a/include/scot/stream.h +++ b/include/scot/stream.h @@ -42,7 +42,7 @@ #define SCOT_STREAM_TYPE_INOTIFY 4 #ifdef WIN32 -# include +# include # include # define SCOT_FILE_READ win_file_read #else diff --git a/include/scot/win32/scot_types.h b/include/scot/win32/scot_types.h index 4530747..2700a5a 100644 --- a/include/scot/win32/scot_types.h +++ b/include/scot/win32/scot_types.h @@ -1,6 +1,6 @@ #ifndef SCOT_TYPES_H #define SCOT_TYPES_H -#include +#include #endif /* SCOT_TYPES_H */ diff --git a/include/scot/win32/thread.h b/include/scot/win32/thread.h index d10a5f7..ed06abb 100644 --- a/include/scot/win32/thread.h +++ b/include/scot/win32/thread.h @@ -40,7 +40,9 @@ #define THREAD_ID_T DWORD #define THREAD_ID GetCurrentThreadId () #define SELF_THREAD GetCurrentThread () -#define NEW_THREAD thread_new +#define THREAD_EQUAL(a, b) thread_equal ((a), (b)) +#define NEW_THREAD(f, a) thread_new ((f), (a)) +#define END_THREAD thread_end #define CANCEL_THREAD(thread) (! TerminateThread ((thread), -1)) #define EXIT_THREAD(retval) _endthreadex ((DWORD) (retval)) #define JOIN_THREAD thread_join @@ -73,6 +75,8 @@ typedef T_PROC_RET (*scot_thread_entry_fptr) (void *); THREAD_T thread_new (T_PROC_RET (*)(void *), void*); int thread_join (THREAD_T, unsigned int); +void thread_end (THREAD_T, unsigned int); +int thread_equal (THREAD_T, THREAD_T); THREAD_MUTEX_T thread_mutex_new (void); int thread_mutex_lock (THREAD_MUTEX_T *); diff --git a/src/Makefile.am b/src/Makefile.am index a2985c6..532cc82 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,12 +6,19 @@ LIBS += @THREAD_LIB@ @SOCK_LIB@ INCLUDES = -I../include @INOTIFY_INCLUDES@ +#libscot_source = scot_common.c cmdla.c \ +# list.c stack.c queue.c \ +# thread.c dir.c \ +# exception.c scot_exceptions.c \ +# stream.c stream_ctl.c socket.c socket_in.c \ +# event.c event_listener.c + libscot_source = scot_common.c cmdla.c \ list.c stack.c queue.c \ thread.c dir.c \ exception.c scot_exceptions.c \ stream.c stream_ctl.c socket.c socket_in.c \ - event.c event_listener.c \ + event.c event_listener.c \ stream_pool_base.c stream_pool_management.c \ stream_pool_fraction.c spf_thread_impl.c diff --git a/src/event_listener.c b/src/event_listener.c index 1471c2d..0d86676 100644 --- a/src/event_listener.c +++ b/src/event_listener.c @@ -62,7 +62,7 @@ scot_event_listener_fini ( void scot_event_listener_start (struct scot_event_listener * l) { - NEW_THREAD (&l->thread_handle, l->el_entry_func, l); + l->thread_handle = NEW_THREAD (l->el_entry_func, l); } void diff --git a/src/exception.c b/src/exception.c index 45e457b..7e1997e 100644 --- a/src/exception.c +++ b/src/exception.c @@ -138,7 +138,7 @@ static int compare_thread_excenv_t (const thread_excenv_t *a , const thread_excenv_t *b) { - return ! THREAD_EQUAL (*(THREAD_T *) a, *(THREAD_T *) b); + return ! THREAD_EQUAL (a->thread_handle, b->thread_handle); } /** diff --git a/src/posix/thread.c b/src/posix/thread.c index 69fe166..10aebdc 100644 --- a/src/posix/thread.c +++ b/src/posix/thread.c @@ -36,13 +36,17 @@ join_thread (void * arg) return NULL; } -void -thread_new (THREAD_T * handle, T_PROC_RET (*t_proc)(void *), void* arg) +THREAD_T +thread_new (T_PROC_RET (*t_proc)(void *), void* arg) { - if (pthread_create (handle, NULL, t_proc, arg) != 0) + THREAD_T handle; + + if (pthread_create (&handle, NULL, t_proc, arg) != 0) { - SCOT_MEM_ZERO (handle, sizeof (THREAD_T)); + SCOT_MEM_ZERO (&handle, sizeof (THREAD_T)); } + + return handle; } void @@ -103,7 +107,7 @@ thread_join (THREAD_T thread, uint32_t timeout) perror ("problem achiving mutex within join"); } - NEW_THREAD (&join_thr, join_thread, (void *) &join_cond); + join_thr = NEW_THREAD (join_thread, (void *) &join_cond); status = pthread_cond_timedwait ( &(join_cond.join_alarm_cond), diff --git a/src/stream.c b/src/stream.c index 7877cf7..7bf8515 100644 --- a/src/stream.c +++ b/src/stream.c @@ -5,8 +5,8 @@ #ifndef WIN32 # include #else -# include -# include +# include +# include #endif #include diff --git a/src/stream_pool_base.c b/src/stream_pool_base.c index faf87ad..486adb2 100644 --- a/src/stream_pool_base.c +++ b/src/stream_pool_base.c @@ -124,7 +124,7 @@ scot_stream_pool_main_loop (struct scot_stream_pool * sp) f_node = list_scot_sp_fraction_next (f_node); f = list_scot_sp_fraction_retrive (f_node); - NEW_THREAD (&f->thread_handle, f->spf_entry_func, f); + f->thread_handle = NEW_THREAD (f->spf_entry_func, f); f->thread_run_flg = 1; } @@ -150,7 +150,7 @@ scot_stream_pool_main_loop (struct scot_stream_pool * sp) END_THREAD (f->thread_handle, INFINITE); thread_exc_end (f->thread_handle); } - NEW_THREAD (&f->thread_handle, f->spf_entry_func, f); + f->thread_handle = NEW_THREAD (f->spf_entry_func, f); f->thread_run_flg = 1; } break; @@ -164,7 +164,7 @@ scot_stream_pool_main_loop (struct scot_stream_pool * sp) if (f->thread_run_flg == 0) { - NEW_THREAD (&f->thread_handle, f->spf_entry_func, f); + f->thread_handle = NEW_THREAD (f->spf_entry_func, f); f->thread_run_flg = 1; } } @@ -193,7 +193,7 @@ scot_stream_pool_main_loop (struct scot_stream_pool * sp) END_THREAD (f->thread_handle, INFINITE); thread_exc_end (f->thread_handle); } - NEW_THREAD (&f->thread_handle, f->spf_entry_func, f); + f->thread_handle = NEW_THREAD (f->spf_entry_func, f); f->thread_run_flg = 1; break; @@ -201,7 +201,7 @@ scot_stream_pool_main_loop (struct scot_stream_pool * sp) f = (struct scot_sp_fraction *) sp->cmd_arg; if (f->thread_run_flg == 0) { - NEW_THREAD (&f->thread_handle, f->spf_entry_func, f); + f->thread_handle = NEW_THREAD (f->spf_entry_func, f); f->thread_run_flg = 1; } break; diff --git a/src/win32/stream_ctl.c b/src/win32/stream_ctl.c index d6fab43..524060c 100644 --- a/src/win32/stream_ctl.c +++ b/src/win32/stream_ctl.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include diff --git a/src/win32/thread.c b/src/win32/thread.c index 1a07e42..390709a 100644 --- a/src/win32/thread.c +++ b/src/win32/thread.c @@ -1,9 +1,39 @@ #include +#include -THREAD_T +int +thread_equal (THREAD_T a, THREAD_T b) +{ + return (a == b); +} + +void +thread_end (THREAD_T thread, unsigned int timeout) +{ + int err; + + err = CANCEL_THREAD (thread); + if (err != 0) + THROW (EXC (EXC_ERROR, + err, + "[EVENT_SOURCE_THREAD]cancel request failed")); + + err = JOIN_THREAD (thread, timeout); + if (err == JOIN_TIMEOUT) + THROW (EXC (EXC_ERROR, + err, + "[EVENT_SOURCE_THREAD]timeout exceeded while waiting " + "for threads end")); + if (err == JOIN_ERROR) + THROW (EXC (EXC_ERROR, + err, + "[EVENT_SOURCE_THREAD]failure on waiting for threads end")); +} + +THREAD_T thread_new (T_PROC_RET (*t_proc)(void *), void* arg) { - return (_beginthreadex (NULL, 0, t_proc, arg, 0, NULL)); + return _beginthreadex (NULL, 0, t_proc, arg, 0, NULL); } int diff --git a/test/Makefile.am b/test/Makefile.am index 64ba7b9..81dc0ef 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,9 +1,9 @@ LTLIBS = $(INTLLIBS) @SOCK_LIB@ LIBS = $(INTLLIBS) @SOCK_LIB@ -PROGS = scot_test list_test exc_test \ - tcp_socket_serv tcp_socket_clie \ - multiserv +PROGS = scot_test list_test exc_test + +#PROGS = scot_test list_test exc_test INCLUDES = -I../include @INOTIFY_INCLUDES@ @@ -12,7 +12,9 @@ PROGS += exc_thr_test endif if !WIN32 -PROGS += unx_socket_serv unx_socket_clie fs_events +PROGS += unx_socket_serv unx_socket_clie fs_events \ + tcp_socket_serv tcp_socket_clie \ + multiserv endif bin_PROGRAMS = $(PROGS)