Skip to content

Commit 4a445c1

Browse files
author
Sascha Schumann
committed
Fix leak in pthreads, and add initial support for GNU Pth
1 parent 6f41683 commit 4a445c1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

TSRM/TSRM.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ static int tsrm_debug_status;
6363
/* Startup TSRM (call once for the entire process) */
6464
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_status)
6565
{
66+
#if defined(GNUPTH)
67+
pth_init();
68+
#endif
69+
6670
tsrm_tls_table_size = expected_threads;
6771
tsrm_tls_table = (tsrm_tls_entry **) calloc(tsrm_tls_table_size, sizeof(tsrm_tls_entry *));
6872
if (!tsrm_tls_table) {
@@ -116,6 +120,9 @@ TSRM_API void tsrm_shutdown(void)
116120
}
117121
tsrm_mutex_free(tsmm_mutex);
118122
tsrm_debug("Shutdown TSRM\n");
123+
#if defined(GNUPTH)
124+
pth_kill();
125+
#endif
119126
}
120127

121128

@@ -301,6 +308,8 @@ TSRM_API THREAD_T tsrm_thread_id(void)
301308
{
302309
#ifdef WIN32
303310
return GetCurrentThreadId();
311+
#elif defined(GNUPTH)
312+
return pth_self();
304313
#elif defined(PTHREADS)
305314
return pthread_self();
306315
#elif defined(NSAPI)
@@ -318,6 +327,9 @@ TSRM_API MUTEX_T tsrm_mutex_alloc( void )
318327

319328
#ifdef WIN32
320329
mutexp = CreateMutex(NULL,FALSE,NULL);
330+
#elif defined(GNUPTH)
331+
mutexp = (MUTEX_T) malloc(sizeof(*mutexp));
332+
pth_mutex_init(mutexp);
321333
#elif defined(PTHREADS)
322334
mutexp = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
323335
pthread_mutex_init(mutexp,NULL);
@@ -339,8 +351,11 @@ TSRM_API void tsrm_mutex_free( MUTEX_T mutexp )
339351
if (mutexp) {
340352
#ifdef WIN32
341353
CloseHandle(mutexp);
354+
#elif defined(GNUPTH)
355+
free(mutexp);
342356
#elif defined(PTHREADS)
343357
pthread_mutex_destroy(mutexp);
358+
free(mutexp);
344359
#elif defined(NSAPI)
345360
crit_terminate(mutexp);
346361
#elif defined(PI3WEB)
@@ -361,6 +376,8 @@ TSRM_API int tsrm_mutex_lock( MUTEX_T mutexp )
361376
#endif
362377
#ifdef WIN32
363378
return WaitForSingleObject(mutexp,1000);
379+
#elif defined(GNUPTH)
380+
return pth_mutex_acquire(mutexp, 0, NULL);
364381
#elif defined(PTHREADS)
365382
return pthread_mutex_lock(mutexp);
366383
#elif defined(NSAPI)
@@ -379,6 +396,8 @@ TSRM_API int tsrm_mutex_unlock( MUTEX_T mutexp )
379396
#endif
380397
#ifdef WIN32
381398
return ReleaseMutex(mutexp);
399+
#elif defined(GNUPTH)
400+
return pth_mutex_release(mutexp);
382401
#elif defined(PTHREADS)
383402
return pthread_mutex_unlock(mutexp);
384403
#elif defined(NSAPI)

TSRM/TSRM.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#if WIN32||WINNT
2929
# include <windows.h>
30+
#elif defined(GNUPTH)
31+
# include <pth.h>
3032
#elif defined(PTHREADS)
3133
# include <pthread.h>
3234
#endif
@@ -48,6 +50,9 @@ typedef int ts_rsrc_id;
4850
#if defined(WIN32)
4951
# define THREAD_T DWORD
5052
# define MUTEX_T void *
53+
#elif defined(GNUPTH)
54+
# define THREAD_T pth_attr_t
55+
# define MUTEX_T pth_mutex_t *
5156
#elif defined(PTHREADS)
5257
# define THREAD_T pthread_t
5358
# define MUTEX_T pthread_mutex_t *

0 commit comments

Comments
 (0)