]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
Makes services_discovery option strictly load the given module (i.e. not use a fallba...
[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 <pthread.h>
62 #   ifdef DEBUG
63         /* Needed for pthread_cond_timedwait */
64 #       include <errno.h>
65 #   endif
66     /* This is not prototyped under Linux, though it exists. */
67     int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
68
69 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
70 #   include <cthreads.h>
71
72 #else
73 #   error no threads available on your system !
74
75 #endif
76
77 /*****************************************************************************
78  * Constants
79  *****************************************************************************/
80
81 /* Thread priorities */
82 #ifdef __APPLE__
83 #   define VLC_THREAD_PRIORITY_LOW (-47)
84 #   define VLC_THREAD_PRIORITY_INPUT 37
85 #   define VLC_THREAD_PRIORITY_AUDIO 37
86 #   define VLC_THREAD_PRIORITY_VIDEO (-47)
87 #   define VLC_THREAD_PRIORITY_OUTPUT 37
88 #   define VLC_THREAD_PRIORITY_HIGHEST 37
89
90 #elif defined(SYS_BEOS)
91 #   define VLC_THREAD_PRIORITY_LOW 5
92 #   define VLC_THREAD_PRIORITY_INPUT 10
93 #   define VLC_THREAD_PRIORITY_AUDIO 10
94 #   define VLC_THREAD_PRIORITY_VIDEO 5
95 #   define VLC_THREAD_PRIORITY_OUTPUT 15
96 #   define VLC_THREAD_PRIORITY_HIGHEST 15
97
98 #elif defined(PTHREAD_COND_T_IN_PTHREAD_H)
99 #   define VLC_THREAD_PRIORITY_LOW 0
100 #   define VLC_THREAD_PRIORITY_INPUT 20
101 #   define VLC_THREAD_PRIORITY_AUDIO 10
102 #   define VLC_THREAD_PRIORITY_VIDEO 0
103 #   define VLC_THREAD_PRIORITY_OUTPUT 30
104 #   define VLC_THREAD_PRIORITY_HIGHEST 40
105
106 #elif defined(WIN32) || defined(UNDER_CE)
107 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
108 #   define VLC_THREAD_PRIORITY_LOW 0
109 #   define VLC_THREAD_PRIORITY_INPUT \
110         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
111 #   define VLC_THREAD_PRIORITY_AUDIO \
112         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
113 #   define VLC_THREAD_PRIORITY_VIDEO \
114         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
115 #   define VLC_THREAD_PRIORITY_OUTPUT \
116         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
117 #   define VLC_THREAD_PRIORITY_HIGHEST \
118         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
119
120 #else
121 #   define VLC_THREAD_PRIORITY_LOW 0
122 #   define VLC_THREAD_PRIORITY_INPUT 0
123 #   define VLC_THREAD_PRIORITY_AUDIO 0
124 #   define VLC_THREAD_PRIORITY_VIDEO 0
125 #   define VLC_THREAD_PRIORITY_OUTPUT 0
126 #   define VLC_THREAD_PRIORITY_HIGHEST 0
127
128 #endif
129
130 /*****************************************************************************
131  * Type definitions
132  *****************************************************************************/
133
134 #if defined( PTH_INIT_IN_PTH_H )
135 typedef pth_t            vlc_thread_t;
136 typedef struct
137 {
138     pth_mutex_t mutex;
139     vlc_object_t * p_this;
140 } vlc_mutex_t;
141 typedef struct
142 {
143     pth_cond_t cond;
144     vlc_object_t * p_this;
145 } vlc_cond_t;
146 typedef struct
147 {
148     int handle;
149 } vlc_threadvar_t;
150
151 #elif defined( ST_INIT_IN_ST_H )
152 typedef st_thread_t      vlc_thread_t;
153 typedef struct
154 {
155     st_mutex_t mutex;
156     vlc_object_t * p_this;
157 } vlc_mutex_t;
158 typedef struct
159 {
160     st_cond_t cond;
161     vlc_object_t * p_this;
162 } vlc_cond_t;
163 typedef struct
164 {
165     int handle;
166 } vlc_threadvar_t;
167
168 #elif defined( WIN32 ) || defined( UNDER_CE )
169 typedef struct
170 {
171     /* thread id */
172     DWORD  id;
173     /*
174     ** handle to created thread, needs be closed to dispose of it
175     ** even after thread has exited
176     */
177     HANDLE hThread;
178 } vlc_thread_t;
179
180 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
181
182 typedef struct
183 {
184     /* WinNT/2K/XP implementation */
185     HANDLE              mutex;
186     /* Win95/98/ME implementation */
187     CRITICAL_SECTION    csection;
188
189     vlc_object_t * p_this;
190 } vlc_mutex_t;
191
192 typedef struct
193 {
194     volatile int        i_waiting_threads;
195     /* WinNT/2K/XP implementation */
196     HANDLE              event;
197     SIGNALOBJECTANDWAIT SignalObjectAndWait;
198     /* Win95/98/ME implementation */
199     HANDLE              semaphore;
200     CRITICAL_SECTION    csection;
201     int                 i_win9x_cv;
202
203     vlc_object_t * p_this;
204 } vlc_cond_t;
205
206 typedef struct
207 {
208     DWORD   handle;
209 } vlc_threadvar_t;
210
211 #elif defined( HAVE_KERNEL_SCHEDULER_H )
212 /* This is the BeOS implementation of the vlc threads, note that the mutex is
213  * not a real mutex and the cond_var is not like a pthread cond_var but it is
214  * enough for what we need */
215
216 typedef thread_id vlc_thread_t;
217
218 typedef struct
219 {
220     int32_t         init;
221     sem_id          lock;
222
223     vlc_object_t * p_this;
224 } vlc_mutex_t;
225
226 typedef struct
227 {
228     int32_t         init;
229     thread_id       thread;
230
231     vlc_object_t * p_this;
232 } vlc_cond_t;
233
234 typedef struct
235 {
236 } vlc_threadvar_t;
237
238
239 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
240 typedef pthread_t       vlc_thread_t;
241 typedef struct
242 {
243     pthread_mutex_t mutex;
244     vlc_object_t * p_this;
245 } vlc_mutex_t;
246 typedef struct
247 {
248     pthread_cond_t cond;
249     vlc_object_t * p_this;
250 } vlc_cond_t;
251
252 typedef struct
253 {
254     pthread_key_t handle;
255 } vlc_threadvar_t;
256
257 #elif defined( HAVE_CTHREADS_H )
258 typedef cthread_t       vlc_thread_t;
259
260 /* Those structs are the ones defined in /include/cthreads.h but we need
261  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
262  * foo is a (mutex_t*) */
263 typedef struct
264 {
265     spin_lock_t held;
266     spin_lock_t lock;
267     char *name;
268     struct cthread_queue queue;
269
270     vlc_object_t * p_this;
271 } vlc_mutex_t;
272
273 typedef struct
274 {
275     spin_lock_t lock;
276     struct cthread_queue queue;
277     char *name;
278     struct cond_imp *implications;
279
280     vlc_object_t * p_this;
281 } vlc_cond_t;
282
283 typedef struct
284 {
285     cthread_key_t handle;
286 } vlc_threadvar_t;
287
288 #endif
289
290 #endif