00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #import "OFMacros.h"
00013
00014 #if !defined(OF_THREADS) || (!defined(OF_HAVE_PTHREADS) && !defined(_WIN32))
00015 # error No threads available!
00016 #endif
00017
00018 #if defined(OF_HAVE_PTHREADS)
00019 # include <pthread.h>
00020 typedef pthread_t of_thread_t;
00021 typedef pthread_mutex_t of_mutex_t;
00022 typedef pthread_key_t of_tlskey_t;
00023 #elif defined(_WIN32)
00024 # include <windows.h>
00025 typedef HANDLE of_thread_t;
00026 typedef CRITICAL_SECTION of_mutex_t;
00027 typedef DWORD of_tlskey_t;
00028 #endif
00029
00030 #if defined(OF_ATOMIC_OPS)
00031 # import "atomic.h"
00032 typedef int32_t of_spinlock_t;
00033 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
00034 typedef pthread_spinlock_t of_spinlock_t;
00035 #else
00036 typedef pthread_mutex_t of_spinlock_t;
00037 #endif
00038
00039 #if defined(OF_HAVE_PTHREADS)
00040 # define of_thread_is_current(t) pthread_equal(t, pthread_self())
00041 # define of_thread_current() pthread_self()
00042 #elif defined(_WIN32)
00043 # define of_thread_is_current(t) (t == GetCurrentThread())
00044 # define of_thread_current() GetCurrentThread()
00045 #endif
00046
00047 static OF_INLINE BOOL
00048 of_thread_new(of_thread_t *thread, id (*main)(id), id data)
00049 {
00050 #if defined(OF_HAVE_PTHREADS)
00051 return (pthread_create(thread, NULL, (void*(*)(void*))main,
00052 (void*)data) ? NO : YES);
00053 #elif defined(_WIN32)
00054 *thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main,
00055 (void*)data, 0, NULL);
00056
00057 return (thread == NULL ? NO : YES);
00058 #endif
00059 }
00060
00061 static OF_INLINE BOOL
00062 of_thread_join(of_thread_t thread)
00063 {
00064 #if defined(OF_HAVE_PTHREADS)
00065 void *ret;
00066
00067 if (pthread_join(thread, &ret))
00068 return NO;
00069
00070 return (ret != PTHREAD_CANCELED ? YES : NO);
00071 #elif defined(_WIN32)
00072 if (WaitForSingleObject(thread, INFINITE))
00073 return NO;
00074
00075 CloseHandle(thread);
00076
00077 return YES;
00078 #endif
00079 }
00080
00081 static OF_INLINE BOOL
00082 of_thread_cancel(of_thread_t thread)
00083 {
00084 #if defined(OF_HAVE_PTHREADS)
00085 return (pthread_cancel(thread) ? NO : YES);
00086 #elif defined(_WIN32)
00087 if (thread != INVALID_HANDLE_VALUE) {
00088 TerminateThread(thread, 1);
00089 CloseHandle(thread);
00090 }
00091
00092 return YES;
00093 #endif
00094 }
00095
00096 static OF_INLINE BOOL
00097 of_mutex_new(of_mutex_t *mutex)
00098 {
00099 #if defined(OF_HAVE_PTHREADS)
00100 return (pthread_mutex_init(mutex, NULL) ? NO : YES);
00101 #elif defined(_WIN32)
00102 InitializeCriticalSection(mutex);
00103 return YES;
00104 #endif
00105 }
00106
00107 static OF_INLINE BOOL
00108 of_mutex_free(of_mutex_t *mutex)
00109 {
00110 #if defined(OF_HAVE_PTHREADS)
00111 return (pthread_mutex_destroy(mutex) ? NO : YES);
00112 #elif defined(_WIN32)
00113 DeleteCriticalSection(mutex);
00114 return YES;
00115 #endif
00116 }
00117
00118 static OF_INLINE BOOL
00119 of_mutex_lock(of_mutex_t *mutex)
00120 {
00121 #if defined(OF_HAVE_PTHREADS)
00122 return (pthread_mutex_lock(mutex) ? NO : YES);
00123 #elif defined(_WIN32)
00124 EnterCriticalSection(mutex);
00125 return YES;
00126 #endif
00127 }
00128
00129 static OF_INLINE BOOL
00130 of_mutex_trylock(of_mutex_t *mutex)
00131 {
00132 #if defined(OF_HAVE_PTHREADS)
00133 return (pthread_mutex_trylock(mutex) ? NO : YES);
00134 #elif defined(_WIN32)
00135 return (TryEnterCriticalSection(mutex) ? YES : NO);
00136 #endif
00137 }
00138
00139 static OF_INLINE BOOL
00140 of_mutex_unlock(of_mutex_t *mutex)
00141 {
00142 #if defined(OF_HAVE_PTHREADS)
00143 return (pthread_mutex_unlock(mutex) ? NO : YES);
00144 #elif defined(_WIN32)
00145 LeaveCriticalSection(mutex);
00146 return YES;
00147 #endif
00148 }
00149
00150 static OF_INLINE BOOL
00151 of_tlskey_new(of_tlskey_t *key)
00152 {
00153 #if defined(OF_HAVE_PTHREADS)
00154 return (pthread_key_create(key, NULL) ? NO : YES);
00155 #elif defined(_WIN32)
00156 return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES);
00157 #endif
00158 }
00159
00160 static OF_INLINE id
00161 of_tlskey_get(of_tlskey_t key)
00162 {
00163 #if defined(OF_HAVE_PTHREADS)
00164 void *ret = pthread_getspecific(key);
00165 #elif defined(_WIN32)
00166 void *ret = TlsGetValue(key);
00167 #endif
00168
00169
00170 if (ret == NULL)
00171 return nil;
00172
00173 return (id)ret;
00174 }
00175
00176 static OF_INLINE BOOL
00177 of_tlskey_set(of_tlskey_t key, id obj)
00178 {
00179 void *p = (obj != nil ? (void*)obj : NULL);
00180
00181 #if defined(OF_HAVE_PTHREADS)
00182 return (pthread_setspecific(key, p) ? NO : YES);
00183 #elif defined(_WIN32)
00184 return (TlsSetValue(key, p) ? YES : NO);
00185 #endif
00186 }
00187
00188 static OF_INLINE BOOL
00189 of_tlskey_free(of_tlskey_t key)
00190 {
00191 #if defined(OF_HAVE_PTHREADS)
00192 return (pthread_key_delete(key) ? NO : YES);
00193 #elif defined(_WIN32)
00194 return (TlsFree(key) ? YES : NO);
00195 #endif
00196 }
00197
00198 static OF_INLINE BOOL
00199 of_spinlock_new(of_spinlock_t *s)
00200 {
00201 #if defined(OF_ATOMIC_OPS)
00202 *s = 0;
00203 return YES;
00204 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
00205 return (pthread_spin_init(s, 0) ? NO : YES);
00206 #else
00207 return of_mutex_new(s);
00208 #endif
00209 }
00210
00211 static OF_INLINE BOOL
00212 of_spinlock_trylock(of_spinlock_t *s)
00213 {
00214 #if defined(OF_ATOMIC_OPS)
00215 return (of_atomic_cmpswap32(s, 0, 1) ? YES : NO);
00216 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
00217 return (pthread_spin_trylock(s) ? NO : YES);
00218 #else
00219 return of_mutex_trylock(s);
00220 #endif
00221 }
00222
00223 static OF_INLINE BOOL
00224 of_spinlock_lock(of_spinlock_t *s)
00225 {
00226 #if defined(OF_ATOMIC_OPS)
00227 while (!of_spinlock_trylock(s)) {
00228 # ifdef OF_HAVE_SCHED_YIELD
00229 sched_yield();
00230 # endif
00231 }
00232
00233 return YES;
00234 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
00235 return (pthread_spin_lock(s) ? NO : YES);
00236 #else
00237 return of_mutex_lock(s);
00238 #endif
00239 }
00240
00241 static OF_INLINE BOOL
00242 of_spinlock_unlock(of_spinlock_t *s)
00243 {
00244 #if defined(OF_ATOMIC_OPS)
00245 of_atomic_and32((uint32_t*)s, 0);
00246 return YES;
00247 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
00248 return (pthread_spin_unlock(s) ? NO : YES);
00249 #else
00250 return of_mutex_unlock(s);
00251 #endif
00252 }
00253
00254 static OF_INLINE BOOL
00255 of_spinlock_free(of_spinlock_t *s)
00256 {
00257 #if defined(OF_ATOMIC_OPS)
00258 return YES;
00259 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
00260 return (pthread_spin_destroy(s) ? NO : YES);
00261 #else
00262 return of_mutex_free(s);
00263 #endif
00264 }