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 /* Thread management macros */
20 #define _WIN32_WINNT 0x500 /* WINBASE.H - Enable signal_object_and_wait */
23 #define THREAD_FUNCTION DWORD WINAPI
24 #define THREAD_FUNCTION_RETURN DWORD
25 #define THREAD_SPECIFIC_INDEX DWORD
26 #define pthread_t HANDLE
27 #define pthread_attr_t DWORD
28 #define pthread_create(thhandle,attr,thfunc,tharg) (int)((*thhandle=(HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))thfunc,tharg,0,NULL))==NULL)
29 #define pthread_join(thread, result) ((WaitForSingleObject((thread),VPXINFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
30 #define pthread_detach(thread) if(thread!=NULL)CloseHandle(thread)
31 #define thread_sleep(nms) Sleep(nms)
32 #define pthread_cancel(thread) terminate_thread(thread,0)
33 #define ts_key_create(ts_key, destructor) {ts_key = TlsAlloc();};
34 #define pthread_getspecific(ts_key) TlsGetValue(ts_key)
35 #define pthread_setspecific(ts_key, value) TlsSetValue(ts_key, (void *)value)
36 #define pthread_self() GetCurrentThreadId()
39 #include <mach/semaphore.h>
40 #include <mach/task.h>
45 #include <semaphore.h>
50 /* Nearly everything is already defined */
51 #define THREAD_FUNCTION void *
52 #define THREAD_FUNCTION_RETURN void *
53 #define THREAD_SPECIFIC_INDEX pthread_key_t
54 #define ts_key_create(ts_key, destructor) pthread_key_create (&(ts_key), destructor);
57 /* Syncrhronization macros: Win32 and Pthreads */
60 #define pause(voidpara) __asm PAUSE
61 #define sem_init(sem, sem_attr1, sem_init_value) (int)((*sem = CreateEvent(NULL,FALSE,FALSE,NULL))==NULL)
62 #define sem_wait(sem) (int)(WAIT_OBJECT_0 != WaitForSingleObject(*sem,VPXINFINITE))
63 #define sem_post(sem) SetEvent(*sem)
64 #define sem_destroy(sem) if(*sem)((int)(CloseHandle(*sem))==TRUE)
65 #define thread_sleep(nms) Sleep(nms)
70 #define sem_t semaphore_t
71 #define sem_init(X,Y,Z) semaphore_create(mach_task_self(), X, SYNC_POLICY_FIFO, Z)
72 #define sem_wait(sem) (semaphore_wait(*sem) )
73 #define sem_post(sem) semaphore_signal(*sem)
74 #define sem_destroy(sem) semaphore_destroy(mach_task_self(),*sem)
75 #define thread_sleep(nms) /* { struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
79 #define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
81 /* Not Windows. Assume pthreads */
85 #if ARCH_X86 || ARCH_X86_64
86 #include "vpx_ports/x86.h"
88 #define x86_pause_hint()