SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Lock.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Copyright (C) 2013 Soeren Sonnenburg
8  */
9 #include <shogun/lib/config.h>
10 #include <shogun/lib/memory.h>
11 #include <shogun/lib/Lock.h>
12 
13 #ifdef HAVE_PTHREAD
14 #include <pthread.h>
15 #ifdef USE_SPINLOCKS
16 #ifdef DARWIN
17 #include <libkern/OSAtomic.h>
18  #define PTHREAD_LOCK_T OSSpinLock
19  #define PTHREAD_LOCK_INIT(lock) *lock = OS_SPINLOCK_INIT
20  #define PTHREAD_LOCK_DESTROY(lock)
21  #define PTHREAD_LOCK(lock) OSSpinLockLock(lock)
22  #define PTHREAD_UNLOCK(lock) OSSpinLockUnlock(lock)
23 #else
24  #define PTHREAD_LOCK_T pthread_spinlock_t
25  #define PTHREAD_LOCK_INIT(lock) pthread_spin_init(lock, 0)
26  #define PTHREAD_LOCK_DESTROY(lock) pthread_spin_destroy(lock)
27  #define PTHREAD_LOCK(lock) pthread_spin_lock(lock)
28  #define PTHREAD_UNLOCK(lock) pthread_spin_unlock(lock)
29 #endif
30 #else
31  #define PTHREAD_LOCK_T pthread_mutex_t
32  #define PTHREAD_LOCK_INIT(lock) pthread_mutex_init(lock, NULL)
33  #define PTHREAD_LOCK_DESTROY(lock) pthread_mutex_destroy(lock)
34  #define PTHREAD_LOCK(lock) pthread_mutex_lock(lock)
35  #define PTHREAD_UNLOCK(lock) pthread_mutex_unlock(lock)
36 #endif
37 #endif
38 
39 using namespace shogun;
40 
42 {
43 #ifdef HAVE_PTHREAD
44  lock_object=(void*) SG_MALLOC(PTHREAD_LOCK_T, 1);
45  PTHREAD_LOCK_INIT((PTHREAD_LOCK_T*) lock_object);
46 #endif
47 }
48 
50 {
51 #ifdef HAVE_PTHREAD
52  PTHREAD_LOCK_DESTROY((PTHREAD_LOCK_T*) lock_object);
53  SG_FREE(lock_object);
54 #endif
55 }
56 
58 {
59 #ifdef HAVE_PTHREAD
60  PTHREAD_LOCK((PTHREAD_LOCK_T*) lock_object);
61 #endif
62 }
63 
65 {
66 #ifdef HAVE_PTHREAD
67  PTHREAD_UNLOCK((PTHREAD_LOCK_T*) lock_object);
68 #endif
69 }
void unlock()
Definition: Lock.cpp:64
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void lock()
Definition: Lock.cpp:57

SHOGUN Machine Learning Toolbox - Documentation