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