]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
Make Zorglub less unhappy
[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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 #include <stdio.h>
29
30 #if defined(DEBUG) && defined(HAVE_SYS_TIME_H)
31 #   include <sys/time.h>
32 #endif
33
34 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
35 #   include <pth.h>
36
37 #elif defined( ST_INIT_IN_ST_H )                            /* State threads */
38 #   include <st.h>
39
40 #elif defined( UNDER_CE )
41                                                                 /* WinCE API */
42 #elif defined( WIN32 )
43 #   include <process.h>                                         /* Win32 API */
44
45 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
46 #   include <kernel/OS.h>
47 #   include <kernel/scheduler.h>
48 #   include <byteorder.h>
49
50 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
51 #   include <pthread.h>
52 #   ifdef DEBUG
53         /* Needed for pthread_cond_timedwait */
54 #       include <errno.h>
55 #   endif
56     /* This is not prototyped under Linux, though it exists. */
57     int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
58
59 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
60 #   include <cthreads.h>
61
62 #else
63 #   error no threads available on your system !
64
65 #endif
66
67 /*****************************************************************************
68  * Constants
69  *****************************************************************************/
70
71 /* Thread priorities */
72 #ifdef SYS_DARWIN
73 #   define VLC_THREAD_PRIORITY_LOW (-47)
74 #   define VLC_THREAD_PRIORITY_INPUT 37
75 #   define VLC_THREAD_PRIORITY_AUDIO 37
76 #   define VLC_THREAD_PRIORITY_VIDEO (-47)
77 #   define VLC_THREAD_PRIORITY_OUTPUT 37
78 #   define VLC_THREAD_PRIORITY_HIGHEST 37
79
80 #elif defined(SYS_BEOS)
81 #   define VLC_THREAD_PRIORITY_LOW 5
82 #   define VLC_THREAD_PRIORITY_INPUT 10
83 #   define VLC_THREAD_PRIORITY_AUDIO 10
84 #   define VLC_THREAD_PRIORITY_VIDEO 5
85 #   define VLC_THREAD_PRIORITY_OUTPUT 15
86 #   define VLC_THREAD_PRIORITY_HIGHEST 15
87
88 #elif defined(PTHREAD_COND_T_IN_PTHREAD_H)
89 #   define VLC_THREAD_PRIORITY_LOW 0
90 #   define VLC_THREAD_PRIORITY_INPUT 20
91 #   define VLC_THREAD_PRIORITY_AUDIO 10
92 #   define VLC_THREAD_PRIORITY_VIDEO 0
93 #   define VLC_THREAD_PRIORITY_OUTPUT 30
94 #   define VLC_THREAD_PRIORITY_HIGHEST 40
95
96 #elif defined(WIN32) || defined(UNDER_CE)
97 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
98 #   define VLC_THREAD_PRIORITY_LOW 0
99 #   define VLC_THREAD_PRIORITY_INPUT \
100         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
101 #   define VLC_THREAD_PRIORITY_AUDIO \
102         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
103 #   define VLC_THREAD_PRIORITY_VIDEO \
104         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
105 #   define VLC_THREAD_PRIORITY_OUTPUT \
106         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
107 #   define VLC_THREAD_PRIORITY_HIGHEST \
108         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
109
110 #else
111 #   define VLC_THREAD_PRIORITY_LOW 0
112 #   define VLC_THREAD_PRIORITY_INPUT 0
113 #   define VLC_THREAD_PRIORITY_AUDIO 0
114 #   define VLC_THREAD_PRIORITY_VIDEO 0
115 #   define VLC_THREAD_PRIORITY_OUTPUT 0
116 #   define VLC_THREAD_PRIORITY_HIGHEST 0
117
118 #endif
119
120 /*****************************************************************************
121  * Type definitions
122  *****************************************************************************/
123
124 #if defined( PTH_INIT_IN_PTH_H )
125 typedef pth_t            vlc_thread_t;
126 typedef struct
127 {
128     pth_mutex_t mutex;
129     vlc_object_t * p_this;
130 } vlc_mutex_t;
131 typedef struct
132 {
133     pth_cond_t cond;
134     vlc_object_t * p_this;
135 } vlc_cond_t;
136
137 #elif defined( ST_INIT_IN_ST_H )
138 typedef st_thread_t      vlc_thread_t;
139 typedef struct
140 {
141     st_mutex_t mutex;
142     vlc_object_t * p_this;
143 } vlc_mutex_t;
144 typedef struct
145 {
146     st_cond_t cond;
147     vlc_object_t * p_this;
148 } vlc_cond_t;
149
150 #elif defined( WIN32 ) || defined( UNDER_CE )
151 typedef HANDLE vlc_thread_t;
152 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
153 typedef unsigned (WINAPI *PTHREAD_START) (void *);
154
155 typedef struct
156 {
157     /* WinNT/2K/XP implementation */
158     HANDLE              mutex;
159     /* Win95/98/ME implementation */
160     CRITICAL_SECTION    csection;
161
162     vlc_object_t * p_this;
163 } vlc_mutex_t;
164
165 typedef struct
166 {
167     volatile int        i_waiting_threads;
168     /* WinNT/2K/XP implementation */
169     HANDLE              event;
170     SIGNALOBJECTANDWAIT SignalObjectAndWait;
171     /* Win95/98/ME implementation */
172     HANDLE              semaphore;
173     CRITICAL_SECTION    csection;
174     int                 i_win9x_cv;
175
176     vlc_object_t * p_this;
177 } vlc_cond_t;
178
179 #elif defined( HAVE_KERNEL_SCHEDULER_H )
180 /* This is the BeOS implementation of the vlc threads, note that the mutex is
181  * not a real mutex and the cond_var is not like a pthread cond_var but it is
182  * enough for what wee need */
183
184 typedef thread_id vlc_thread_t;
185
186 typedef struct
187 {
188     int32_t         init;
189     sem_id          lock;
190
191     vlc_object_t * p_this;
192 } vlc_mutex_t;
193
194 typedef struct
195 {
196     int32_t         init;
197     thread_id       thread;
198
199     vlc_object_t * p_this;
200 } vlc_cond_t;
201
202 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
203 typedef pthread_t       vlc_thread_t;
204 typedef struct
205 {
206     pthread_mutex_t mutex;
207     vlc_object_t * p_this;
208 } vlc_mutex_t;
209 typedef struct
210 {
211     pthread_cond_t cond;
212     vlc_object_t * p_this;
213 } vlc_cond_t;
214
215 #elif defined( HAVE_CTHREADS_H )
216 typedef cthread_t       vlc_thread_t;
217
218 /* Those structs are the ones defined in /include/cthreads.h but we need
219  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
220  * foo is a (mutex_t*) */
221 typedef struct
222 {
223     spin_lock_t held;
224     spin_lock_t lock;
225     char *name;
226     struct cthread_queue queue;
227
228     vlc_object_t * p_this;
229 } vlc_mutex_t;
230
231 typedef struct
232 {
233     spin_lock_t lock;
234     struct cthread_queue queue;
235     char *name;
236     struct cond_imp *implications;
237
238     vlc_object_t * p_this;
239 } vlc_cond_t;
240
241 #endif
242