12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #include "toolkit.h"
- #ifndef _WIN32
- int toolkit_mutex_init(toolkit_mutex_t* mutex)
- {
- #if defined(NDEBUG) || !defined(PTHREAD_MUTEX_ERRORCHECK)
- if (pthread_mutex_init(mutex, NULL) != 0)
- return -1;
- return 0;
- #else
- pthread_mutexattr_t attr;
- int err;
- if (pthread_mutexattr_init(&attr))
- abort();
- if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK))
- abort();
- err = pthread_mutex_init(mutex, &attr);
- if (pthread_mutexattr_destroy(&attr))
- abort();
- return err == 0 ? 0 : -1;
- #endif
- }
- int toolkit_mutex_init_recursive(toolkit_mutex_t* mutex) {
- pthread_mutexattr_t attr;
- int err;
- if (pthread_mutexattr_init(&attr))
- abort();
- if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
- abort();
- err = pthread_mutex_init(mutex, &attr);
- if (pthread_mutexattr_destroy(&attr))
- abort();
- return err == 0 ? 0 : -1;
- }
- void toolkit_mutex_destroy(toolkit_mutex_t* mutex) {
- if (pthread_mutex_destroy(mutex))
- abort();
- }
- void toolkit_mutex_lock(toolkit_mutex_t* mutex) {
- if (pthread_mutex_lock(mutex))
- abort();
- }
- int toolkit_mutex_trylock(toolkit_mutex_t* mutex) {
- int err;
- err = pthread_mutex_trylock(mutex);
- if (err) {
- if (err != EBUSY && err != EAGAIN)
- abort();
- return -1;
- }
- return 0;
- }
- void toolkit_mutex_unlock(toolkit_mutex_t* mutex) {
- if (pthread_mutex_unlock(mutex))
- abort();
- }
- void toolkit_once(toolkit_once_t* guard, void (*callback)(void)) {
- if (pthread_once(guard, callback))
- abort();
- }
- #endif //NOT _WIN32
|