]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
* ./bootstrap : Fixed an issue with old shell versions
[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.10 2002/08/29 23:53:22 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(GPROF) || defined(DEBUG)
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( WIN32 )                                          /* Win32 API */
41 #   include <process.h>
42
43 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
44 #   include <pthread.h>
45 #   ifdef DEBUG
46         /* Needed for pthread_cond_timedwait */
47 #       include <errno.h>
48 #   endif
49     /* This is not prototyped under Linux, though it exists. */
50     int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
51
52 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
53 #   include <cthreads.h>
54
55 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
56 #   include <kernel/OS.h>
57 #   include <kernel/scheduler.h>
58 #   include <byteorder.h>
59
60 #else
61 #   error no threads available on your system !
62
63 #endif
64
65 /*****************************************************************************
66  * Constants
67  *****************************************************************************/
68
69 /* Thread priorities */
70 #ifdef SYS_DARWIN
71 #   define VLC_THREAD_PRIORITY_LOW 10
72 #   define VLC_THREAD_PRIORITY_INPUT 37
73 #   define VLC_THREAD_PRIORITY_AUDIO 38
74 #   define VLC_THREAD_PRIORITY_OUTPUT 38
75
76 #elif defined(WIN32)
77 #   define VLC_THREAD_PRIORITY_LOW 0
78 #   define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL
79 #   define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_ABOVE_NORMAL
80 #   define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL
81
82 #else
83 #   define VLC_THREAD_PRIORITY_LOW 0
84 #   define VLC_THREAD_PRIORITY_INPUT 0
85 #   define VLC_THREAD_PRIORITY_AUDIO 0
86 #   define VLC_THREAD_PRIORITY_OUTPUT 0
87
88 #endif
89
90 /*****************************************************************************
91  * Type definitions
92  *****************************************************************************/
93
94 #if defined( PTH_INIT_IN_PTH_H )
95 typedef pth_t            vlc_thread_t;
96 typedef struct
97 {
98     pth_mutex_t mutex;
99     vlc_object_t * p_this;
100 } vlc_mutex_t;
101 typedef struct
102 {
103     pth_cond_t cond;
104     vlc_object_t * p_this;
105 } vlc_cond_t;
106
107 #elif defined( ST_INIT_IN_ST_H )
108 typedef st_thread_t *    vlc_thread_t;
109 typedef struct
110 {
111     st_mutex_t * mutex;
112     vlc_object_t * p_this;
113 } vlc_mutex_t;
114 typedef struct
115 {
116     st_cond_t * cond;
117     vlc_object_t * p_this;
118 } vlc_cond_t;
119
120 #elif defined( WIN32 )
121 typedef HANDLE vlc_thread_t;
122 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
123 typedef unsigned (__stdcall *PTHREAD_START) (void *);
124
125 typedef struct
126 {
127     /* WinNT/2K/XP implementation */
128     HANDLE              mutex;
129     /* Win95/98/ME implementation */
130     CRITICAL_SECTION    csection;
131
132     vlc_object_t * p_this;
133 } vlc_mutex_t;
134
135 typedef struct
136 {
137     volatile int        i_waiting_threads;
138     /* WinNT/2K/XP implementation */
139     HANDLE              event;
140     SIGNALOBJECTANDWAIT SignalObjectAndWait;
141     /* Win95/98/ME implementation */
142     HANDLE              semaphore;
143     CRITICAL_SECTION    csection;
144     int                 i_win9x_cv;
145
146     vlc_object_t * p_this;
147 } vlc_cond_t;
148
149 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
150 typedef pthread_t       vlc_thread_t;
151 typedef struct
152 {
153     pthread_mutex_t mutex;
154     vlc_object_t * p_this;
155 } vlc_mutex_t;
156 typedef struct
157 {
158     pthread_cond_t cond;
159     vlc_object_t * p_this;
160 } vlc_cond_t;
161
162 #elif defined( HAVE_CTHREADS_H )
163 typedef cthread_t       vlc_thread_t;
164
165 /* Those structs are the ones defined in /include/cthreads.h but we need
166  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
167  * foo is a (mutex_t*) */
168 typedef struct
169 {
170     spin_lock_t held;
171     spin_lock_t lock;
172     char *name;
173     struct cthread_queue queue;
174
175     vlc_object_t * p_this;
176 } vlc_mutex_t;
177
178 typedef struct
179 {
180     spin_lock_t lock;
181     struct cthread_queue queue;
182     char *name;
183     struct cond_imp *implications;
184
185     vlc_object_t * p_this;
186 } vlc_cond_t;
187
188 #elif defined( HAVE_KERNEL_SCHEDULER_H )
189 /* This is the BeOS implementation of the vlc threads, note that the mutex is
190  * not a real mutex and the cond_var is not like a pthread cond_var but it is
191  * enough for what wee need */
192
193 typedef thread_id vlc_thread_t;
194
195 typedef struct
196 {
197     int32           init;
198     sem_id          lock;
199
200     vlc_object_t * p_this;
201 } vlc_mutex_t;
202
203 typedef struct
204 {
205     int32           init;
206     thread_id       thread;
207
208     vlc_object_t * p_this;
209 } vlc_cond_t;
210
211 #endif
212