Skip to content

Commit 67b1257

Browse files
authored
Replaced Semaphores with Mutexes in pthread library (#294)
* Wrong Printf version requirement printing * Small profile fixes * Fixed crash when debug is enabled * Semaphores are replaced by Mutexes when possible vfprintf now use an internal optimized version of __putc_unlocked that doesn't call clib4 every time * Reworked ctype implementation using newlib one * don't call check_abort in stdio functions that are calling it already into hook * ctype functions was using wrong _P define (while ctype inline #define the correct one) * More optimizations on I/O functions that avoid useless check abort calls * More optimizations to I/O operations. Now stdout and stderr are fully buffered and no more line buffered * Some more optimizations * more read fixes * Fixed an error on pthreds was causing DSI in some occasions. Small fixes * Fixed a problem cased by wrong Input/Output/Error streams created before checking if process is run from WB * Speedup lookup_name function was loading and parsing resolv conf file at each call * Fixed fopen was slowing down due a mistake on file buffer. setenv speedup gethostbyaddr speedup * Replaced Semaphores with Mutexes in pthread library Clean up code a little bit
1 parent 5b3cae8 commit 67b1257

File tree

13 files changed

+45
-41
lines changed

13 files changed

+45
-41
lines changed

library/crtbegin.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ clib4_start(char *args, const int32 arglen, struct Library *sysbase) {
129129
struct MsgPort *mp = &me->pr_MsgPort;
130130
iexec->WaitPort(mp);
131131
sms = (struct WBStartup *) iexec->GetMsg(mp);
132-
iexec->DebugPrintF("<crtbegin> : NumArgs == %ld\n", sms->sm_NumArgs);
133132
}
134133

135134
IExec = iexec;

library/fcntl/open.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ __open_r(struct _clib4 *__clib4, const char *path_name, int open_flag, ... /* mo
362362
SET_FLAG(fd->fd_Flags, FDF_CREATED);
363363
}
364364
if (FLAG_IS_SET(open_flag, O_LITTLE_ENDIAN)) {
365-
DebugPrintF("open() called with Little Endian mode\n");
366-
DebugPrintF("%ld\n", fd_slot_number);
365+
SHOWMSG("open() called with Little Endian mode\n");
366+
D(("%ld\n", fd_slot_number));
367367
SET_FLAG(fd->fd_Flags, FDF_LITTLE_ENDIAN);
368368
}
369369

library/fcntl/read.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ read(int file_descriptor, void *buffer, size_t num_bytes) {
119119

120120
struct fd *fd = __get_file_descriptor(__clib4, file_descriptor);
121121
if (!FLAG_IS_SET(fd->fd_Flags, FDF_IS_SOCKET) && FLAG_IS_SET(fd->fd_Flags, FDF_LITTLE_ENDIAN)) {
122-
DebugPrintF("[read] Reading in Little endian mode\n");
122+
SHOWMSG("[read] Reading in Little endian mode\n");
123123
if (num_bytes == 2) {
124124
byteswap16(buffer);
125125
} else if (num_bytes == 4) {

library/pthread/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ typedef struct {
9797
extern struct Library *_DOSBase;
9898
extern struct DOSIFace *_IDOS;
9999

100-
extern struct SignalSemaphore thread_sem;
100+
extern APTR thread_sem;
101101
extern ThreadInfo threads[PTHREAD_THREADS_MAX];
102-
extern struct SignalSemaphore tls_sem;
102+
extern APTR tls_sem;
103103
extern TLSKey tlskeys[PTHREAD_KEYS_MAX];
104104
extern APTR timerMutex;
105105
extern struct TimeRequest *timedTimerIO;

library/pthread/pthread.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ void __attribute__((constructor, used)) __pthread_init();
4949
void __attribute__((destructor, used)) __pthread_exit();
5050

5151
ThreadInfo threads[PTHREAD_THREADS_MAX];
52-
struct SignalSemaphore thread_sem;
52+
APTR thread_sem = NULL;
5353
TLSKey tlskeys[PTHREAD_KEYS_MAX];
54-
struct SignalSemaphore tls_sem;
54+
APTR tls_sem = NULL;
5555
APTR timerMutex = NULL;
5656
struct TimeRequest *timedTimerIO = NULL;
5757
struct MsgPort *timedTimerPort = NULL;
@@ -269,10 +269,11 @@ static void set_tls_register(ThreadInfo *ti) {
269269

270270
int __pthread_init_func(void) {
271271
pthread_t i;
272+
SHOWMSG("[__pthread_init_func :] Pthread __pthread_init_func called.\n");
272273

273274
memset(&threads, 0, sizeof(threads));
274-
InitSemaphore(&thread_sem);
275-
InitSemaphore(&tls_sem);
275+
thread_sem = AllocSysObjectTags(ASOT_MUTEX, ASOMUTEX_Recursive, TRUE, TAG_DONE);
276+
tls_sem = AllocSysObjectTags(ASOT_MUTEX, ASOMUTEX_Recursive, TRUE, TAG_DONE);
276277

277278
// reserve ID 0 for the main thread
278279
ThreadInfo *inf = &threads[0];
@@ -306,7 +307,16 @@ void __pthread_exit_func(void) {
306307
pthread_t i;
307308
ThreadInfo *inf;
308309
struct DOSIFace *IDOS = _IDOS;
310+
SHOWMSG("[__pthread_exit_func :] Pthread __pthread_exit_func called.\n");
309311

312+
if (thread_sem) {
313+
FreeSysObject(ASOT_MUTEX, thread_sem);
314+
thread_sem = NULL;
315+
}
316+
if (tls_sem) {
317+
FreeSysObject(ASOT_MUTEX, tls_sem);
318+
tls_sem = NULL;
319+
}
310320
if (timerMutex) {
311321
FreeSysObject(ASOT_MUTEX, timerMutex);
312322
timerMutex = NULL;
@@ -340,7 +350,7 @@ void __pthread_exit_func(void) {
340350

341351
PTHREAD_CONSTRUCTOR(__pthread_init) {
342352
ENTER();
343-
DebugPrintF("[__pthread_init :] Pthread constructor called.\n");
353+
SHOWMSG("[__pthread_init :] Pthread constructor called.\n");
344354
_DOSBase = OpenLibrary("dos.library", MIN_OS_VERSION);
345355
if (_DOSBase) {
346356
_IDOS = (struct DOSIFace *) GetInterface((struct Library *) _DOSBase, "main", 1, NULL);
@@ -356,6 +366,7 @@ PTHREAD_CONSTRUCTOR(__pthread_init) {
356366

357367
PTHREAD_DESTRUCTOR(__pthread_exit) {
358368
ENTER();
369+
SHOWMSG("[__pthread_exit :] Pthread destructor called.\n");
359370
if (_DOSBase != NULL) {
360371
CloseLibrary(_DOSBase);
361372
_DOSBase = NULL;

library/pthread/pthread_cleanup_push.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pthread_cleanup_push(void (*routine)(void *), void *arg) {
5050
return;
5151

5252
inf = GetCurrentThreadInfo();
53+
if (!inf)
54+
return;
5355

5456
handler->routine = routine;
5557
handler->arg = arg;

library/pthread/pthread_create.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
extern struct DOSIFace *_IDOS;
4141

42-
static inline void set_tls_register(ThreadInfo *ti) {
42+
static void set_tls_register(ThreadInfo *ti) {
4343
__asm__ volatile("mr r2, %0" :: "r"(ti));
4444
}
4545

@@ -80,7 +80,7 @@ StarterFunc() {
8080

8181
// destroy all non-NULL TLS key values
8282
// since the destructors can set the keys themselves, we have to do multiple iterations
83-
ObtainSemaphoreShared(&tls_sem);
83+
MutexObtain(tls_sem);
8484
for (int j = 0; keyFound && j < PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
8585
keyFound = FALSE;
8686
for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
@@ -92,7 +92,7 @@ StarterFunc() {
9292
}
9393
}
9494
}
95-
ReleaseSemaphore(&tls_sem);
95+
MutexRelease(tls_sem);
9696

9797
if (stackSwapped)
9898
StackSwap(&stack);
@@ -104,9 +104,9 @@ StarterFunc() {
104104
Signal((struct Task *) inf->parent, SIGF_PARENT);
105105
} else {
106106
// no one is waiting for us, do the clean up
107-
ObtainSemaphore(&thread_sem);
107+
MutexObtain(thread_sem);
108108
_pthread_clear_threadinfo(inf);
109-
ReleaseSemaphore(&thread_sem);
109+
MutexRelease(thread_sem);
110110
}
111111

112112
return RETURN_OK;
@@ -125,12 +125,12 @@ pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(voi
125125
return EINVAL;
126126

127127
// grab an empty thread slot
128-
ObtainSemaphore(&thread_sem);
128+
MutexObtain(thread_sem);
129129
threadnew = GetThreadId(NULL);
130-
ReleaseSemaphore(&thread_sem);
130+
MutexRelease(thread_sem);
131131

132132
if (threadnew == PTHREAD_THREADS_MAX) {
133-
ReleaseSemaphore(&thread_sem);
133+
MutexRelease(thread_sem);
134134
return EAGAIN;
135135
}
136136

library/pthread/pthread_join.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ pthread_join(pthread_t thread, void **value_ptr) {
6161
if (value_ptr)
6262
*value_ptr = inf->ret;
6363

64-
ObtainSemaphore(&thread_sem);
64+
MutexObtain(thread_sem);
6565
_pthread_clear_threadinfo(inf);
66-
ReleaseSemaphore(&thread_sem);
66+
MutexRelease(thread_sem);
6767

6868
return 0;
6969
}

library/pthread/pthread_key_create.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ pthread_key_create(pthread_key_t *key, void (*destructor)(void *)) {
4545
if (key == NULL)
4646
return EINVAL;
4747

48-
ObtainSemaphore(&tls_sem);
48+
MutexObtain(tls_sem);
4949

5050
for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
5151
if (tlskeys[i].used == FALSE)
5252
break;
5353
}
5454

5555
if (i == PTHREAD_KEYS_MAX) {
56-
ReleaseSemaphore(&tls_sem);
56+
MutexRelease(tls_sem);
5757
return EAGAIN;
5858
}
5959

6060
tls = &tlskeys[i];
6161
tls->used = TRUE;
6262
tls->destructor = destructor;
6363

64-
ReleaseSemaphore(&tls_sem);
64+
MutexRelease(tls_sem);
6565

6666
*key = i;
6767

library/pthread/pthread_key_delete.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ pthread_key_delete(pthread_key_t key) {
4646

4747
tls = &tlskeys[key];
4848

49-
ObtainSemaphore(&tls_sem);
49+
MutexObtain(tls_sem);
5050

5151
if (tls->used == FALSE) {
52-
ReleaseSemaphore(&tls_sem);
52+
MutexRelease(tls_sem);
5353
return EINVAL;
5454
}
5555

5656
tls->used = FALSE;
5757
tls->destructor = NULL;
5858

59-
ReleaseSemaphore(&tls_sem);
59+
MutexRelease(tls_sem);
6060

6161
return 0;
6262
}

0 commit comments

Comments
 (0)