FFmpeg
Loading...
Searching...
No Matches
os2threads.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2017 KO Myung-Hun <komh@chollian.net>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * os2threads to pthreads wrapper
24 */
25
26#ifndef COMPAT_OS2THREADS_H
27#define COMPAT_OS2THREADS_H
28
29#define INCL_DOS
30#define INCL_DOSERRORS
31#include <os2.h>
32
33#undef __STRICT_ANSI__ /* for _beginthread() */
34#include <stdlib.h>
35#include <time.h>
36
37#include <sys/builtin.h>
38#include <sys/fmutex.h>
39
41#include "libavutil/common.h"
42#include "libavutil/time.h"
43
44typedef struct {
45 TID tid;
46 void *(*start_routine)(void *);
47 void *arg;
48 void *result;
49} pthread_t;
50
51typedef void pthread_attr_t;
52
53typedef _fmutex pthread_mutex_t;
55
56#define PTHREAD_MUTEX_INITIALIZER _FMUTEX_INITIALIZER
57
58typedef struct {
61 volatile unsigned wait_count;
63
64typedef void pthread_condattr_t;
65
66typedef struct {
67 volatile int done;
68 _fmutex mtx;
70
71#define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
72
73static void thread_entry(void *arg)
74{
75 pthread_t *thread = arg;
76
77 thread->result = thread->start_routine(thread->arg);
78}
79
81 const pthread_attr_t *attr,
82 void *(*start_routine)(void*),
83 void *arg)
84{
85 thread->start_routine = start_routine;
86 thread->arg = arg;
87 thread->result = NULL;
88
89 thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
90
91 return 0;
92}
93
94static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
95{
96 DosWaitThread(&thread.tid, DCWW_WAIT);
97
98 if (value_ptr)
99 *value_ptr = thread.result;
100
101 return 0;
102}
103
105 const pthread_mutexattr_t *attr)
106{
107 _fmutex_create(mutex, 0);
108
109 return 0;
110}
111
113{
114 _fmutex_close(mutex);
115
116 return 0;
117}
118
120{
121 _fmutex_request(mutex, 0);
122
123 return 0;
124}
125
127{
128 return _fmutex_request(mutex, _FMR_NOWAIT) == ERROR_MUTEX_OWNED ?
129 EBUSY : 0;
130}
131
133{
134 _fmutex_release(mutex);
135
136 return 0;
137}
138
140 const pthread_condattr_t *attr)
141{
142 DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
143 DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
144
145 cond->wait_count = 0;
146
147 return 0;
148}
149
151{
152 DosCloseEventSem(cond->event_sem);
153 DosCloseEventSem(cond->ack_sem);
154
155 return 0;
156}
157
159{
160 if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
161 DosPostEventSem(cond->event_sem);
162 DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
163 }
164
165 return 0;
166}
167
169{
170 while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
172
173 return 0;
174}
175
178 const struct timespec *abstime)
179{
180 int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
181 ULONG t = av_clip64(abs_milli - av_gettime() / 1000, 0, ULONG_MAX);
182
183 __atomic_increment(&cond->wait_count);
184
186
187 APIRET ret = DosWaitEventSem(cond->event_sem, t);
188
189 __atomic_decrement(&cond->wait_count);
190
191 DosPostEventSem(cond->ack_sem);
192
194
195 return (ret == ERROR_TIMEOUT) ? ETIMEDOUT : 0;
196}
197
200{
201 __atomic_increment(&cond->wait_count);
202
204
205 DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
206
207 __atomic_decrement(&cond->wait_count);
208
209 DosPostEventSem(cond->ack_sem);
210
212
213 return 0;
214}
215
217 void (*init_routine)(void))
218{
219 if (!once_control->done)
220 {
221 _fmutex_request(&once_control->mtx, 0);
222
223 if (!once_control->done)
224 {
225 init_routine();
226
227 once_control->done = 1;
228 }
229
230 _fmutex_release(&once_control->mtx);
231 }
232
233 return 0;
234}
235#endif /* COMPAT_OS2THREADS_H */
common internal and external API header
#define av_clip64
Definition common.h:103
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
const char * arg
Definition jacosubdec.c:65
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
static void thread_entry(void *arg)
Definition os2threads.h:73
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition os2threads.h:168
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition os2threads.h:158
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition os2threads.h:119
void pthread_mutexattr_t
Definition os2threads.h:54
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition os2threads.h:150
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition os2threads.h:104
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition os2threads.h:94
static av_always_inline int pthread_mutex_trylock(pthread_mutex_t *mutex)
Definition os2threads.h:126
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition os2threads.h:139
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition os2threads.h:80
void pthread_condattr_t
Definition os2threads.h:64
static av_always_inline int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
Definition os2threads.h:216
_fmutex pthread_mutex_t
Definition os2threads.h:53
void pthread_attr_t
Definition os2threads.h:51
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition os2threads.h:132
static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition os2threads.h:176
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition os2threads.h:198
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition os2threads.h:112
int(* cond)(enum AVPixelFormat pix_fmt)
static AVMutex mutex
Definition resman.c:61
volatile unsigned wait_count
Definition os2threads.h:61
volatile int done
Definition os2threads.h:67
void * arg
Definition os2threads.h:47
void *(* start_routine)(void *)
Definition os2threads.h:46
void * result
Definition os2threads.h:48
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40