SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Copyright (C) 2013 Soeren Sonnenburg 00008 */ 00009 #include <shogun/lib/config.h> 00010 #include <shogun/lib/memory.h> 00011 #include <shogun/lib/Lock.h> 00012 00013 #ifdef HAVE_PTHREAD 00014 #include <pthread.h> 00015 #ifdef USE_SPINLOCKS 00016 #ifdef DARWIN 00017 #include <libkern/OSAtomic.h> 00018 #define PTHREAD_LOCK_T OSSpinLock 00019 #define PTHREAD_LOCK_INIT(lock) *lock = OS_SPINLOCK_INIT 00020 #define PTHREAD_LOCK_DESTROY(lock) 00021 #define PTHREAD_LOCK(lock) OSSpinLockLock(lock) 00022 #define PTHREAD_UNLOCK(lock) OSSpinLockUnlock(lock) 00023 #else 00024 #define PTHREAD_LOCK_T pthread_spinlock_t 00025 #define PTHREAD_LOCK_INIT(lock) pthread_spin_init(lock, 0) 00026 #define PTHREAD_LOCK_DESTROY(lock) pthread_spin_destroy(lock) 00027 #define PTHREAD_LOCK(lock) pthread_spin_lock(lock) 00028 #define PTHREAD_UNLOCK(lock) pthread_spin_unlock(lock) 00029 #endif 00030 #else 00031 #define PTHREAD_LOCK_T pthread_mutex_t 00032 #define PTHREAD_LOCK_INIT(lock) pthread_mutex_init(lock, NULL) 00033 #define PTHREAD_LOCK_DESTROY(lock) pthread_mutex_destroy(lock) 00034 #define PTHREAD_LOCK(lock) pthread_mutex_lock(lock) 00035 #define PTHREAD_UNLOCK(lock) pthread_mutex_unlock(lock) 00036 #endif 00037 #endif 00038 00039 using namespace shogun; 00040 00041 CLock::CLock() 00042 { 00043 #ifdef HAVE_PTHREAD 00044 lock_object=(void*) SG_MALLOC(PTHREAD_LOCK_T, 1); 00045 PTHREAD_LOCK_INIT((PTHREAD_LOCK_T*) lock_object); 00046 #endif 00047 } 00048 00049 CLock::~CLock() 00050 { 00051 #ifdef HAVE_PTHREAD 00052 PTHREAD_LOCK_DESTROY((PTHREAD_LOCK_T*) lock_object); 00053 SG_FREE(lock_object); 00054 #endif 00055 } 00056 00057 void CLock::lock() 00058 { 00059 #ifdef HAVE_PTHREAD 00060 PTHREAD_LOCK((PTHREAD_LOCK_T*) lock_object); 00061 #endif 00062 } 00063 00064 void CLock::unlock() 00065 { 00066 #ifdef HAVE_PTHREAD 00067 PTHREAD_UNLOCK((PTHREAD_LOCK_T*) lock_object); 00068 #endif 00069 }