7 #include "xmlrpc_config.h"
10 #include "xmlrpc-c/string_int.h"
12 #include "xmlrpc-c/abyss.h"
22 TThreadDoneFn * threadDone;
25 /* We used to have THREAD_STACK_SIZE = 16K, which was said to be the
26 minimum stack size on Win32. Scott Kolodzeski found in November
27 2005 that this was insufficient for 64 bit Solaris -- we fail
28 when creating the first thread. So we changed to 128K.
30 #define THREAD_STACK_SIZE (128*1024L)
33 typedef void * (pthreadStartRoutine)(void *);
37 static pthreadStartRoutine pthreadStart;
40 pthreadStart(void * const arg) {
42 struct abyss_thread * const threadP = arg;
43 abyss_bool const executeTrue = true;
45 pthread_cleanup_push(threadP->threadDone, threadP->userHandle);
47 threadP->func(threadP->userHandle);
49 pthread_cleanup_pop(executeTrue);
57 ThreadCreate(TThread ** const threadPP,
58 void * const userHandle,
59 TThreadProc * const func,
60 TThreadDoneFn * const threadDone,
61 abyss_bool const useSigchld ATTR_UNUSED,
62 const char ** const errorP) {
68 xmlrpc_asprintf(errorP,
69 "Can't allocate memory for thread descriptor.");
74 pthread_attr_init(&attr);
76 pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE);
78 threadP->userHandle = userHandle;
80 threadP->threadDone = threadDone;
82 rc = pthread_create(&threadP->thread, &attr,
83 pthreadStart, threadP);
89 errorP, "pthread_create() failed, errno = %d (%s)",
90 errno, strerror(errno));
92 pthread_attr_destroy(&attr);
102 ThreadRun(TThread * const threadP ATTR_UNUSED) {
109 ThreadStop(TThread * const threadP ATTR_UNUSED) {
116 ThreadKill(TThread * const threadP ATTR_UNUSED) {
118 return (pthread_kill(threadP->thread, SIGTERM) == 0);
124 ThreadWaitAndRelease(TThread * const threadP) {
128 pthread_join(threadP->thread, &threadReturn);
136 ThreadExit(int const retValue) {
138 pthread_exit((void*)&retValue);
140 /* Note that the above runs our cleanup routine (which we registered
141 with pthread_cleanup_push() before exiting.
148 ThreadRelease(TThread * const threadP) {
150 pthread_detach(threadP->thread);
166 ThreadUpdateStatus(TThread * const threadP ATTR_UNUSED) {
168 /* Threads keep their own statuses up to date, so there's nothing
176 ThreadHandleSigchld(pid_t const pid ATTR_UNUSED) {
178 /* Death of a child signals have nothing to do with pthreads */
183 /*********************************************************************
185 *********************************************************************/
190 MutexCreate(TMutex * const mutexP) {
192 return (pthread_mutex_init(mutexP, NULL) == 0);
198 MutexLock(TMutex * const mutexP) {
199 return (pthread_mutex_lock(mutexP) == 0);
205 MutexUnlock(TMutex * const mutexP) {
206 return (pthread_mutex_unlock(mutexP) == 0);
212 MutexTryLock(TMutex * const mutexP) {
213 return (pthread_mutex_trylock(mutexP) == 0);
219 MutexFree(TMutex * const mutexP) {
220 pthread_mutex_destroy(mutexP);