cvs: pear /PECL/threads threadapi.c threads.c
| From: | Alan Knowles | Date: | Fri, 09 Aug 2002 04:43:19 +0000 |
| Subject: | cvs: pear /PECL/threads threadapi.c threads.c | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5643@lists.php.net to get a copy of this message | ||
alan_k Fri Aug 9 00:43:19 2002 EDT
Modified files:
/pear/PECL/threads threads.c threadapi.c
Log:
ok alot more of that API now works
modified the thread stucture slightly to make args -> args[2], saves alot of hastle getting the
data back
added debug printfs - see threads.c top for more details
Index: pear/PECL/threads/threads.c diff -u pear/PECL/threads/threads.c:1.3 pear/PECL/threads/threads.c:1.4 --- pear/PECL/threads/threads.c:1.3 Sun Aug 4 20:45:58 2002 +++ pear/PECL/threads/threads.c Fri Aug 9 00:43:19 2002 @@ -21,6 +21,15 @@ #include "config.h" #endif +/* debugging - use THR_PRINTF(("somthing %s", string)); - note the double brackets.. */ +/* #define THR_DEBUG */ + +#ifdef THR_DEBUG +#define THR_PRINTF(v) printf v; fflush(stdout); +#else +#define THR_PRINTF(v) +#endif + #define USE_SERIALIZE #include "php.h" @@ -120,6 +129,7 @@ */ PHP_MINIT_FUNCTION(threads) { + THR_PRINTF(("thread:minit\n")); ZEND_INIT_MODULE_GLOBALS(threads, _php_threads_init_globals, NULL); /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); @@ -136,7 +146,7 @@ UNREGISTER_INI_ENTRIES(); */ /* TODO: initialize some kind of shared memory for shared vars */ - printf("module shutdown in thread\n"); + THR_PRINTF(("module shutdown in thread\n")); return SUCCESS; } /* }}} */ @@ -147,8 +157,10 @@ PHP_RINIT_FUNCTION(threads) { /* TODO: initialize a list for child threads */ + THR_PRINTF(("thread:rinit\n"));; THREADS_G(self) = NULL; THREADS_G(vars) = NULL; + zend_llist_init(&THREADS_G(children), sizeof(THR_THREAD),_php_threads_join_children,1); return SUCCESS; } @@ -161,13 +173,14 @@ { /* destroying the children makes this thread wait for all children to exit */ + THR_PRINTF(("thread:rshutdown\n")); zend_llist_destroy(&THREADS_G(children)); if (!THREADS_G(self)) { /* since self was never set, this is the master thread */ zend_hash_destroy(&THREADS_G(vars)->vars); thr_close_rwlock(THREADS_G(vars)->rwlock); } - printf("request shutdown in thread\n"); + THR_PRINTF(("request shutdown in thread\n")); return SUCCESS; } /* }}} */ @@ -210,7 +223,7 @@ SG(request_info).path_translated = estrdup(THR_SG(thread->parent_tls,request_info).path_translated); /* copy our arguments */ COPY_PZVAL_TO_ZVAL(callback,(zval *)thread->callback); - COPY_PZVAL_TO_ZVAL(args,(zval *)thread->args); + COPY_PZVAL_TO_ZVAL(args,(zval *)thread->args[0]); argv[0] = &args; /* notify we're done copying, don't touch 'thread' after this, it will be invalid memory! */ @@ -225,9 +238,9 @@ /* this can be an issue we have to deal with somehow */ SG(headers_sent) = 1; - SG(request_info).no_headers = 1; + SG(request_info).no_headers = 1; - file_handle.handle.fp = VCWD_FOPEN(SG(request_info).path_translated, "rb"); + file_handle.handle.fp = VCWD_FOPEN(SG(request_info).path_translated, "rb"); file_handle.filename = SG(request_info).path_translated; file_handle.type = ZEND_HANDLE_FP; @@ -284,7 +297,7 @@ thread->start_event = thr_create_event(); thread->callback = callback; thread->parent_tls = tsrm_ls; - thread->args = (void *) args; + thread->args[0] = (void *) args; threadid = thr_thread_create(thread, (void *) phpthreads_create); /* we wait here until the child thread has copied the @@ -296,42 +309,54 @@ /* }}} */ -THR_THREAD_PROC(phpthreads_include) { +/* THR_THREAD_PROC(phpthreads_include) { */ +void phpthreads_include (void * data) { + int callback_len = 0; zend_file_handle file_handle; THR_THREAD *thread = (THR_THREAD *) data; - void **thr_data = thread->args; - char *script_file = (char *)estrdup((char *) thr_data[0]); - THR_SHARED_VARS *vars = (THR_SHARED_VARS *)thr_data[1]; + /* void **thr_data = thread->args; */ + char *script_file; + + /*char *script_file = (char *)estrdup((char *) thr_data[0]);*/ + THR_SHARED_VARS *vars = (THR_SHARED_VARS *) thread->args[1]; TSRMLS_FETCH(); - printf("starting thread %s \n", script_file); fflush(stdout); - thread->args = NULL; + script_file = (char *) thread->args[0]; + THR_PRINTF(("starting thread %s \n", script_file)) + + thread->args[0] = NULL; /* we need to have the same context as the parent thread */ SG(server_context) = THR_SG(thread->parent_tls,server_context); - /* notify we're done copying, don't touch 'thread' after this, - it will be invalid memory! */ - thr_set_event(thread->start_event); php_request_startup(TSRMLS_C); - THREADS_G(self) = thread; /* is ok */ + THREADS_G(self) = thread; /* is ok */ THREADS_G(vars) = vars; /* is ok */ - + + THR_PRINTF(("I'm done doing the unsafe stuff %s \n")); + thr_set_event(thread->start_event); + SG(request_info).path_translated = script_file; /* this can be an issue we have to deal with somehow */ SG(headers_sent) = 1; - SG(request_info).no_headers = 1; + SG(request_info).no_headers = 1; - file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"); + file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"); file_handle.filename = script_file; file_handle.type = ZEND_HANDLE_FP; file_handle.opened_path = NULL; file_handle.free_filename = 1; - php_execute_script(&file_handle TSRMLS_CC); + /* notify we're done copying, don't touch 'thread' after this, + it will be invalid memory! */ + + THR_PRINTF(("sent - running script \n")); + php_execute_script(&file_handle TSRMLS_CC); + + THR_PRINTF(("done running script \n")); /* self destruct */ php_request_shutdown(NULL); thr_thread_exit(0); @@ -345,35 +370,42 @@ char *script_file_in; int len; int threadid; - void *thr_data[2]; + + + pthread_t thread1; + THR_THREAD *thread = NULL; THR_SHARED_VARS *vars = THREADS_G(vars); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &script_file_in,&len) == FAILURE) { RETURN_FALSE; } - + if (!THREADS_G(vars)) { /* only the master thread creates this */ THREADS_G(vars) = (THR_SHARED_VARS *)emalloc(sizeof(THR_SHARED_VARS)); THREADS_G(vars)->rwlock = thr_create_rwlock(); - zend_hash_init(&THREADS_G(vars)->vars, 0, NULL, NULL,1); + zend_hash_init(&THREADS_G(vars)->vars, 0, NULL, NULL,0); vars = THREADS_G(vars); } - - thr_data[0] = script_file_in; - thr_data[1] = THREADS_G(vars); - + script_file= (char *) emalloc(len + 1); + strncpy(script_file , script_file_in, len+1); thread = (THR_THREAD *) emalloc(sizeof(THR_THREAD)); thread->start_event = thr_create_event(); thread->callback = NULL; thread->parent_tls = tsrm_ls; - thread->args = thr_data; - - threadid = thr_thread_create(thread, (void *) phpthreads_include); + thread->args[0] = script_file; + thread->args[1] = (void *) THREADS_G(vars); + THR_PRINTF(("value of args is %x\n", thread->args)); + // pthread_create( &thread1, NULL, (void *) phpthreads_include, (void *) script_file); + threadid = thr_thread_create(thread, (void *) phpthreads_include); /* we wait here until the child thread has copied the parent threads data */ + //printf("helo"); fflush(stdout); + THR_PRINTF(("done creating thread - waiting for all clear \n")) + thr_wait_event(thread->start_event,THR_INFINITE); + THR_PRINTF(("got message that thread had finished initializing \n")) zend_llist_add_element(&THREADS_G(children), (void *)thread); RETURN_TRUE; } @@ -416,7 +448,7 @@ thr_acquire_write_lock(THREADS_G(vars)->rwlock); zend_hash_update(&THREADS_G(vars)->vars, name, len+1, (void *) &var, sizeof(zval *),NULL); if (zend_hash_find(&THREADS_G(vars)->vars, name, len+1, (void **) &xvar) == SUCCESS) { - printf("hash find ok\n"); + THR_PRINTF(("hash find ok\n")); } thr_release_write_lock(THREADS_G(vars)->rwlock); #endif Index: pear/PECL/threads/threadapi.c diff -u pear/PECL/threads/threadapi.c:1.5 pear/PECL/threads/threadapi.c:1.6 --- pear/PECL/threads/threadapi.c:1.5 Thu Aug 8 06:14:20 2002 +++ pear/PECL/threads/threadapi.c Fri Aug 9 00:43:19 2002 @@ -18,6 +18,8 @@ */ #include "threadapi.h" +#include <stdio.h> +#include "zend.h" #ifdef TSRM_WIN32 #include <process.h> #endif @@ -29,8 +31,13 @@ l->lock = tsrm_mutex_alloc(); l->event = thr_create_event(); #elif defined(PTHREADS) - pthread_mutex_init(l->lock,NULL); - pthread_cond_init(l->event->cond,NULL); + + l->lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(l->lock,NULL); + l->event = thr_create_event(); + + + #else #endif @@ -46,7 +53,7 @@ thr_close_event(rwlock->event); #elif defined(PTHREADS) pthread_mutex_destroy(rwlock->lock); - pthread_cond_destroy(rwlock->event->cond); + thr_close_event(rwlock->event); #else #endif free(rwlock); @@ -140,6 +147,7 @@ #else #endif } + /* create an event or condition var */ THR_EVENT thr_create_event(void) @@ -147,9 +155,12 @@ #ifdef TSRM_WIN32 return CreateEvent(NULL,FALSE,FALSE,NULL); #elif defined(PTHREADS) - THR_EVENT c = (THR_EVENT)malloc(sizeof(THR_COND_VAR)); + THR_EVENT c = (THR_EVENT) malloc(sizeof(THR_COND_VAR)); + c->mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); + c->cond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t)); pthread_mutex_init(c->mutex,NULL); pthread_cond_init(c->cond,NULL); + return c; #elif defined(NETWARE) return NULL; @@ -171,12 +182,26 @@ #ifdef TSRM_WIN32 return WaitForSingleObject(event, timeout); #elif defined(PTHREADS) - struct timespec ts_timeout; - struct timeval now; - gettimeofday(&now); - ts_timeout.tv_sec = now.tv_sec; - ts_timeout.tv_nsec = now.tv_usec + timeout; - return pthread_cond_timedwait(event->cond, event->mutex, &ts_timeout); + + + if (timeout == THR_INFINITE) { + int ret; + pthread_mutex_lock(event->mutex); + ret=pthread_cond_wait(event->cond, event->mutex); + pthread_mutex_unlock(event->mutex); + return ret; + } else { + int ret; + struct timespec ts_timeout; + struct timeval now; + gettimeofday(&now); + ts_timeout.tv_sec = now.tv_sec; + ts_timeout.tv_nsec = now.tv_usec + timeout; + pthread_mutex_lock(event->mutex); + ret = pthread_cond_timedwait(event->cond, event->mutex, &ts_timeout); + pthread_mutex_unlock(event->mutex); + return ret; + } #elif defined(NETWARE) return -1; #elif defined(GNUPTH) @@ -197,7 +222,11 @@ #ifdef TSRM_WIN32 return SetEvent(event); #elif defined(PTHREADS) - return pthread_cond_broadcast(event->cond); + int ret; + pthread_mutex_lock(event->mutex); + ret =pthread_cond_broadcast(event->cond); + pthread_mutex_unlock(event->mutex); + return ret; #elif defined(NETWARE) return -1; #elif defined(GNUPTH) @@ -242,7 +271,9 @@ #elif defined(PTHREADS) pthread_cond_destroy(event->cond); pthread_mutex_destroy(event->mutex); - free(event); + free(event->cond); + free(event->mutex); + /* free(event); */ #elif defined(NETWARE) #elif defined(GNUPTH) #elif defined(NSAPI) @@ -260,7 +291,7 @@ #ifdef TSRM_WIN32 thread->thread=(HANDLE)_beginthreadex(NULL,0,callback,(void *)thread,0,&thread->id); #elif defined(PTHREADS) - pthread_create( &thread->thread, NULL, callback, (void *)thread); + pthread_create( &(thread->thread), NULL, callback, (void *)thread); #elif defined(NETWARE) #elif defined(GNUPTH) #elif defined(NSAPI) @@ -275,7 +306,7 @@ #ifdef TSRM_WIN32 _endthreadex(retval); #elif defined(PTHREADS) - pthread_exit(retval); + pthread_exit((void *) &retval); #elif defined(NETWARE) #elif defined(GNUPTH) #elif defined(NSAPI)
Index: pear/PECL/threads/threads.c diff -u pear/PECL/threads/threads.c:1.3 pear/PECL/threads/threads.c:1.4 --- pear/PECL/threads/threads.c:1.3 Sun Aug 4 20:45:58 2002 +++ pear/PECL/threads/threads.c Fri Aug 9 00:43:19 2002 @@ -21,6 +21,15 @@ #include "config.h" #endif +/* debugging - use THR_PRINTF(("somthing %s", string)); - note the double brackets.. */ +/* #define THR_DEBUG */ + +#ifdef THR_DEBUG +#define THR_PRINTF(v) printf v; fflush(stdout); +#else +#define THR_PRINTF(v) +#endif + #define USE_SERIALIZE #include "php.h" @@ -120,6 +129,7 @@ */ PHP_MINIT_FUNCTION(threads) { + THR_PRINTF(("thread:minit\n")); ZEND_INIT_MODULE_GLOBALS(threads, _php_threads_init_globals, NULL); /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); @@ -136,7 +146,7 @@ UNREGISTER_INI_ENTRIES(); */ /* TODO: initialize some kind of shared memory for shared vars */ - printf("module shutdown in thread\n"); + THR_PRINTF(("module shutdown in thread\n")); return SUCCESS; } /* }}} */ @@ -147,8 +157,10 @@ PHP_RINIT_FUNCTION(threads) { /* TODO: initialize a list for child threads */ + THR_PRINTF(("thread:rinit\n"));; THREADS_G(self) = NULL; THREADS_G(vars) = NULL; + zend_llist_init(&THREADS_G(children), sizeof(THR_THREAD),_php_threads_join_children,1); return SUCCESS; } @@ -161,13 +173,14 @@ { /* destroying the children makes this thread wait for all children to exit */ + THR_PRINTF(("thread:rshutdown\n")); zend_llist_destroy(&THREADS_G(children)); if (!THREADS_G(self)) { /* since self was never set, this is the master thread */ zend_hash_destroy(&THREADS_G(vars)->vars); thr_close_rwlock(THREADS_G(vars)->rwlock); } - printf("request shutdown in thread\n"); + THR_PRINTF(("request shutdown in thread\n")); return SUCCESS; } /* }}} */ @@ -210,7 +223,7 @@ SG(request_info).path_translated = estrdup(THR_SG(thread->parent_tls,request_info).path_translated); /* copy our arguments */ COPY_PZVAL_TO_ZVAL(callback,(zval *)thread->callback); - COPY_PZVAL_TO_ZVAL(args,(zval *)thread->args); + COPY_PZVAL_TO_ZVAL(args,(zval *)thread->args[0]); argv[0] = &args; /* notify we're done copying, don't touch 'thread' after this, it will be invalid memory! */ @@ -225,9 +238,9 @@ /* this can be an issue we have to deal with somehow */ SG(headers_sent) = 1; - SG(request_info).no_headers = 1; + SG(request_info).no_headers = 1; - file_handle.handle.fp = VCWD_FOPEN(SG(request_info).path_translated, "rb"); + file_handle.handle.fp = VCWD_FOPEN(SG(request_info).path_translated, "rb"); file_handle.filename = SG(request_info).path_translated; file_handle.type = ZEND_HANDLE_FP; @@ -284,7 +297,7 @@ thread->start_event = thr_create_event(); thread->callback = callback; thread->parent_tls = tsrm_ls; - thread->args = (void *) args; + thread->args[0] = (void *) args; threadid = thr_thread_create(thread, (void *) phpthreads_create); /* we wait here until the child thread has copied the @@ -296,42 +309,54 @@ /* }}} */ -THR_THREAD_PROC(phpthreads_include) { +/* THR_THREAD_PROC(phpthreads_include) { */ +void phpthreads_include (void * data) { + int callback_len = 0; zend_file_handle file_handle; THR_THREAD *thread = (THR_THREAD *) data; - void **thr_data = thread->args; - char *script_file = (char *)estrdup((char *) thr_data[0]); - THR_SHARED_VARS *vars = (THR_SHARED_VARS *)thr_data[1]; + /* void **thr_data = thread->args; */ + char *script_file; + + /*char *script_file = (char *)estrdup((char *) thr_data[0]);*/ + THR_SHARED_VARS *vars = (THR_SHARED_VARS *) thread->args[1]; TSRMLS_FETCH(); - printf("starting thread %s \n", script_file); fflush(stdout); - thread->args = NULL; + script_file = (char *) thread->args[0]; + THR_PRINTF(("starting thread %s \n", script_file)) + + thread->args[0] = NULL; /* we need to have the same context as the parent thread */ SG(server_context) = THR_SG(thread->parent_tls,server_context); - /* notify we're done copying, don't touch 'thread' after this, - it will be invalid memory! */ - thr_set_event(thread->start_event); php_request_startup(TSRMLS_C); - THREADS_G(self) = thread; /* is ok */ + THREADS_G(self) = thread; /* is ok */ THREADS_G(vars) = vars; /* is ok */ - + + THR_PRINTF(("I'm done doing the unsafe stuff %s \n")); + thr_set_event(thread->start_event); + SG(request_info).path_translated = script_file; /* this can be an issue we have to deal with somehow */ SG(headers_sent) = 1; - SG(request_info).no_headers = 1; + SG(request_info).no_headers = 1; - file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"); + file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"); file_handle.filename = script_file; file_handle.type = ZEND_HANDLE_FP; file_handle.opened_path = NULL; file_handle.free_filename = 1; - php_execute_script(&file_handle TSRMLS_CC); + /* notify we're done copying, don't touch 'thread' after this, + it will be invalid memory! */ + + THR_PRINTF(("sent - running script \n")); + php_execute_script(&file_handle TSRMLS_CC); + + THR_PRINTF(("done running script \n")); /* self destruct */ php_request_shutdown(NULL); thr_thread_exit(0); @@ -345,35 +370,42 @@ char *script_file_in; int len; int threadid; - void *thr_data[2]; + + + pthread_t thread1; + THR_THREAD *thread = NULL; THR_SHARED_VARS *vars = THREADS_G(vars); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &script_file_in,&len) == FAILURE) { RETURN_FALSE; } - + if (!THREADS_G(vars)) { /* only the master thread creates this */ THREADS_G(vars) = (THR_SHARED_VARS *)emalloc(sizeof(THR_SHARED_VARS)); THREADS_G(vars)->rwlock = thr_create_rwlock(); - zend_hash_init(&THREADS_G(vars)->vars, 0, NULL, NULL,1); + zend_hash_init(&THREADS_G(vars)->vars, 0, NULL, NULL,0); vars = THREADS_G(vars); } - - thr_data[0] = script_file_in; - thr_data[1] = THREADS_G(vars); - + script_file= (char *) emalloc(len + 1); + strncpy(script_file , script_file_in, len+1); thread = (THR_THREAD *) emalloc(sizeof(THR_THREAD)); thread->start_event = thr_create_event(); thread->callback = NULL; thread->parent_tls = tsrm_ls; - thread->args = thr_data; - - threadid = thr_thread_create(thread, (void *) phpthreads_include); + thread->args[0] = script_file; + thread->args[1] = (void *) THREADS_G(vars); + THR_PRINTF(("value of args is %x\n", thread->args)); + // pthread_create( &thread1, NULL, (void *) phpthreads_include, (void *) script_file); + threadid = thr_thread_create(thread, (void *) phpthreads_include); /* we wait here until the child thread has copied the parent threads data */ + //printf("helo"); fflush(stdout); + THR_PRINTF(("done creating thread - waiting for all clear \n")) + thr_wait_event(thread->start_event,THR_INFINITE); + THR_PRINTF(("got message that thread had finished initializing \n")) zend_llist_add_element(&THREADS_G(children), (void *)thread); RETURN_TRUE; } @@ -416,7 +448,7 @@ thr_acquire_write_lock(THREADS_G(vars)->rwlock); zend_hash_update(&THREADS_G(vars)->vars, name, len+1, (void *) &var, sizeof(zval *),NULL); if (zend_hash_find(&THREADS_G(vars)->vars, name, len+1, (void **) &xvar) == SUCCESS) { - printf("hash find ok\n"); + THR_PRINTF(("hash find ok\n")); } thr_release_write_lock(THREADS_G(vars)->rwlock); #endif Index: pear/PECL/threads/threadapi.c diff -u pear/PECL/threads/threadapi.c:1.5 pear/PECL/threads/threadapi.c:1.6 --- pear/PECL/threads/threadapi.c:1.5 Thu Aug 8 06:14:20 2002 +++ pear/PECL/threads/threadapi.c Fri Aug 9 00:43:19 2002 @@ -18,6 +18,8 @@ */ #include "threadapi.h" +#include <stdio.h> +#include "zend.h" #ifdef TSRM_WIN32 #include <process.h> #endif @@ -29,8 +31,13 @@ l->lock = tsrm_mutex_alloc(); l->event = thr_create_event(); #elif defined(PTHREADS) - pthread_mutex_init(l->lock,NULL); - pthread_cond_init(l->event->cond,NULL); + + l->lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(l->lock,NULL); + l->event = thr_create_event(); + + + #else #endif @@ -46,7 +53,7 @@ thr_close_event(rwlock->event); #elif defined(PTHREADS) pthread_mutex_destroy(rwlock->lock); - pthread_cond_destroy(rwlock->event->cond); + thr_close_event(rwlock->event); #else #endif free(rwlock); @@ -140,6 +147,7 @@ #else #endif } + /* create an event or condition var */ THR_EVENT thr_create_event(void) @@ -147,9 +155,12 @@ #ifdef TSRM_WIN32 return CreateEvent(NULL,FALSE,FALSE,NULL); #elif defined(PTHREADS) - THR_EVENT c = (THR_EVENT)malloc(sizeof(THR_COND_VAR)); + THR_EVENT c = (THR_EVENT) malloc(sizeof(THR_COND_VAR)); + c->mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); + c->cond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t)); pthread_mutex_init(c->mutex,NULL); pthread_cond_init(c->cond,NULL); + return c; #elif defined(NETWARE) return NULL; @@ -171,12 +182,26 @@ #ifdef TSRM_WIN32 return WaitForSingleObject(event, timeout); #elif defined(PTHREADS) - struct timespec ts_timeout; - struct timeval now; - gettimeofday(&now); - ts_timeout.tv_sec = now.tv_sec; - ts_timeout.tv_nsec = now.tv_usec + timeout; - return pthread_cond_timedwait(event->cond, event->mutex, &ts_timeout); + + + if (timeout == THR_INFINITE) { + int ret; + pthread_mutex_lock(event->mutex); + ret=pthread_cond_wait(event->cond, event->mutex); + pthread_mutex_unlock(event->mutex); + return ret; + } else { + int ret; + struct timespec ts_timeout; + struct timeval now; + gettimeofday(&now); + ts_timeout.tv_sec = now.tv_sec; + ts_timeout.tv_nsec = now.tv_usec + timeout; + pthread_mutex_lock(event->mutex); + ret = pthread_cond_timedwait(event->cond, event->mutex, &ts_timeout); + pthread_mutex_unlock(event->mutex); + return ret; + } #elif defined(NETWARE) return -1; #elif defined(GNUPTH) @@ -197,7 +222,11 @@ #ifdef TSRM_WIN32 return SetEvent(event); #elif defined(PTHREADS) - return pthread_cond_broadcast(event->cond); + int ret; + pthread_mutex_lock(event->mutex); + ret =pthread_cond_broadcast(event->cond); + pthread_mutex_unlock(event->mutex); + return ret; #elif defined(NETWARE) return -1; #elif defined(GNUPTH) @@ -242,7 +271,9 @@ #elif defined(PTHREADS) pthread_cond_destroy(event->cond); pthread_mutex_destroy(event->mutex); - free(event); + free(event->cond); + free(event->mutex); + /* free(event); */ #elif defined(NETWARE) #elif defined(GNUPTH) #elif defined(NSAPI) @@ -260,7 +291,7 @@ #ifdef TSRM_WIN32 thread->thread=(HANDLE)_beginthreadex(NULL,0,callback,(void *)thread,0,&thread->id); #elif defined(PTHREADS) - pthread_create( &thread->thread, NULL, callback, (void *)thread); + pthread_create( &(thread->thread), NULL, callback, (void *)thread); #elif defined(NETWARE) #elif defined(GNUPTH) #elif defined(NSAPI) @@ -275,7 +306,7 @@ #ifdef TSRM_WIN32 _endthreadex(retval); #elif defined(PTHREADS) - pthread_exit(retval); + pthread_exit((void *) &retval); #elif defined(NETWARE) #elif defined(GNUPTH) #elif defined(NSAPI)