X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Funix%2Fsilcunixthread.c;h=2415a380e31eb3bb21509ff4be74b9c58ce07d2b;hb=0d56eecc0a4e39d1a0b1695a576ab2a51fb87470;hp=3985a6174fe26f5fd37397d45705a2f7a52b35c5;hpb=a894395e0be871e9919cb2f9070e2baa2258cb47;p=crypto.git diff --git a/lib/silcutil/unix/silcunixthread.c b/lib/silcutil/unix/silcunixthread.c index 3985a617..2415a380 100644 --- a/lib/silcutil/unix/silcunixthread.c +++ b/lib/silcutil/unix/silcunixthread.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2001 - 2006 Pekka Riikonen + Copyright (C) 2001 - 2007 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 @@ -97,6 +97,14 @@ SilcBool silc_thread_wait(SilcThread thread, void **exit_value) #endif } +void silc_thread_yield(void) +{ +#ifdef SILC_THREADS +#ifdef HAVE_SCHED_YIELD + sched_yield(); +#endif /* HAVE_SCHED_YIELD */ +#endif /* SILC_THREADS */ +} /***************************** SILC Mutex API *******************************/ @@ -136,8 +144,7 @@ void silc_mutex_lock(SilcMutex mutex) { #ifdef SILC_THREADS if (mutex) { - if (pthread_mutex_lock(&mutex->mutex)) - SILC_ASSERT(FALSE); + SILC_VERIFY(pthread_mutex_lock(&mutex->mutex) == 0); mutex->locked = TRUE; } #endif /* SILC_THREADS */ @@ -147,9 +154,8 @@ void silc_mutex_unlock(SilcMutex mutex) { #ifdef SILC_THREADS if (mutex) { - if (pthread_mutex_unlock(&mutex->mutex)) - SILC_ASSERT(FALSE); mutex->locked = FALSE; + SILC_VERIFY(pthread_mutex_unlock(&mutex->mutex) == 0); } #endif /* SILC_THREADS */ } @@ -158,12 +164,69 @@ void silc_mutex_assert_locked(SilcMutex mutex) { #ifdef SILC_THREADS if (mutex) - SILC_ASSERT(mutex->locked); + SILC_VERIFY(mutex->locked); +#endif /* SILC_THREADS */ +} + +/***************************** SILC Rwlock API ******************************/ + +/* SILC read/write lock structure */ +struct SilcRwLockStruct { +#ifdef SILC_THREADS + pthread_rwlock_t rwlock; +#else + void *tmp; +#endif /* SILC_THREADS */ +}; + +SilcBool silc_rwlock_alloc(SilcRwLock *rwlock) +{ +#ifdef SILC_THREADS + *rwlock = silc_calloc(1, sizeof(**rwlock)); + if (*rwlock == NULL) + return FALSE; + pthread_rwlock_init(&(*rwlock)->rwlock, NULL); + return TRUE; +#else + return FALSE; #endif /* SILC_THREADS */ } +void silc_rwlock_free(SilcRwLock rwlock) +{ +#ifdef SILC_THREADS + if (rwlock) { + pthread_rwlock_destroy(&rwlock->rwlock); + silc_free(rwlock); + } +#endif /* SILC_THREADS */ +} + +void silc_rwlock_rdlock(SilcRwLock rwlock) +{ +#ifdef SILC_THREADS + if (rwlock) + pthread_rwlock_rdlock(&rwlock->rwlock); +#endif /* SILC_THREADS */ +} + +void silc_rwlock_wrlock(SilcRwLock rwlock) +{ +#ifdef SILC_THREADS + if (rwlock) + SILC_VERIFY(pthread_rwlock_wrlock(&rwlock->rwlock) == 0); +#endif /* SILC_THREADS */ +} + +void silc_rwlock_unlock(SilcRwLock rwlock) +{ +#ifdef SILC_THREADS + if (rwlock) + SILC_VERIFY(pthread_rwlock_unlock(&rwlock->rwlock) == 0); +#endif /* SILC_THREADS */ +} -/**************************** SILC Cond API ******************************/ +/****************************** SILC Cond API *******************************/ /* SILC Conditional Variable context */ struct SilcCondStruct {