17 #import "objfw-defs.h"
19 #if !defined(OF_HAVE_THREADS) || \
20 (!defined(OF_HAVE_PTHREADS) && !defined(_WIN32))
21 # error No threads available!
28 #if defined(OF_HAVE_PTHREADS)
31 typedef pthread_t of_thread_t;
32 typedef pthread_key_t of_tlskey_t;
33 typedef pthread_mutex_t of_mutex_t;
34 typedef pthread_cond_t of_condition_t;
35 typedef pthread_once_t of_once_t;
36 # define OF_ONCE_INIT PTHREAD_ONCE_INIT
43 # ifdef OF_HAVE_SOCKETS
44 # include <winsock2.h>
47 typedef HANDLE of_thread_t;
48 typedef DWORD of_tlskey_t;
49 typedef CRITICAL_SECTION of_mutex_t;
54 typedef volatile LONG of_once_t;
55 # define OF_ONCE_INIT 0
57 # error No threads available!
60 #if defined(OF_HAVE_ATOMIC_OPS)
62 typedef volatile int of_spinlock_t;
63 # define OF_SPINCOUNT 10
64 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
65 typedef pthread_spinlock_t of_spinlock_t;
67 typedef of_mutex_t of_spinlock_t;
70 #ifdef OF_HAVE_SCHED_YIELD
74 #if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(_WIN32)
75 # define of_rmutex_t of_mutex_t
83 typedef struct of_thread_attr_t {
88 #if defined(OF_HAVE_PTHREADS)
89 # define of_thread_is_current(t) pthread_equal(t, pthread_self())
90 # define of_thread_current pthread_self
92 # define of_thread_is_current(t) (t == GetCurrentThread())
93 # define of_thread_current GetCurrentThread
95 # error of_thread_is_current not implemented!
96 # error of_thread_current not implemented!
99 extern bool of_thread_attr_init(of_thread_attr_t *attr);
100 extern bool of_thread_new(of_thread_t *thread,
void (*
function)(
id),
id object,
101 const of_thread_attr_t *attr);
102 extern void of_thread_set_name(of_thread_t thread,
const char *name);
103 extern bool of_thread_join(of_thread_t thread);
104 extern bool of_thread_detach(of_thread_t thread);
105 extern void OF_NO_RETURN_FUNC of_thread_exit(
void);
106 extern void of_once(of_once_t *control,
void (*func)(
void));
107 extern bool of_mutex_new(of_mutex_t *mutex);
108 extern bool of_mutex_lock(of_mutex_t *mutex);
109 extern bool of_mutex_trylock(of_mutex_t *mutex);
110 extern bool of_mutex_unlock(of_mutex_t *mutex);
111 extern bool of_mutex_free(of_mutex_t *mutex);
112 extern bool of_rmutex_new(of_rmutex_t *rmutex);
113 extern bool of_rmutex_lock(of_rmutex_t *rmutex);
114 extern bool of_rmutex_trylock(of_rmutex_t *rmutex);
115 extern bool of_rmutex_unlock(of_rmutex_t *rmutex);
116 extern bool of_rmutex_free(of_rmutex_t *rmutex);
117 extern bool of_condition_new(of_condition_t *condition);
118 extern bool of_condition_signal(of_condition_t *condition);
119 extern bool of_condition_broadcast(of_condition_t *condition);
120 extern bool of_condition_wait(of_condition_t *condition, of_mutex_t *mutex);
121 extern bool of_condition_timed_wait(of_condition_t *condition,
123 extern bool of_condition_free(of_condition_t *condition);
127 static OF_INLINE
bool
128 of_tlskey_new(of_tlskey_t *key)
130 #if defined(OF_HAVE_PTHREADS)
131 return !pthread_key_create(key, NULL);
132 #elif defined(_WIN32)
133 return ((*key = TlsAlloc()) != TLS_OUT_OF_INDEXES);
135 # error of_tlskey_new not implemented!
139 static OF_INLINE
void*
140 of_tlskey_get(of_tlskey_t key)
142 #if defined(OF_HAVE_PTHREADS)
143 return pthread_getspecific(key);
144 #elif defined(_WIN32)
145 return TlsGetValue(key);
147 # error of_tlskey_get not implemented!
151 static OF_INLINE
bool
152 of_tlskey_set(of_tlskey_t key,
void *ptr)
154 #if defined(OF_HAVE_PTHREADS)
155 return !pthread_setspecific(key, ptr);
156 #elif defined(_WIN32)
157 return TlsSetValue(key, ptr);
159 # error of_tlskey_set not implemented!
163 static OF_INLINE
bool
164 of_tlskey_free(of_tlskey_t key)
166 #if defined(OF_HAVE_PTHREADS)
167 return !pthread_key_delete(key);
168 #elif defined(_WIN32)
171 # error of_tlskey_free not implemented!
175 static OF_INLINE
bool
176 of_spinlock_new(of_spinlock_t *spinlock)
178 #if defined(OF_HAVE_ATOMIC_OPS)
181 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
182 return !pthread_spin_init(spinlock, 0);
184 return of_mutex_new(spinlock);
188 static OF_INLINE
bool
189 of_spinlock_trylock(of_spinlock_t *spinlock)
191 #if defined(OF_HAVE_ATOMIC_OPS)
192 return of_atomic_int_cmpswap(spinlock, 0, 1);
193 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
194 return !pthread_spin_trylock(spinlock);
196 return of_mutex_trylock(spinlock);
200 static OF_INLINE
bool
201 of_spinlock_lock(of_spinlock_t *spinlock)
203 #if defined(OF_HAVE_ATOMIC_OPS)
204 # if defined(OF_HAVE_SCHED_YIELD) || defined(_WIN32)
207 for (i = 0; i < OF_SPINCOUNT; i++)
208 if (of_spinlock_trylock(spinlock))
211 while (!of_spinlock_trylock(spinlock))
218 while (!of_spinlock_trylock(spinlock));
222 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
223 return !pthread_spin_lock(spinlock);
225 return of_mutex_lock(spinlock);
229 static OF_INLINE
bool
230 of_spinlock_unlock(of_spinlock_t *spinlock)
232 #if defined(OF_HAVE_ATOMIC_OPS)
233 return of_atomic_int_cmpswap(spinlock, 1, 0);
234 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
235 return !pthread_spin_unlock(spinlock);
237 return of_mutex_unlock(spinlock);
241 static OF_INLINE
bool
242 of_spinlock_free(of_spinlock_t *spinlock)
244 #if defined(OF_HAVE_ATOMIC_OPS)
246 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
247 return !pthread_spin_destroy(spinlock);
249 return of_mutex_free(spinlock);
double of_time_interval_t
A time interval in seconds.
Definition: OFObject.h:90