Merge "vp8_rd_pick_best_mbsegmentation code restructure"
[libvpx.git] / vp8 / common / threading.h
blob1929f7c4fe9e09712d5efc2c44a785938d763373
1 /*
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.
9 */
12 #ifndef _PTHREAD_EMULATION
13 #define _PTHREAD_EMULATION
15 #define VPXINFINITE 10000 /* 10second. */
17 /* Thread management macros */
18 #ifdef _WIN32
19 /* Win32 */
20 #define _WIN32_WINNT 0x500 /* WINBASE.H - Enable signal_object_and_wait */
21 #include <process.h>
22 #include <windows.h>
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()
37 #else
38 #ifdef __APPLE__
39 #include <mach/semaphore.h>
40 #include <mach/task.h>
41 #include <time.h>
42 #include <unistd.h>
44 #else
45 #include <semaphore.h>
46 #endif
48 #include <pthread.h>
49 /* pthreads */
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);
55 #endif
57 /* Syncrhronization macros: Win32 and Pthreads */
58 #ifdef _WIN32
59 #define sem_t HANDLE
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)
67 #else
69 #ifdef __APPLE__
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);} */
76 #else
77 #include <unistd.h>
78 #include <sched.h>
79 #define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
80 #endif
81 /* Not Windows. Assume pthreads */
83 #endif
85 #if ARCH_X86 || ARCH_X86_64
86 #include "vpx_ports/x86.h"
87 #else
88 #define x86_pause_hint()
89 #endif
91 #endif