media/libvpx/vp8/common/threading.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/common/threading.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,186 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +
    1.15 +#ifndef _PTHREAD_EMULATION
    1.16 +#define _PTHREAD_EMULATION
    1.17 +
    1.18 +#if CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD
    1.19 +
    1.20 +/* Thread management macros */
    1.21 +#ifdef _WIN32
    1.22 +/* Win32 */
    1.23 +#include <process.h>
    1.24 +#include <windows.h>
    1.25 +#define THREAD_FUNCTION DWORD WINAPI
    1.26 +#define THREAD_FUNCTION_RETURN DWORD
    1.27 +#define THREAD_SPECIFIC_INDEX DWORD
    1.28 +#define pthread_t HANDLE
    1.29 +#define pthread_attr_t DWORD
    1.30 +#define pthread_create(thhandle,attr,thfunc,tharg) (int)((*thhandle=(HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))thfunc,tharg,0,NULL))==NULL)
    1.31 +#define pthread_join(thread, result) ((WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
    1.32 +#define pthread_detach(thread) if(thread!=NULL)CloseHandle(thread)
    1.33 +#define thread_sleep(nms) Sleep(nms)
    1.34 +#define pthread_cancel(thread) terminate_thread(thread,0)
    1.35 +#define ts_key_create(ts_key, destructor) {ts_key = TlsAlloc();};
    1.36 +#define pthread_getspecific(ts_key) TlsGetValue(ts_key)
    1.37 +#define pthread_setspecific(ts_key, value) TlsSetValue(ts_key, (void *)value)
    1.38 +#define pthread_self() GetCurrentThreadId()
    1.39 +
    1.40 +#elif defined(__OS2__)
    1.41 +/* OS/2 */
    1.42 +#define INCL_DOS
    1.43 +#include <os2.h>
    1.44 +
    1.45 +#include <stdlib.h>
    1.46 +#define THREAD_FUNCTION void
    1.47 +#define THREAD_FUNCTION_RETURN void
    1.48 +#define THREAD_SPECIFIC_INDEX PULONG
    1.49 +#define pthread_t TID
    1.50 +#define pthread_attr_t ULONG
    1.51 +#define pthread_create(thhandle,attr,thfunc,tharg) \
    1.52 +    ((int)((*(thhandle)=_beginthread(thfunc,NULL,1024*1024,tharg))==-1))
    1.53 +#define pthread_join(thread, result) ((int)DosWaitThread(&(thread),0))
    1.54 +#define pthread_detach(thread) 0
    1.55 +#define thread_sleep(nms) DosSleep(nms)
    1.56 +#define pthread_cancel(thread) DosKillThread(thread)
    1.57 +#define ts_key_create(ts_key, destructor) \
    1.58 +    DosAllocThreadLocalMemory(1, &(ts_key));
    1.59 +#define pthread_getspecific(ts_key) ((void *)(*(ts_key)))
    1.60 +#define pthread_setspecific(ts_key, value) (*(ts_key)=(ULONG)(value))
    1.61 +#define pthread_self() _gettid()
    1.62 +#else
    1.63 +#ifdef __APPLE__
    1.64 +#include <mach/mach_init.h>
    1.65 +#include <mach/semaphore.h>
    1.66 +#include <mach/task.h>
    1.67 +#include <time.h>
    1.68 +#include <unistd.h>
    1.69 +
    1.70 +#else
    1.71 +#include <semaphore.h>
    1.72 +#endif
    1.73 +
    1.74 +#include <pthread.h>
    1.75 +/* pthreads */
    1.76 +/* Nearly everything is already defined */
    1.77 +#define THREAD_FUNCTION void *
    1.78 +#define THREAD_FUNCTION_RETURN void *
    1.79 +#define THREAD_SPECIFIC_INDEX pthread_key_t
    1.80 +#define ts_key_create(ts_key, destructor) pthread_key_create (&(ts_key), destructor);
    1.81 +#endif
    1.82 +
    1.83 +/* Syncrhronization macros: Win32 and Pthreads */
    1.84 +#ifdef _WIN32
    1.85 +#define sem_t HANDLE
    1.86 +#define pause(voidpara) __asm PAUSE
    1.87 +#define sem_init(sem, sem_attr1, sem_init_value) (int)((*sem = CreateSemaphore(NULL,0,32768,NULL))==NULL)
    1.88 +#define sem_wait(sem) (int)(WAIT_OBJECT_0 != WaitForSingleObject(*sem,INFINITE))
    1.89 +#define sem_post(sem) ReleaseSemaphore(*sem,1,NULL)
    1.90 +#define sem_destroy(sem) if(*sem)((int)(CloseHandle(*sem))==TRUE)
    1.91 +#define thread_sleep(nms) Sleep(nms)
    1.92 +
    1.93 +#elif defined(__OS2__)
    1.94 +typedef struct
    1.95 +{
    1.96 +    HEV  event;
    1.97 +    HMTX wait_mutex;
    1.98 +    HMTX count_mutex;
    1.99 +    int  count;
   1.100 +} sem_t;
   1.101 +
   1.102 +static inline int sem_init(sem_t *sem, int pshared, unsigned int value)
   1.103 +{
   1.104 +    DosCreateEventSem(NULL, &sem->event, pshared ? DC_SEM_SHARED : 0,
   1.105 +                      value > 0 ? TRUE : FALSE);
   1.106 +    DosCreateMutexSem(NULL, &sem->wait_mutex, 0, FALSE);
   1.107 +    DosCreateMutexSem(NULL, &sem->count_mutex, 0, FALSE);
   1.108 +
   1.109 +    sem->count = value;
   1.110 +
   1.111 +    return 0;
   1.112 +}
   1.113 +
   1.114 +static inline int sem_wait(sem_t * sem)
   1.115 +{
   1.116 +    DosRequestMutexSem(sem->wait_mutex, -1);
   1.117 +
   1.118 +    DosWaitEventSem(sem->event, -1);
   1.119 +
   1.120 +    DosRequestMutexSem(sem->count_mutex, -1);
   1.121 +
   1.122 +    sem->count--;
   1.123 +    if (sem->count == 0)
   1.124 +    {
   1.125 +        ULONG post_count;
   1.126 +
   1.127 +        DosResetEventSem(sem->event, &post_count);
   1.128 +    }
   1.129 +
   1.130 +    DosReleaseMutexSem(sem->count_mutex);
   1.131 +
   1.132 +    DosReleaseMutexSem(sem->wait_mutex);
   1.133 +
   1.134 +    return 0;
   1.135 +}
   1.136 +
   1.137 +static inline int sem_post(sem_t * sem)
   1.138 +{
   1.139 +    DosRequestMutexSem(sem->count_mutex, -1);
   1.140 +
   1.141 +    if (sem->count < 32768)
   1.142 +    {
   1.143 +        sem->count++;
   1.144 +        DosPostEventSem(sem->event);
   1.145 +    }
   1.146 +
   1.147 +    DosReleaseMutexSem(sem->count_mutex);
   1.148 +
   1.149 +    return 0;
   1.150 +}
   1.151 +
   1.152 +static inline int sem_destroy(sem_t * sem)
   1.153 +{
   1.154 +    DosCloseEventSem(sem->event);
   1.155 +    DosCloseMutexSem(sem->wait_mutex);
   1.156 +    DosCloseMutexSem(sem->count_mutex);
   1.157 +
   1.158 +    return 0;
   1.159 +}
   1.160 +
   1.161 +#define thread_sleep(nms) DosSleep(nms)
   1.162 +
   1.163 +#else
   1.164 +
   1.165 +#ifdef __APPLE__
   1.166 +#define sem_t semaphore_t
   1.167 +#define sem_init(X,Y,Z) semaphore_create(mach_task_self(), X, SYNC_POLICY_FIFO, Z)
   1.168 +#define sem_wait(sem) (semaphore_wait(*sem) )
   1.169 +#define sem_post(sem) semaphore_signal(*sem)
   1.170 +#define sem_destroy(sem) semaphore_destroy(mach_task_self(),*sem)
   1.171 +#define thread_sleep(nms) /* { struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
   1.172 +#else
   1.173 +#include <unistd.h>
   1.174 +#include <sched.h>
   1.175 +#define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
   1.176 +#endif
   1.177 +/* Not Windows. Assume pthreads */
   1.178 +
   1.179 +#endif
   1.180 +
   1.181 +#if ARCH_X86 || ARCH_X86_64
   1.182 +#include "vpx_ports/x86.h"
   1.183 +#else
   1.184 +#define x86_pause_hint()
   1.185 +#endif
   1.186 +
   1.187 +#endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */
   1.188 +
   1.189 +#endif

mercurial