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