threading.h 5.94 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


12
13
#ifndef VP8_COMMON_THREADING_H_
#define VP8_COMMON_THREADING_H_
John Koleszar's avatar
John Koleszar committed
14

15
#include "./vpx_config.h"
Yaowu Xu's avatar
Yaowu Xu committed
16

17
18
19
20
#ifdef __cplusplus
extern "C" {
#endif

21
#if CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD
22

John Koleszar's avatar
John Koleszar committed
23
/* Thread management macros */
24
#if defined(_WIN32) && !HAVE_PTHREAD_H
John Koleszar's avatar
John Koleszar committed
25
26
27
/* Win32 */
#include <process.h>
#include <windows.h>
Yunqing Wang's avatar
Yunqing Wang committed
28
#define THREAD_FUNCTION unsigned int __stdcall
John Koleszar's avatar
John Koleszar committed
29
30
31
32
33
34
35
36
37
38
39
#define THREAD_FUNCTION_RETURN DWORD
#define THREAD_SPECIFIC_INDEX DWORD
#define pthread_t HANDLE
#define pthread_attr_t DWORD
#define pthread_detach(thread) if(thread!=NULL)CloseHandle(thread)
#define thread_sleep(nms) Sleep(nms)
#define pthread_cancel(thread) terminate_thread(thread,0)
#define ts_key_create(ts_key, destructor) {ts_key = TlsAlloc();};
#define pthread_getspecific(ts_key) TlsGetValue(ts_key)
#define pthread_setspecific(ts_key, value) TlsSetValue(ts_key, (void *)value)
#define pthread_self() GetCurrentThreadId()
KO Myung-Hun's avatar
KO Myung-Hun committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#elif defined(__OS2__)
/* OS/2 */
#define INCL_DOS
#include <os2.h>

#include <stdlib.h>
#define THREAD_FUNCTION void
#define THREAD_FUNCTION_RETURN void
#define THREAD_SPECIFIC_INDEX PULONG
#define pthread_t TID
#define pthread_attr_t ULONG
#define pthread_detach(thread) 0
#define thread_sleep(nms) DosSleep(nms)
#define pthread_cancel(thread) DosKillThread(thread)
#define ts_key_create(ts_key, destructor) \
    DosAllocThreadLocalMemory(1, &(ts_key));
#define pthread_getspecific(ts_key) ((void *)(*(ts_key)))
#define pthread_setspecific(ts_key, value) (*(ts_key)=(ULONG)(value))
#define pthread_self() _gettid()
John Koleszar's avatar
John Koleszar committed
60
61
#else
#ifdef __APPLE__
62
#include <mach/mach_init.h>
John Koleszar's avatar
John Koleszar committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <mach/semaphore.h>
#include <mach/task.h>
#include <time.h>
#include <unistd.h>

#else
#include <semaphore.h>
#endif

#include <pthread.h>
/* pthreads */
/* Nearly everything is already defined */
#define THREAD_FUNCTION void *
#define THREAD_FUNCTION_RETURN void *
#define THREAD_SPECIFIC_INDEX pthread_key_t
#define ts_key_create(ts_key, destructor) pthread_key_create (&(ts_key), destructor);
#endif

81
82
/* Synchronization macros: Win32 and Pthreads */
#if defined(_WIN32) && !HAVE_PTHREAD_H
John Koleszar's avatar
John Koleszar committed
83
84
#define sem_t HANDLE
#define pause(voidpara) __asm PAUSE
85
#define sem_init(sem, sem_attr1, sem_init_value) (int)((*sem = CreateSemaphore(NULL,0,32768,NULL))==NULL)
86
#define sem_wait(sem) (int)(WAIT_OBJECT_0 != WaitForSingleObject(*sem,INFINITE))
87
#define sem_post(sem) ReleaseSemaphore(*sem,1,NULL)
John Koleszar's avatar
John Koleszar committed
88
89
90
#define sem_destroy(sem) if(*sem)((int)(CloseHandle(*sem))==TRUE)
#define thread_sleep(nms) Sleep(nms)

KO Myung-Hun's avatar
KO Myung-Hun committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#elif defined(__OS2__)
typedef struct
{
    HEV  event;
    HMTX wait_mutex;
    HMTX count_mutex;
    int  count;
} sem_t;

static inline int sem_init(sem_t *sem, int pshared, unsigned int value)
{
    DosCreateEventSem(NULL, &sem->event, pshared ? DC_SEM_SHARED : 0,
                      value > 0 ? TRUE : FALSE);
    DosCreateMutexSem(NULL, &sem->wait_mutex, 0, FALSE);
    DosCreateMutexSem(NULL, &sem->count_mutex, 0, FALSE);

    sem->count = value;

    return 0;
}

static inline int sem_wait(sem_t * sem)
{
    DosRequestMutexSem(sem->wait_mutex, -1);

    DosWaitEventSem(sem->event, -1);

    DosRequestMutexSem(sem->count_mutex, -1);

    sem->count--;
    if (sem->count == 0)
    {
        ULONG post_count;

        DosResetEventSem(sem->event, &post_count);
    }

    DosReleaseMutexSem(sem->count_mutex);

    DosReleaseMutexSem(sem->wait_mutex);

    return 0;
}

static inline int sem_post(sem_t * sem)
{
    DosRequestMutexSem(sem->count_mutex, -1);

    if (sem->count < 32768)
    {
        sem->count++;
        DosPostEventSem(sem->event);
    }

    DosReleaseMutexSem(sem->count_mutex);

    return 0;
}

static inline int sem_destroy(sem_t * sem)
{
    DosCloseEventSem(sem->event);
    DosCloseMutexSem(sem->wait_mutex);
    DosCloseMutexSem(sem->count_mutex);

    return 0;
}

#define thread_sleep(nms) DosSleep(nms)

John Koleszar's avatar
John Koleszar committed
161
162
163
164
165
166
167
168
#else

#ifdef __APPLE__
#define sem_t semaphore_t
#define sem_init(X,Y,Z) semaphore_create(mach_task_self(), X, SYNC_POLICY_FIFO, Z)
#define sem_wait(sem) (semaphore_wait(*sem) )
#define sem_post(sem) semaphore_signal(*sem)
#define sem_destroy(sem) semaphore_destroy(mach_task_self(),*sem)
169
#define thread_sleep(nms) /* { struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
John Koleszar's avatar
John Koleszar committed
170
171
#else
#include <unistd.h>
172
#include <sched.h>
173
#define thread_sleep(nms) sched_yield();/* {struct timespec ts;ts.tv_sec=0; ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
John Koleszar's avatar
John Koleszar committed
174
175
176
177
178
179
180
181
182
183
184
#endif
/* Not Windows. Assume pthreads */

#endif

#if ARCH_X86 || ARCH_X86_64
#include "vpx_ports/x86.h"
#else
#define x86_pause_hint()
#endif

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "vpx_util/vpx_thread.h"

static INLINE void mutex_lock(pthread_mutex_t *const mutex) {
    const int kMaxTryLocks = 4000;
    int locked = 0;
    int i;

    for (i = 0; i < kMaxTryLocks; ++i) {
        if (!pthread_mutex_trylock(mutex)) {
            locked = 1;
            break;
        }
    }

    if (!locked)
        pthread_mutex_lock(mutex);
}

static INLINE int protected_read(pthread_mutex_t *const mutex, const int *p) {
    int ret;
    mutex_lock(mutex);
    ret = *p;
    pthread_mutex_unlock(mutex);
    return ret;
}

static INLINE void sync_read(pthread_mutex_t *const mutex, int mb_col,
                             const int *last_row_current_mb_col,
                             const int nsync) {
    while (mb_col > (protected_read(mutex, last_row_current_mb_col) - nsync)) {
        x86_pause_hint();
        thread_sleep(0);
    }
}

static INLINE void protected_write(pthread_mutex_t *mutex, int *p, int v) {
    mutex_lock(mutex);
    *p = v;
    pthread_mutex_unlock(mutex);
}

226
#endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */
227

228
229
230
231
#ifdef __cplusplus
}  // extern "C"
#endif

232
#endif  // VP8_COMMON_THREADING_H_