1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 12 #ifndef VP8_COMMON_THREADING_H_ 13 #define VP8_COMMON_THREADING_H_ 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 #if CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD 20 21 /* Thread management macros */ 22 #ifdef _WIN32 23 /* Win32 */ 24 #include <process.h> 25 #include <windows.h> 26 #define THREAD_FUNCTION DWORD WINAPI 27 #define THREAD_FUNCTION_RETURN DWORD 28 #define THREAD_SPECIFIC_INDEX DWORD 29 #define pthread_t HANDLE 30 #define pthread_attr_t DWORD 31 #define pthread_create(thhandle,attr,thfunc,tharg) (int)((*thhandle=(HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))thfunc,tharg,0,NULL))==NULL) 32 #define pthread_join(thread, result) ((WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread)) 33 #define pthread_detach(thread) if(thread!=NULL)CloseHandle(thread) 34 #define thread_sleep(nms) Sleep(nms) 35 #define pthread_cancel(thread) terminate_thread(thread,0) 36 #define ts_key_create(ts_key, destructor) {ts_key = TlsAlloc();}; 37 #define pthread_getspecific(ts_key) TlsGetValue(ts_key) 38 #define pthread_setspecific(ts_key, value) TlsSetValue(ts_key, (void *)value) 39 #define pthread_self() GetCurrentThreadId() 40 41 #elif defined(__OS2__) 42 /* OS/2 */ 43 #define INCL_DOS 44 #include <os2.h> 45 46 #include <stdlib.h> 47 #define THREAD_FUNCTION void 48 #define THREAD_FUNCTION_RETURN void 49 #define THREAD_SPECIFIC_INDEX PULONG 50 #define pthread_t TID 51 #define pthread_attr_t ULONG 52 #define pthread_create(thhandle,attr,thfunc,tharg) \ 53 ((int)((*(thhandle)=_beginthread(thfunc,NULL,1024*1024,tharg))==-1)) 54 #define pthread_join(thread, result) ((int)DosWaitThread(&(thread),0)) 55 #define pthread_detach(thread) 0 56 #define thread_sleep(nms) DosSleep(nms) 57 #define pthread_cancel(thread) DosKillThread(thread) 58 #define ts_key_create(ts_key, destructor) \ 59 DosAllocThreadLocalMemory(1, &(ts_key)); 60 #define pthread_getspecific(ts_key) ((void *)(*(ts_key))) 61 #define pthread_setspecific(ts_key, value) (*(ts_key)=(ULONG)(value)) 62 #define pthread_self() _gettid() 63 #else 64 #ifdef __APPLE__ 65 #include <mach/mach_init.h> 66 #include <mach/semaphore.h> 67 #include <mach/task.h> 68 #include <time.h> 69 #include <unistd.h> 70 71 #else 72 #include <semaphore.h> 73 #endif 74 75 #include <pthread.h> 76 /* pthreads */ 77 /* Nearly everything is already defined */ 78 #define THREAD_FUNCTION void * 79 #define THREAD_FUNCTION_RETURN void * 80 #define THREAD_SPECIFIC_INDEX pthread_key_t 81 #define ts_key_create(ts_key, destructor) pthread_key_create (&(ts_key), destructor); 82 #endif 83 84 /* Syncrhronization macros: Win32 and Pthreads */ 85 #ifdef _WIN32 86 #define sem_t HANDLE 87 #define pause(voidpara) __asm PAUSE 88 #define sem_init(sem, sem_attr1, sem_init_value) (int)((*sem = CreateSemaphore(NULL,0,32768,NULL))==NULL) 89 #define sem_wait(sem) (int)(WAIT_OBJECT_0 != WaitForSingleObject(*sem,INFINITE)) 90 #define sem_post(sem) ReleaseSemaphore(*sem,1,NULL) 91 #define sem_destroy(sem) if(*sem)((int)(CloseHandle(*sem))==TRUE) 92 #define thread_sleep(nms) Sleep(nms) 93 94 #elif defined(__OS2__) 95 typedef struct 96 { 97 HEV event; 98 HMTX wait_mutex; 99 HMTX count_mutex; 100 int count; 101 } sem_t; 102 103 static inline int sem_init(sem_t *sem, int pshared, unsigned int value) 104 { 105 DosCreateEventSem(NULL, &sem->event, pshared ? DC_SEM_SHARED : 0, 106 value > 0 ? TRUE : FALSE); 107 DosCreateMutexSem(NULL, &sem->wait_mutex, 0, FALSE); 108 DosCreateMutexSem(NULL, &sem->count_mutex, 0, FALSE); 109 110 sem->count = value; 111 112 return 0; 113 } 114 115 static inline int sem_wait(sem_t * sem) 116 { 117 DosRequestMutexSem(sem->wait_mutex, -1); 118 119 DosWaitEventSem(sem->event, -1); 120 121 DosRequestMutexSem(sem->count_mutex, -1); 122 123 sem->count--; 124 if (sem->count == 0) 125 { 126 ULONG post_count; 127 128 DosResetEventSem(sem->event, &post_count); 129 } 130 131 DosReleaseMutexSem(sem->count_mutex); 132 133 DosReleaseMutexSem(sem->wait_mutex); 134 135 return 0; 136 } 137 138 static inline int sem_post(sem_t * sem) 139 { 140 DosRequestMutexSem(sem->count_mutex, -1); 141 142 if (sem->count < 32768) 143 { 144 sem->count++; 145 DosPostEventSem(sem->event); 146 } 147 148 DosReleaseMutexSem(sem->count_mutex); 149 150 return 0; 151 } 152 153 static inline int sem_destroy(sem_t * sem) 154 { 155 DosCloseEventSem(sem->event); 156 DosCloseMutexSem(sem->wait_mutex); 157 DosCloseMutexSem(sem->count_mutex); 158 159 return 0; 160 } 161 162 #define thread_sleep(nms) DosSleep(nms) 163 164 #else 165 166 #ifdef __APPLE__ 167 #define sem_t semaphore_t 168 #define sem_init(X,Y,Z) semaphore_create(mach_task_self(), X, SYNC_POLICY_FIFO, Z) 169 #define sem_wait(sem) (semaphore_wait(*sem) ) 170 #define sem_post(sem) semaphore_signal(*sem) 171 #define sem_destroy(sem) semaphore_destroy(mach_task_self(),*sem) 172 #define thread_sleep(nms) /* { struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */ 173 #else 174 #include <unistd.h> 175 #include <sched.h> 176 #define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */ 177 #endif 178 /* Not Windows. Assume pthreads */ 179 180 #endif 181 182 #if ARCH_X86_32 || ARCH_X86_64 183 #include "vpx_ports/x86.h" 184 #else 185 #define x86_pause_hint() 186 #endif 187 188 #endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */ 189 190 #ifdef __cplusplus 191 } // extern "C" 192 #endif 193 194 #endif // VP8_COMMON_THREADING_H_ 195