Index: src/tlskey.m ================================================================== --- src/tlskey.m +++ src/tlskey.m @@ -29,10 +29,11 @@ */ # import "runtime/private.h" static of_tlskey_t firstKey = NULL, lastKey = NULL; static struct SignalSemaphore semaphore; +static bool semaphoreInitialized = false; static uint32_t hashFunc(const void *ptr) { return (uint32_t)(uintptr_t)ptr; @@ -44,11 +45,14 @@ return (ptr1 == ptr2); } OF_CONSTRUCTOR() { - InitSemaphore(&semaphore); + if (!semaphoreInitialized) { + InitSemaphore(&semaphore); + semaphoreInitialized = true; + } } #endif bool of_tlskey_new(of_tlskey_t *key) @@ -56,10 +60,20 @@ #if defined(OF_HAVE_PTHREADS) return (pthread_key_create(key, NULL) == 0); #elif defined(OF_WINDOWS) return ((*key = TlsAlloc()) != TLS_OUT_OF_INDEXES); #elif defined(OF_AMIGAOS) + if (!semaphoreInitialized) { + /* + * We might be called from another constructor, while ours has + * not run yet. This is safe, as the constructor is definitely + * run before a thread is spawned. + */ + InitSemaphore(&semaphore); + semaphoreInitialized = true; + } + if ((*key = malloc(sizeof(**key))) == NULL) return false; (*key)->table = NULL;