Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2001 - 2007 Pekka Riikonen
+ Copyright (C) 2001 - 2008 Pekka Riikonen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
GNU General Public License for more details.
*/
-/* $Id$ */
-#include "silc.h"
+#include "silcruntime.h"
/**************************** SILC Thread API *******************************/
c->context = context;
if (pthread_attr_init(&attr)) {
- SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
silc_free(c);
return NULL;
}
if (pthread_attr_setdetachstate(&attr,
waitable ? PTHREAD_CREATE_JOINABLE :
PTHREAD_CREATE_DETACHED)) {
- SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
pthread_attr_destroy(&attr);
silc_free(c);
return NULL;
ret = pthread_create(&thread, &attr, silc_thread_start, c);
if (ret) {
- SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
pthread_attr_destroy(&attr);
silc_free(c);
return NULL;
*mutex = silc_calloc(1, sizeof(**mutex));
if (*mutex == NULL)
return FALSE;
- pthread_mutex_init(&(*mutex)->mutex, NULL);
+ if (pthread_mutex_init(&(*mutex)->mutex, NULL)) {
+ silc_set_errno_posix(errno);
+ silc_free(*mutex);
+ return FALSE;
+ }
(*mutex)->locked = FALSE;
return TRUE;
#else
*rwlock = silc_calloc(1, sizeof(**rwlock));
if (*rwlock == NULL)
return FALSE;
- pthread_rwlock_init(&(*rwlock)->rwlock, NULL);
+ if (pthread_rwlock_init(&(*rwlock)->rwlock, NULL)) {
+ silc_set_errno_posix(errno);
+ silc_free(*rwlock);
+ return FALSE;
+ }
return TRUE;
#else
return FALSE;
*cond = silc_calloc(1, sizeof(**cond));
if (*cond == NULL)
return FALSE;
- pthread_cond_init(&(*cond)->cond, NULL);
+ if (pthread_cond_init(&(*cond)->cond, NULL)) {
+ silc_set_errno_posix(errno);
+ silc_free(*cond);
+ return FALSE;
+ }
return TRUE;
#else
return FALSE;
#if (defined(SILC_THREADS) && defined(HAVE_PTHREAD_KEY_CREATE) && \
defined(HAVE_PTHREAD_ONCE))
+static SilcBool key_set = FALSE;
static pthread_key_t key;
-static pthread_once_t key_once;
+static pthread_once_t key_once = PTHREAD_ONCE_INIT;
static void silc_thread_tls_destructor(void *context)
{
{
if (pthread_key_create(&key, silc_thread_tls_destructor))
SILC_LOG_ERROR(("Error creating Thread-local storage"));
+ key_set = TRUE;
}
SilcTls silc_thread_tls_init(void)
SilcTls silc_thread_get_tls(void)
{
+ if (!key_set)
+ return NULL;
return pthread_getspecific(key);
}