FFmpeg
w32pthreads.h
Go to the documentation of this file.
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 #include <time.h>
42 
43 #include "libavutil/attributes.h"
44 #include "libavutil/common.h"
45 #include "libavutil/internal.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/time.h"
48 
49 typedef struct pthread_t {
50  void *handle;
51  void *(*func)(void* arg);
52  void *arg;
53  void *ret;
54 } pthread_t;
55 
56 /* use light weight mutex/condition variable API for Windows Vista and later */
57 typedef SRWLOCK pthread_mutex_t;
58 typedef CONDITION_VARIABLE pthread_cond_t;
59 
60 #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
61 #define PTHREAD_COND_INITIALIZER CONDITION_VARIABLE_INIT
62 
63 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
64 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
65 
66 static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
67 {
68  pthread_t *h = (pthread_t*)arg;
69  h->ret = h->func(h->arg);
70  return 0;
71 }
72 
73 static av_unused int pthread_create(pthread_t *thread, const void *unused_attr,
74  void *(*start_routine)(void*), void *arg)
75 {
76  thread->func = start_routine;
77  thread->arg = arg;
78 #if HAVE_WINRT
79  thread->handle = (void*)CreateThread(NULL, 0, win32thread_worker, thread,
80  0, NULL);
81 #else
82  thread->handle = (void*)_beginthreadex(NULL, 0, win32thread_worker, thread,
83  0, NULL);
84 #endif
85  return !thread->handle;
86 }
87 
88 static av_unused int pthread_join(pthread_t thread, void **value_ptr)
89 {
90  DWORD ret = WaitForSingleObject(thread.handle, INFINITE);
91  if (ret != WAIT_OBJECT_0) {
92  if (ret == WAIT_ABANDONED)
93  return EINVAL;
94  else
95  return EDEADLK;
96  }
97  if (value_ptr)
98  *value_ptr = thread.ret;
99  CloseHandle(thread.handle);
100  return 0;
101 }
102 
103 static inline int pthread_mutex_init(pthread_mutex_t *m, void* attr)
104 {
105  InitializeSRWLock(m);
106  return 0;
107 }
109 {
110  /* Unlocked SWR locks use no resources */
111  return 0;
112 }
113 static inline int pthread_mutex_lock(pthread_mutex_t *m)
114 {
115  AcquireSRWLockExclusive(m);
116  return 0;
117 }
119 {
120  ReleaseSRWLockExclusive(m);
121  return 0;
122 }
123 
124 typedef INIT_ONCE pthread_once_t;
125 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
126 
127 static av_unused int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
128 {
129  BOOL pending = FALSE;
130  InitOnceBeginInitialize(once_control, 0, &pending, NULL);
131  if (pending)
132  init_routine();
133  InitOnceComplete(once_control, 0, NULL);
134  return 0;
135 }
136 
137 static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
138 {
139  InitializeConditionVariable(cond);
140  return 0;
141 }
142 
143 /* native condition variables do not destroy */
145 {
146  return 0;
147 }
148 
150 {
151  WakeAllConditionVariable(cond);
152  return 0;
153 }
154 
156 {
157  SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
158  return 0;
159 }
160 
162  const struct timespec *abstime)
163 {
164  int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
165  DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX);
166 
167  if (!SleepConditionVariableSRW(cond, mutex, t, 0)) {
168  DWORD err = GetLastError();
169  if (err == ERROR_TIMEOUT)
170  return ETIMEDOUT;
171  else
172  return EINVAL;
173  }
174  return 0;
175 }
176 
178 {
179  WakeConditionVariable(cond);
180  return 0;
181 }
182 
183 #endif /* COMPAT_W32PTHREADS_H */
#define NULL
Definition: coverity.c:32
static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:155
static AVMutex mutex
Definition: log.c:44
#define WaitForSingleObject(a, b)
Definition: w32pthreads.h:64
CONDITION_VARIABLE pthread_cond_t
Definition: w32pthreads.h:58
static av_unused unsigned __stdcall attribute_align_arg win32thread_worker(void *arg)
Definition: w32pthreads.h:66
Memory handling functions.
Macro definitions for various function/variable attributes.
static int pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:144
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:73
void * ret
Definition: w32pthreads.h:53
SRWLOCK pthread_mutex_t
Definition: w32pthreads.h:57
void * arg
Definition: os2threads.h:47
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:103
#define FALSE
Definition: windows2linux.h:37
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:118
common internal API header
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:108
void *(* func)(void *arg)
Definition: w32pthreads.h:51
void *(* start_routine)(void *)
Definition: os2threads.h:46
static int pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:177
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
INIT_ONCE pthread_once_t
Definition: w32pthreads.h:124
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:113
static int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: w32pthreads.h:161
uint32_t DWORD
static av_unused int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
Definition: w32pthreads.h:127
#define __stdcall
Definition: windows2linux.h:21
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
static int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:149
static av_unused int pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:88
_fmutex pthread_mutex_t
Definition: os2threads.h:53
common internal and external API header
void * handle
Definition: w32pthreads.h:50
uint32_t BOOL
static int pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:137
#define av_unused
Definition: attributes.h:125