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