]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
3df206b4a658a957e21c64e681a5e51f3ed380d9
[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 VideoLAN
6  * $Id: vlc_threads.h,v 1.37 2004/03/01 12:50:39 gbazin Exp $
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
79 #elif defined(SYS_BEOS)
80 #   define VLC_THREAD_PRIORITY_LOW 5
81 #   define VLC_THREAD_PRIORITY_INPUT 10
82 #   define VLC_THREAD_PRIORITY_AUDIO 10
83 #   define VLC_THREAD_PRIORITY_VIDEO 5
84 #   define VLC_THREAD_PRIORITY_OUTPUT 15
85
86 #elif defined(PTHREAD_COND_T_IN_PTHREAD_H)
87 #   define VLC_THREAD_PRIORITY_LOW 0
88 #   define VLC_THREAD_PRIORITY_INPUT 20
89 #   define VLC_THREAD_PRIORITY_AUDIO 10
90 #   define VLC_THREAD_PRIORITY_VIDEO 0
91 #   define VLC_THREAD_PRIORITY_OUTPUT 30
92
93 #elif defined(WIN32) || defined(UNDER_CE)
94 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
95 #   define VLC_THREAD_PRIORITY_LOW 0
96 #   define VLC_THREAD_PRIORITY_INPUT \
97         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
98 #   define VLC_THREAD_PRIORITY_AUDIO \
99         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
100 #   define VLC_THREAD_PRIORITY_VIDEO \
101         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
102 #   define VLC_THREAD_PRIORITY_OUTPUT \
103         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
104 #   define VLC_THREAD_PRIORITY_HIGHEST \
105         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
106
107 #else
108 #   define VLC_THREAD_PRIORITY_LOW 0
109 #   define VLC_THREAD_PRIORITY_INPUT 0
110 #   define VLC_THREAD_PRIORITY_AUDIO 0
111 #   define VLC_THREAD_PRIORITY_VIDEO 0
112 #   define VLC_THREAD_PRIORITY_OUTPUT 0
113
114 #endif
115
116 /*****************************************************************************
117  * Type definitions
118  *****************************************************************************/
119
120 #if defined( PTH_INIT_IN_PTH_H )
121 typedef pth_t            vlc_thread_t;
122 typedef struct
123 {
124     pth_mutex_t mutex;
125     vlc_object_t * p_this;
126 } vlc_mutex_t;
127 typedef struct
128 {
129     pth_cond_t cond;
130     vlc_object_t * p_this;
131 } vlc_cond_t;
132
133 #elif defined( ST_INIT_IN_ST_H )
134 typedef st_thread_t      vlc_thread_t;
135 typedef struct
136 {
137     st_mutex_t mutex;
138     vlc_object_t * p_this;
139 } vlc_mutex_t;
140 typedef struct
141 {
142     st_cond_t cond;
143     vlc_object_t * p_this;
144 } vlc_cond_t;
145
146 #elif defined( WIN32 ) || defined( UNDER_CE )
147 typedef HANDLE vlc_thread_t;
148 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
149 typedef unsigned (__stdcall *PTHREAD_START) (void *);
150
151 typedef struct
152 {
153     /* WinNT/2K/XP implementation */
154     HANDLE              mutex;
155     /* Win95/98/ME implementation */
156     CRITICAL_SECTION    csection;
157
158     vlc_object_t * p_this;
159 } vlc_mutex_t;
160
161 typedef struct
162 {
163     volatile int        i_waiting_threads;
164     /* WinNT/2K/XP implementation */
165     HANDLE              event;
166     SIGNALOBJECTANDWAIT SignalObjectAndWait;
167     /* Win95/98/ME implementation */
168     HANDLE              semaphore;
169     CRITICAL_SECTION    csection;
170     int                 i_win9x_cv;
171
172     vlc_object_t * p_this;
173 } vlc_cond_t;
174
175 #elif defined( HAVE_KERNEL_SCHEDULER_H )
176 /* This is the BeOS implementation of the vlc threads, note that the mutex is
177  * not a real mutex and the cond_var is not like a pthread cond_var but it is
178  * enough for what wee need */
179
180 typedef thread_id vlc_thread_t;
181
182 typedef struct
183 {
184     int32_t         init;
185     sem_id          lock;
186
187     vlc_object_t * p_this;
188 } vlc_mutex_t;
189
190 typedef struct
191 {
192     int32_t         init;
193     thread_id       thread;
194
195     vlc_object_t * p_this;
196 } vlc_cond_t;
197
198 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
199 typedef pthread_t       vlc_thread_t;
200 typedef struct
201 {
202     pthread_mutex_t mutex;
203     vlc_object_t * p_this;
204 } vlc_mutex_t;
205 typedef struct
206 {
207     pthread_cond_t cond;
208     vlc_object_t * p_this;
209 } vlc_cond_t;
210
211 #elif defined( HAVE_CTHREADS_H )
212 typedef cthread_t       vlc_thread_t;
213
214 /* Those structs are the ones defined in /include/cthreads.h but we need
215  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
216  * foo is a (mutex_t*) */
217 typedef struct
218 {
219     spin_lock_t held;
220     spin_lock_t lock;
221     char *name;
222     struct cthread_queue queue;
223
224     vlc_object_t * p_this;
225 } vlc_mutex_t;
226
227 typedef struct
228 {
229     spin_lock_t lock;
230     struct cthread_queue queue;
231     char *name;
232     struct cond_imp *implications;
233
234     vlc_object_t * p_this;
235 } vlc_cond_t;
236
237 #endif
238