]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
Fix previous commit
[vlc] / include / vlc_threads.h
1 /*****************************************************************************
2  * vlc_threads.h : threads implementation for the VideoLAN client
3  * This header provides portable declarations for mutexes & conditions
4  *****************************************************************************
5  * Copyright (C) 1999, 2002 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9  *          Samuel Hocevar <sam@via.ecp.fr>
10  *          Gildas Bazin <gbazin@netcourrier.com>
11  *          Christophe Massiot <massiot@via.ecp.fr>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #if !defined( __LIBVLC__ )
29   #error You are not libvlc or one of its plugins. You cannot include this file
30 #endif
31
32 #ifndef _VLC_THREADS_H_
33 #define _VLC_THREADS_H_
34
35 #include <stdio.h>
36
37 #if defined(DEBUG) && defined(HAVE_SYS_TIME_H)
38 #   include <sys/time.h>
39 #endif
40
41 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
42 #   include <pth.h>
43
44 #elif defined( ST_INIT_IN_ST_H )                            /* State threads */
45 #   include <st.h>
46
47 #elif defined( UNDER_CE )
48                                                                 /* WinCE API */
49 #elif defined( WIN32 )
50 #   include <process.h>                                         /* Win32 API */
51
52 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
53 #   include <kernel/OS.h>
54 #   include <kernel/scheduler.h>
55 #   include <byteorder.h>
56
57 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
58 #   define LIBVLC_USE_PTHREAD 1
59 #   define _APPLE_C_SOURCE    1 /* Proper pthread semantics on OSX */
60
61 #   include <unistd.h> /* _POSIX_SPIN_LOCKS */
62 #   include <pthread.h>
63     /* Needed for pthread_cond_timedwait */
64 #   include <errno.h>
65     /* This is not prototyped under Linux, though it exists. */
66     int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
67
68 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
69 #   include <cthreads.h>
70
71 #else
72 #   error no threads available on your system !
73
74 #endif
75
76 /*****************************************************************************
77  * Constants
78  *****************************************************************************/
79
80 /* Thread priorities */
81 #ifdef __APPLE__
82 #   define VLC_THREAD_PRIORITY_LOW (-47)
83 #   define VLC_THREAD_PRIORITY_INPUT 37
84 #   define VLC_THREAD_PRIORITY_AUDIO 37
85 #   define VLC_THREAD_PRIORITY_VIDEO (-47)
86 #   define VLC_THREAD_PRIORITY_OUTPUT 37
87 #   define VLC_THREAD_PRIORITY_HIGHEST 37
88
89 #elif defined(SYS_BEOS)
90 #   define VLC_THREAD_PRIORITY_LOW 5
91 #   define VLC_THREAD_PRIORITY_INPUT 10
92 #   define VLC_THREAD_PRIORITY_AUDIO 10
93 #   define VLC_THREAD_PRIORITY_VIDEO 5
94 #   define VLC_THREAD_PRIORITY_OUTPUT 15
95 #   define VLC_THREAD_PRIORITY_HIGHEST 15
96
97 #elif defined(PTHREAD_COND_T_IN_PTHREAD_H)
98 #   define VLC_THREAD_PRIORITY_LOW 0
99 #   define VLC_THREAD_PRIORITY_INPUT 20
100 #   define VLC_THREAD_PRIORITY_AUDIO 10
101 #   define VLC_THREAD_PRIORITY_VIDEO 0
102 #   define VLC_THREAD_PRIORITY_OUTPUT 30
103 #   define VLC_THREAD_PRIORITY_HIGHEST 40
104
105 #elif defined(WIN32) || defined(UNDER_CE)
106 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
107 #   define VLC_THREAD_PRIORITY_LOW 0
108 #   define VLC_THREAD_PRIORITY_INPUT \
109         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
110 #   define VLC_THREAD_PRIORITY_AUDIO \
111         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
112 #   define VLC_THREAD_PRIORITY_VIDEO \
113         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
114 #   define VLC_THREAD_PRIORITY_OUTPUT \
115         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
116 #   define VLC_THREAD_PRIORITY_HIGHEST \
117         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
118
119 #else
120 #   define VLC_THREAD_PRIORITY_LOW 0
121 #   define VLC_THREAD_PRIORITY_INPUT 0
122 #   define VLC_THREAD_PRIORITY_AUDIO 0
123 #   define VLC_THREAD_PRIORITY_VIDEO 0
124 #   define VLC_THREAD_PRIORITY_OUTPUT 0
125 #   define VLC_THREAD_PRIORITY_HIGHEST 0
126
127 #endif
128
129 /*****************************************************************************
130  * Type definitions
131  *****************************************************************************/
132
133 #if defined( PTH_INIT_IN_PTH_H )
134 typedef pth_t            vlc_thread_t;
135 typedef struct
136 {
137     pth_mutex_t mutex;
138     vlc_object_t * p_this;
139 } vlc_mutex_t;
140 typedef struct
141 {
142     pth_cond_t cond;
143     vlc_object_t * p_this;
144 } vlc_cond_t;
145 typedef struct
146 {
147     int handle;
148 } vlc_threadvar_t;
149
150 #elif defined( ST_INIT_IN_ST_H )
151 typedef st_thread_t      vlc_thread_t;
152 typedef struct
153 {
154     st_mutex_t mutex;
155     vlc_object_t * p_this;
156 } vlc_mutex_t;
157 typedef struct
158 {
159     st_cond_t cond;
160     vlc_object_t * p_this;
161 } vlc_cond_t;
162 typedef struct
163 {
164     int handle;
165 } vlc_threadvar_t;
166
167 #elif defined( WIN32 ) || defined( UNDER_CE )
168 typedef struct
169 {
170     /* thread id */
171     DWORD  id;
172     /*
173     ** handle to created thread, needs be closed to dispose of it
174     ** even after thread has exited
175     */
176     HANDLE hThread;
177 } vlc_thread_t;
178
179 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
180
181 typedef struct
182 {
183     /* WinNT/2K/XP implementation */
184     HANDLE              mutex;
185     /* Win95/98/ME implementation */
186     CRITICAL_SECTION    csection;
187
188     vlc_object_t * p_this;
189 } vlc_mutex_t;
190
191 typedef struct
192 {
193     volatile int        i_waiting_threads;
194     /* WinNT/2K/XP implementation */
195     HANDLE              event;
196     SIGNALOBJECTANDWAIT SignalObjectAndWait;
197     /* Win95/98/ME implementation */
198     HANDLE              semaphore;
199     CRITICAL_SECTION    csection;
200     int                 i_win9x_cv;
201
202     vlc_object_t * p_this;
203 } vlc_cond_t;
204
205 typedef struct
206 {
207     DWORD   handle;
208 } vlc_threadvar_t;
209
210 #elif defined( HAVE_KERNEL_SCHEDULER_H )
211 /* This is the BeOS implementation of the vlc threads, note that the mutex is
212  * not a real mutex and the cond_var is not like a pthread cond_var but it is
213  * enough for what we need */
214
215 typedef thread_id vlc_thread_t;
216
217 typedef struct
218 {
219     int32_t         init;
220     sem_id          lock;
221
222     vlc_object_t * p_this;
223 } vlc_mutex_t;
224
225 typedef struct
226 {
227     int32_t         init;
228     thread_id       thread;
229
230     vlc_object_t * p_this;
231 } vlc_cond_t;
232
233 typedef struct
234 {
235 } vlc_threadvar_t;
236
237
238 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
239 typedef pthread_t       vlc_thread_t;
240 typedef struct
241 {
242     pthread_mutex_t mutex;
243     vlc_object_t * p_this;
244 } vlc_mutex_t;
245 typedef struct
246 {
247     pthread_cond_t cond;
248     vlc_object_t * p_this;
249 } vlc_cond_t;
250
251 typedef struct
252 {
253     pthread_key_t handle;
254 } vlc_threadvar_t;
255
256 #elif defined( HAVE_CTHREADS_H )
257 typedef cthread_t       vlc_thread_t;
258
259 /* Those structs are the ones defined in /include/cthreads.h but we need
260  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
261  * foo is a (mutex_t*) */
262 typedef struct
263 {
264     spin_lock_t held;
265     spin_lock_t lock;
266     char *name;
267     struct cthread_queue queue;
268
269     vlc_object_t * p_this;
270 } vlc_mutex_t;
271
272 typedef struct
273 {
274     spin_lock_t lock;
275     struct cthread_queue queue;
276     char *name;
277     struct cond_imp *implications;
278
279     vlc_object_t * p_this;
280 } vlc_cond_t;
281
282 typedef struct
283 {
284     cthread_key_t handle;
285 } vlc_threadvar_t;
286
287 #endif
288
289 #endif