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