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