FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
os2threads.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 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 
35 #include <sys/fmutex.h>
36 
37 #include "libavutil/mem.h"
38 
39 typedef TID pthread_t;
40 typedef void pthread_attr_t;
41 
42 typedef HMTX pthread_mutex_t;
43 typedef void pthread_mutexattr_t;
44 
45 typedef struct {
46  HEV event_sem;
49 
50 typedef void pthread_condattr_t;
51 
52 typedef struct {
53  volatile int done;
54  _fmutex mtx;
56 
57 #define PTHREAD_ONCE_INIT {0, _FMUTEX_INITIALIZER}
58 
59 struct thread_arg {
60  void *(*start_routine)(void *);
61  void *arg;
62 };
63 
64 static void thread_entry(void *arg)
65 {
66  struct thread_arg *thread_arg = arg;
67 
68  thread_arg->start_routine(thread_arg->arg);
69 
70  av_free(thread_arg);
71 }
72 
73 static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
74 {
75  struct thread_arg *thread_arg;
76 
77  thread_arg = av_mallocz(sizeof(struct thread_arg));
78  if (!thread_arg)
79  return ENOMEM;
80 
81  thread_arg->start_routine = start_routine;
82  thread_arg->arg = arg;
83 
84  *thread = _beginthread(thread_entry, NULL, 256 * 1024, thread_arg);
85 
86  return 0;
87 }
88 
89 static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
90 {
91  DosWaitThread((PTID)&thread, DCWW_WAIT);
92 
93  return 0;
94 }
95 
97 {
98  DosCreateMutexSem(NULL, (PHMTX)mutex, 0, FALSE);
99 
100  return 0;
101 }
102 
104 {
105  DosCloseMutexSem(*(PHMTX)mutex);
106 
107  return 0;
108 }
109 
111 {
112  DosRequestMutexSem(*(PHMTX)mutex, SEM_INDEFINITE_WAIT);
113 
114  return 0;
115 }
116 
118 {
119  DosReleaseMutexSem(*(PHMTX)mutex);
120 
121  return 0;
122 }
123 
125 {
126  DosCreateEventSem(NULL, &cond->event_sem, DCE_POSTONE, FALSE);
127 
128  cond->wait_count = 0;
129 
130  return 0;
131 }
132 
134 {
135  DosCloseEventSem(cond->event_sem);
136 
137  return 0;
138 }
139 
141 {
142  if (cond->wait_count > 0) {
143  DosPostEventSem(cond->event_sem);
144 
145  cond->wait_count--;
146  }
147 
148  return 0;
149 }
150 
152 {
153  while (cond->wait_count > 0) {
154  DosPostEventSem(cond->event_sem);
155 
156  cond->wait_count--;
157  }
158 
159  return 0;
160 }
161 
163 {
164  cond->wait_count++;
165 
166  pthread_mutex_unlock(mutex);
167 
168  DosWaitEventSem(cond->event_sem, SEM_INDEFINITE_WAIT);
169 
170  pthread_mutex_lock(mutex);
171 
172  return 0;
173 }
174 
175 static av_always_inline int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
176 {
177  if (!once_control->done)
178  {
179  _fmutex_request(&once_control->mtx, 0);
180 
181  if (!once_control->done)
182  {
183  init_routine();
184 
185  once_control->done = 1;
186  }
187 
188  _fmutex_release(&once_control->mtx);
189  }
190 
191  return 0;
192 }
193 #endif /* COMPAT_OS2THREADS_H */
#define NULL
Definition: coverity.c:32
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:103
static void thread_entry(void *arg)
Definition: os2threads.h:64
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:162
memory handling functions
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:133
void *(* start_routine)(void *)
Definition: os2threads.h:60
void pthread_mutexattr_t
Definition: os2threads.h:43
HMTX pthread_mutex_t
Definition: os2threads.h:42
void * arg
Definition: os2threads.h:61
_fmutex mtx
Definition: os2threads.h:54
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:140
volatile int done
Definition: os2threads.h:53
#define FALSE
Definition: windows2linux.h:37
const char * arg
Definition: jacosubdec.c:66
TID pthread_t
Definition: os2threads.h:39
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:89
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:96
void pthread_attr_t
Definition: os2threads.h:40
static pthread_mutex_t * mutex
Definition: w32pthreads.h:258
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:73
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:124
void pthread_condattr_t
Definition: os2threads.h:50
#define av_free(p)
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:151
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:117
#define av_always_inline
Definition: attributes.h:39
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:110
static av_always_inline int pthread_once(pthread_once_t *once_control, void(*init_routine)(void))
Definition: os2threads.h:175
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252