]> git.sesse.net Git - ffmpeg/blob - compat/w32pthreads.h
Merge commit '5846b496f0a1dd5be4ef714622940674305ec00f'
[ffmpeg] / compat / w32pthreads.h
1 /*
2  * Copyright (C) 2010-2011 x264 project
3  *
4  * Authors: Steven Walters <kemuri9@gmail.com>
5  *          Pegasys Inc. <http://www.pegasys-inc.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * w32threads to pthreads wrapper
27  */
28
29 #ifndef COMPAT_W32PTHREADS_H
30 #define COMPAT_W32PTHREADS_H
31
32 /* Build up a pthread-like API using underlying Windows API. Have only static
33  * methods so as to not conflict with a potentially linked in pthread-win32
34  * library.
35  * As most functions here are used without checking return values,
36  * only implement return values as necessary. */
37
38 #define WIN32_LEAN_AND_MEAN
39 #include <windows.h>
40 #include <process.h>
41
42 #include "libavutil/attributes.h"
43 #include "libavutil/common.h"
44 #include "libavutil/internal.h"
45 #include "libavutil/mem.h"
46
47 typedef struct pthread_t {
48     void *handle;
49     void *(*func)(void* arg);
50     void *arg;
51     void *ret;
52 } pthread_t;
53
54 /* use light weight mutex/condition variable API for Windows Vista and later */
55 typedef SRWLOCK pthread_mutex_t;
56 typedef CONDITION_VARIABLE pthread_cond_t;
57
58 #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
59 #define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
60
61 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
62 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
63
64 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
65 {
66     pthread_t *h = (pthread_t*)arg;
67     h->ret = h->func(h->arg);
68     return 0;
69 }
70
71 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
72                                     void *(*start_routine)(void*), void *arg)
73 {
74     thread->func   = start_routine;
75     thread->arg    = arg;
76 #if HAVE_WINRT
77     thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
78                                            0, NULL);
79 #else
80     thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
81                                            0, NULL);
82 #endif
83     return !thread->handle;
84 }
85
86 static av_unused int pthread_join(pthread_t thread, void **value_ptr)
87 {
88     DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
89     if (ret != WAIT_OBJECT_0) {
90         if (ret == WAIT_ABANDONED)
91             return EINVAL;
92         else
93             return EDEADLK;
94     }
95     if (value_ptr)
96         *value_ptr = thread.ret;
97     CloseHandle(thread.handle);
98     return 0;
99 }
100
101 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
102 {
103     InitializeSRWLock(m);
104     return 0;
105 }
106 static inline int pthread_mutex_destroy(pthread_mutex_t *m)
107 {
108     /* Unlocked SWR locks use no resources */
109     return 0;
110 }
111 static inline int pthread_mutex_lock(pthread_mutex_t *m)
112 {
113     AcquireSRWLockExclusive(m);
114     return 0;
115 }
116 static inline int pthread_mutex_unlock(pthread_mutex_t *m)
117 {
118     ReleaseSRWLockExclusive(m);
119     return 0;
120 }
121
122 typedef INIT_ONCE pthread_once_t;
123 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
124
125 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
126 {
127     BOOL pending = FALSE;
128     InitOnceBeginInitialize(once_control, 0, &pending, NULL);
129     if (pending)
130         init_routine();
131     InitOnceComplete(once_control, 0, NULL);
132     return 0;
133 }
134
135 static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
136 {
137     InitializeConditionVariable(cond);
138     return 0;
139 }
140
141 /* native condition variables do not destroy */
142 static inline int pthread_cond_destroy(pthread_cond_t *cond)
143 {
144     return 0;
145 }
146
147 static inline int pthread_cond_broadcast(pthread_cond_t *cond)
148 {
149     WakeAllConditionVariable(cond);
150     return 0;
151 }
152
153 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
154 {
155     SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
156     return 0;
157 }
158
159 static inline int pthread_cond_signal(pthread_cond_t *cond)
160 {
161     WakeConditionVariable(cond);
162     return 0;
163 }
164
165 #endif /* COMPAT_W32PTHREADS_H */