]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
* src/misc/threads.c: Implementation of real-time priorities for UNIX-like
[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.35 2003/11/07 19:30:28 massiot 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(PTHREAD_COND_T_IN_PTHREAD_H)
80 #   define VLC_THREAD_PRIORITY_LOW 0
81 #   define VLC_THREAD_PRIORITY_INPUT 20
82 #   define VLC_THREAD_PRIORITY_AUDIO 10
83 #   define VLC_THREAD_PRIORITY_VIDEO 0
84 #   define VLC_THREAD_PRIORITY_OUTPUT 30
85
86 #elif defined(WIN32) || defined(UNDER_CE)
87 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
88 #   define VLC_THREAD_PRIORITY_LOW 0
89 #   define VLC_THREAD_PRIORITY_INPUT \
90         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
91 #   define VLC_THREAD_PRIORITY_AUDIO \
92         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
93 #   define VLC_THREAD_PRIORITY_VIDEO \
94         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
95 #   define VLC_THREAD_PRIORITY_OUTPUT \
96         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
97 #   define VLC_THREAD_PRIORITY_HIGHEST \
98         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
99
100 #elif defined(SYS_BEOS)
101 #   define VLC_THREAD_PRIORITY_LOW 5
102 #   define VLC_THREAD_PRIORITY_INPUT 10
103 #   define VLC_THREAD_PRIORITY_AUDIO 10
104 #   define VLC_THREAD_PRIORITY_VIDEO 5
105 #   define VLC_THREAD_PRIORITY_OUTPUT 15
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( PTHREAD_COND_T_IN_PTHREAD_H )
176 typedef pthread_t       vlc_thread_t;
177 typedef struct
178 {
179     pthread_mutex_t mutex;
180     vlc_object_t * p_this;
181 } vlc_mutex_t;
182 typedef struct
183 {
184     pthread_cond_t cond;
185     vlc_object_t * p_this;
186 } vlc_cond_t;
187
188 #elif defined( HAVE_CTHREADS_H )
189 typedef cthread_t       vlc_thread_t;
190
191 /* Those structs are the ones defined in /include/cthreads.h but we need
192  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
193  * foo is a (mutex_t*) */
194 typedef struct
195 {
196     spin_lock_t held;
197     spin_lock_t lock;
198     char *name;
199     struct cthread_queue queue;
200
201     vlc_object_t * p_this;
202 } vlc_mutex_t;
203
204 typedef struct
205 {
206     spin_lock_t lock;
207     struct cthread_queue queue;
208     char *name;
209     struct cond_imp *implications;
210
211     vlc_object_t * p_this;
212 } vlc_cond_t;
213
214 #elif defined( HAVE_KERNEL_SCHEDULER_H )
215 /* This is the BeOS implementation of the vlc threads, note that the mutex is
216  * not a real mutex and the cond_var is not like a pthread cond_var but it is
217  * enough for what wee need */
218
219 typedef thread_id vlc_thread_t;
220
221 typedef struct
222 {
223     int32_t         init;
224     sem_id          lock;
225
226     vlc_object_t * p_this;
227 } vlc_mutex_t;
228
229 typedef struct
230 {
231     int32_t         init;
232     thread_id       thread;
233
234     vlc_object_t * p_this;
235 } vlc_cond_t;
236
237 #endif
238