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