Home | History | Annotate | Download | only in common
      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 #define VPXINFINITE 10000       /* 10second. */
     16 
     17 #if CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD
     18 
     19 /* Thread management macros */
     20 #ifdef _WIN32
     21 /* Win32 */
     22 #define _WIN32_WINNT 0x500 /* WINBASE.H - Enable signal_object_and_wait */
     23 #include <process.h>
     24 #include <windows.h>
     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()
     39 #else
     40 #ifdef __APPLE__
     41 #include <mach/mach_init.h>
     42 #include <mach/semaphore.h>
     43 #include <mach/task.h>
     44 #include <time.h>
     45 #include <unistd.h>
     46 
     47 #else
     48 #include <semaphore.h>
     49 #endif
     50 
     51 #include <pthread.h>
     52 /* pthreads */
     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);
     58 #endif
     59 
     60 /* Syncrhronization macros: Win32 and Pthreads */
     61 #ifdef _WIN32
     62 #define sem_t HANDLE
     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)
     69 
     70 #else
     71 
     72 #ifdef __APPLE__
     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);} */
     79 #else
     80 #include <unistd.h>
     81 #include <sched.h>
     82 #define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
     83 #endif
     84 /* Not Windows. Assume pthreads */
     85 
     86 #endif
     87 
     88 #if ARCH_X86 || ARCH_X86_64
     89 #include "vpx_ports/x86.h"
     90 #else
     91 #define x86_pause_hint()
     92 #endif
     93 
     94 #endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */
     95 
     96 #endif
     97