00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #import "OFMacros.h"
00013
00014 #ifndef _WIN32
00015 #include <pthread.h>
00016 typedef pthread_t of_thread_t;
00017 typedef pthread_mutex_t of_mutex_t;
00018 typedef pthread_key_t of_tlskey_t;
00019 #else
00020 #include <windows.h>
00021 typedef HANDLE of_thread_t;
00022 typedef CRITICAL_SECTION of_mutex_t;
00023 typedef DWORD of_tlskey_t;
00024 #endif
00025
00026 #ifndef _WIN32
00027 # define of_thread_is_current(t) pthread_equal(t, pthread_self())
00028 # define of_thread_current() pthread_self()
00029 #else
00030 # define of_thread_is_current(t) (t == GetCurrentThread())
00031 # define of_thread_current() GetCurrentThread()
00032 #endif
00033
00034 static OF_INLINE BOOL
00035 of_thread_new(of_thread_t *thread, id (*main)(id), id data)
00036 {
00037 #ifndef _WIN32
00038 return (pthread_create(thread, NULL, (void*(*)(void*))main,
00039 (void*)data) ? NO : YES);
00040 #else
00041 *thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main,
00042 (void*)data, 0, NULL);
00043
00044 return (thread == NULL ? NO : YES);
00045 #endif
00046 }
00047
00048 static OF_INLINE BOOL
00049 of_thread_join(of_thread_t thread)
00050 {
00051 #ifndef _WIN32
00052 void *ret;
00053
00054 if (pthread_join(thread, &ret))
00055 return NO;
00056
00057 return (ret != PTHREAD_CANCELED ? YES : NO);
00058 #else
00059 if (WaitForSingleObject(thread, INFINITE))
00060 return NO;
00061
00062 CloseHandle(thread);
00063
00064 return YES;
00065 #endif
00066 }
00067
00068 static OF_INLINE BOOL
00069 of_thread_cancel(of_thread_t thread)
00070 {
00071 #ifndef _WIN32
00072 return (pthread_cancel(thread) ? NO : YES);
00073 #else
00074 if (thread != INVALID_HANDLE_VALUE) {
00075 TerminateThread(thread, 1);
00076 CloseHandle(thread);
00077 }
00078
00079 return YES;
00080 #endif
00081 }
00082
00083 static OF_INLINE BOOL
00084 of_mutex_new(of_mutex_t *mutex)
00085 {
00086 #ifndef _WIN32
00087 return (pthread_mutex_init(mutex, NULL) ? NO : YES);
00088 #else
00089 InitializeCriticalSection(mutex);
00090 return YES;
00091 #endif
00092 }
00093
00094 static OF_INLINE BOOL
00095 of_mutex_free(of_mutex_t *mutex)
00096 {
00097 #ifndef _WIN32
00098 return (pthread_mutex_destroy(mutex) ? NO : YES);
00099 #else
00100 DeleteCriticalSection(mutex);
00101 return YES;
00102 #endif
00103 }
00104
00105 static OF_INLINE BOOL
00106 of_mutex_lock(of_mutex_t *mutex)
00107 {
00108 #ifndef _WIN32
00109 return (pthread_mutex_lock(mutex) ? NO : YES);
00110 #else
00111 EnterCriticalSection(mutex);
00112 return YES;
00113 #endif
00114 }
00115
00116 static OF_INLINE BOOL
00117 of_mutex_unlock(of_mutex_t *mutex)
00118 {
00119 #ifndef _WIN32
00120 return (pthread_mutex_unlock(mutex) ? NO : YES);
00121 #else
00122 LeaveCriticalSection(mutex);
00123 return YES;
00124 #endif
00125 }
00126
00127 static OF_INLINE BOOL
00128 of_tlskey_new(of_tlskey_t *key)
00129 {
00130 #ifndef _WIN32
00131 return (pthread_key_create(key, NULL) ? NO : YES);
00132 #else
00133 return ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES ? NO : YES);
00134 #endif
00135 }
00136
00137 static OF_INLINE id
00138 of_tlskey_get(of_tlskey_t key)
00139 {
00140 #ifndef _WIN32
00141 void *ret = pthread_getspecific(key);
00142 #else
00143 void *ret = TlsGetValue(key);
00144 #endif
00145
00146
00147 if (ret == NULL)
00148 return nil;
00149
00150 return (id)ret;
00151 }
00152
00153 static OF_INLINE BOOL
00154 of_tlskey_set(of_tlskey_t key, id obj)
00155 {
00156 void *p = (obj != nil ? (void*)obj : NULL);
00157
00158 #ifndef _WIN32
00159 return (pthread_setspecific(key, p) ? NO : YES);
00160 #else
00161 return (TlsSetValue(key, p) ? YES : NO);
00162 #endif
00163 }
00164
00165 static OF_INLINE BOOL
00166 of_tlskey_free(of_tlskey_t key)
00167 {
00168 #ifndef _WIN32
00169 return (pthread_key_delete(key) ? NO : YES);
00170 #else
00171 return (TlsFree(key) ? YES : NO);
00172 #endif
00173 }