]> git.sesse.net Git - ffmpeg/blob - compat/os2threads.h
avfilter/vf_v360: do not remap alpha plane if that will be overwritten later
[ffmpeg] / compat / os2threads.h
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 #include <os2.h>
31
32 #undef __STRICT_ANSI__          /* for _beginthread() */
33 #include <stdlib.h>
34 #include <time.h>
35
36 #include <sys/builtin.h>
37 #include <sys/fmutex.h>
38
39 #include "libavutil/attributes.h"
40 #include "libavutil/common.h"
41 #include "libavutil/time.h"
42
43 typedef struct {
44     TID tid;
45     void *(*start_routine)(void *);
46     void *arg;
47     void *result;
48 } pthread_t;
49
50 typedef void pthread_attr_t;
51
52 typedef _fmutex pthread_mutex_t;
53 typedef void pthread_mutexattr_t;
54
55 #define PTHREAD_MUTEX_INITIALIZER _FMUTEX_INITIALIZER
56
57 typedef struct {
58     HEV event_sem;
59     HEV ack_sem;
60     volatile unsigned  wait_count;
61 } pthread_cond_t;
62
63 typedef void pthread_condattr_t;
64
65 typedef struct {
66     volatile int done;
67     _fmutex mtx;
68 } pthread_once_t;
69
70 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
71
72 static void thread_entry(void *arg)
73 {
74     pthread_t *thread = arg;
75
76     thread->result = thread->start_routine(thread->arg);
77 }
78
79 static av_always_inline int pthread_create(pthread_t *thread,
80                                            const pthread_attr_t *attr,
81                                            void *(*start_routine)(void*),
82                                            void *arg)
83 {
84     thread->start_routine = start_routine;
85     thread->arg = arg;
86     thread->result = NULL;
87
88     thread->tid = _beginthread(thread_entry, NULL, 1024 * 1024, thread);
89
90     return 0;
91 }
92
93 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
94 {
95     DosWaitThread(&thread.tid, DCWW_WAIT);
96
97     if (value_ptr)
98         *value_ptr = thread.result;
99
100     return 0;
101 }
102
103 static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex,
104                                                const pthread_mutexattr_t *attr)
105 {
106     _fmutex_create(mutex, 0);
107
108     return 0;
109 }
110
111 static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
112 {
113     _fmutex_close(mutex);
114
115     return 0;
116 }
117
118 static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
119 {
120     _fmutex_request(mutex, 0);
121
122     return 0;
123 }
124
125 static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
126 {
127     _fmutex_release(mutex);
128
129     return 0;
130 }
131
132 static av_always_inline int pthread_cond_init(pthread_cond_t *cond,
133                                               const pthread_condattr_t *attr)
134 {
135     DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
136     DosCreateEventSem(NULL, &cond->ack_sem, DCE_POSTONE, FALSE);
137
138     cond->wait_count = 0;
139
140     return 0;
141 }
142
143 static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
144 {
145     DosCloseEventSem(cond->event_sem);
146     DosCloseEventSem(cond->ack_sem);
147
148     return 0;
149 }
150
151 static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
152 {
153     if (!__atomic_cmpxchg32(&cond->wait_count, 0, 0)) {
154         DosPostEventSem(cond->event_sem);
155         DosWaitEventSem(cond->ack_sem, SEM_INDEFINITE_WAIT);
156     }
157
158     return 0;
159 }
160
161 static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
162 {
163     while (!__atomic_cmpxchg32(&cond->wait_count, 0, 0))
164         pthread_cond_signal(cond);
165
166     return 0;
167 }
168
169 static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond,
170                                                    pthread_mutex_t *mutex,
171                                                    const struct timespec *abstime)
172 {
173     int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000;
174     ULONG t = av_clip64(abs_milli - av_gettime() / 1000, 0, ULONG_MAX);
175
176     __atomic_increment(&cond->wait_count);
177
178     pthread_mutex_unlock(mutex);
179
180     APIRET ret = DosWaitEventSem(cond->event_sem, t);
181
182     __atomic_decrement(&cond->wait_count);
183
184     DosPostEventSem(cond->ack_sem);
185
186     pthread_mutex_lock(mutex);
187
188     return (ret == ERROR_TIMEOUT) ? ETIMEDOUT : 0;
189 }
190
191 static av_always_inline int pthread_cond_wait(pthread_cond_t *cond,
192                                               pthread_mutex_t *mutex)
193 {
194     __atomic_increment(&cond->wait_count);
195
196     pthread_mutex_unlock(mutex);
197
198     DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
199
200     __atomic_decrement(&cond->wait_count);
201
202     DosPostEventSem(cond->ack_sem);
203
204     pthread_mutex_lock(mutex);
205
206     return 0;
207 }
208
209 static av_always_inline int pthread_once(pthread_once_t *once_control,
210                                          void (*init_routine)(void))
211 {
212     if (!once_control->done)
213     {
214         _fmutex_request(&once_control->mtx, 0);
215
216         if (!once_control->done)
217         {
218             init_routine();
219
220             once_control->done = 1;
221         }
222
223         _fmutex_release(&once_control->mtx);
224     }
225
226     return 0;
227 }
228 #endif /* COMPAT_OS2THREADS_H */