22
24
TThreadDoneFn * threadDone;
25
/* We used to have THREAD_STACK_SIZE = 16K, which was said to be the
27
/* We used to have MIN_STACK_SIZE = 16K, which was said to be the
26
28
minimum stack size on Win32. Scott Kolodzeski found in November
27
29
2005 that this was insufficient for 64 bit Solaris -- we fail
28
30
when creating the first thread. So we changed to 128K.
30
#define THREAD_STACK_SIZE (128*1024L)
32
#define MIN_STACK_SIZE (128*1024L)
33
35
typedef void * (pthreadStartRoutine)(void *);
40
42
pthreadStart(void * const arg) {
42
44
struct abyss_thread * const threadP = arg;
43
abyss_bool const executeTrue = true;
45
bool const executeTrue = true;
45
47
pthread_cleanup_push(threadP->threadDone, threadP->userHandle);
49
51
pthread_cleanup_pop(executeTrue);
53
/* Note that func() may not return; it may just exit the thread,
54
by calling ThreadExit(), in which case code here doesn't run.
56
threadP->threadDone(threadP->userHandle);
57
65
ThreadCreate(TThread ** const threadPP,
58
66
void * const userHandle,
59
67
TThreadProc * const func,
60
68
TThreadDoneFn * const threadDone,
61
abyss_bool const useSigchld ATTR_UNUSED,
69
bool const useSigchld ATTR_UNUSED,
70
size_t const stackSize,
62
71
const char ** const errorP) {
68
xmlrpc_asprintf(errorP,
69
"Can't allocate memory for thread descriptor.");
73
if ((size_t)(int)stackSize != stackSize)
74
xmlrpc_asprintf(errorP, "Stack size %lu is too big",
75
(unsigned long)stackSize);
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);
81
xmlrpc_asprintf(errorP,
82
"Can't allocate memory for thread descriptor.");
87
pthread_attr_init(&attr);
89
pthread_attr_setstacksize(&attr, MAX(MIN_STACK_SIZE, stackSize));
91
threadP->userHandle = userHandle;
93
threadP->threadDone = threadDone;
95
rc = pthread_create(&threadP->thread, &attr,
96
pthreadStart, threadP);
102
errorP, "pthread_create() failed, errno = %d (%s)",
103
errno, strerror(errno));
105
pthread_attr_destroy(&attr);
102
116
ThreadRun(TThread * const threadP ATTR_UNUSED) {
109
123
ThreadStop(TThread * const threadP ATTR_UNUSED) {
116
130
ThreadKill(TThread * const threadP ATTR_UNUSED) {
118
132
return (pthread_kill(threadP->thread, SIGTERM) == 0);
185
200
*********************************************************************/
190
MutexCreate(TMutex * const mutexP) {
192
return (pthread_mutex_init(mutexP, NULL) == 0);
203
pthread_mutex_t pthreadMutex;
208
MutexCreate(TMutex ** const mutexPP) {
217
rc = pthread_mutex_init(&mutexP->pthreadMutex, NULL);
219
succeeded = (rc == 0);
198
234
MutexLock(TMutex * const mutexP) {
199
return (pthread_mutex_lock(mutexP) == 0);
235
return (pthread_mutex_lock(&mutexP->pthreadMutex) == 0);
205
241
MutexUnlock(TMutex * const mutexP) {
206
return (pthread_mutex_unlock(mutexP) == 0);
242
return (pthread_mutex_unlock(&mutexP->pthreadMutex) == 0);
212
248
MutexTryLock(TMutex * const mutexP) {
213
return (pthread_mutex_trylock(mutexP) == 0);
249
return (pthread_mutex_trylock(&mutexP->pthreadMutex) == 0);
219
MutexFree(TMutex * const mutexP) {
220
pthread_mutex_destroy(mutexP);
255
MutexDestroy(TMutex * const mutexP) {
256
pthread_mutex_destroy(&mutexP->pthreadMutex);