2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #ifndef _PTHREAD_EMULATION
13 #define _PTHREAD_EMULATION
15 #define VPXINFINITE 10000 /* 10second. */
17 #if CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD
19 /* Thread management macros */
22 #define _WIN32_WINNT 0x500 /* WINBASE.H - Enable signal_object_and_wait */
25 #define THREAD_FUNCTION DWORD WINAPI
26 #define THREAD_FUNCTION_RETURN DWORD
27 #define THREAD_SPECIFIC_INDEX DWORD
28 #define pthread_t HANDLE
29 #define pthread_attr_t DWORD
30 #define pthread_create(thhandle,attr,thfunc,tharg) (int)((*thhandle=(HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))thfunc,tharg,0,NULL))==NULL)
31 #define pthread_join(thread, result) ((WaitForSingleObject((thread),VPXINFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
32 #define pthread_detach(thread) if(thread!=NULL)CloseHandle(thread)
33 #define thread_sleep(nms) Sleep(nms)
34 #define pthread_cancel(thread) terminate_thread(thread,0)
35 #define ts_key_create(ts_key, destructor) {ts_key = TlsAlloc();};
36 #define pthread_getspecific(ts_key) TlsGetValue(ts_key)
37 #define pthread_setspecific(ts_key, value) TlsSetValue(ts_key, (void *)value)
38 #define pthread_self() GetCurrentThreadId()
41 #include <mach/mach_init.h>
42 #include <mach/semaphore.h>
43 #include <mach/task.h>
48 #include <semaphore.h>
53 /* Nearly everything is already defined */
54 #define THREAD_FUNCTION void *
55 #define THREAD_FUNCTION_RETURN void *
56 #define THREAD_SPECIFIC_INDEX pthread_key_t
57 #define ts_key_create(ts_key, destructor) pthread_key_create (&(ts_key), destructor);
60 /* Syncrhronization macros: Win32 and Pthreads */
63 #define pause(voidpara) __asm PAUSE
64 #define sem_init(sem, sem_attr1, sem_init_value) (int)((*sem = CreateEvent(NULL,FALSE,FALSE,NULL))==NULL)
65 #define sem_wait(sem) (int)(WAIT_OBJECT_0 != WaitForSingleObject(*sem,VPXINFINITE))
66 #define sem_post(sem) SetEvent(*sem)
67 #define sem_destroy(sem) if(*sem)((int)(CloseHandle(*sem))==TRUE)
68 #define thread_sleep(nms) Sleep(nms)
73 #define sem_t semaphore_t
74 #define sem_init(X,Y,Z) semaphore_create(mach_task_self(), X, SYNC_POLICY_FIFO, Z)
75 #define sem_wait(sem) (semaphore_wait(*sem) )
76 #define sem_post(sem) semaphore_signal(*sem)
77 #define sem_destroy(sem) semaphore_destroy(mach_task_self(),*sem)
78 #define thread_sleep(nms) /* { struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
82 #define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
84 /* Not Windows. Assume pthreads */
88 #if ARCH_X86 || ARCH_X86_64
89 #include "vpx_ports/x86.h"
91 #define x86_pause_hint()
94 #endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */