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