]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
* src/misc/messages.c: don't try to decorate logs on win32.
[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.19 2002/12/30 17:36:01 gbazin 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 different priorities for WinNT/2K/XP and Win9x/Me */
83 #   define VLC_THREAD_PRIORITY_LOW 0
84 #   define VLC_THREAD_PRIORITY_INPUT \
85         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
86 #   define VLC_THREAD_PRIORITY_AUDIO \
87         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
88 #   define VLC_THREAD_PRIORITY_VIDEO 0
89 #   define VLC_THREAD_PRIORITY_OUTPUT \
90         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
91 #   define VLC_THREAD_PRIORITY_HIGHEST \
92         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
93
94 #else
95 #   define VLC_THREAD_PRIORITY_LOW 0
96 #   define VLC_THREAD_PRIORITY_INPUT 0
97 #   define VLC_THREAD_PRIORITY_AUDIO 0
98 #   define VLC_THREAD_PRIORITY_VIDEO 0
99 #   define VLC_THREAD_PRIORITY_OUTPUT 0
100
101 #endif
102
103 /*****************************************************************************
104  * Type definitions
105  *****************************************************************************/
106
107 #if defined( PTH_INIT_IN_PTH_H )
108 typedef pth_t            vlc_thread_t;
109 typedef struct
110 {
111     pth_mutex_t mutex;
112     vlc_object_t * p_this;
113 } vlc_mutex_t;
114 typedef struct
115 {
116     pth_cond_t cond;
117     vlc_object_t * p_this;
118 } vlc_cond_t;
119
120 #elif defined( ST_INIT_IN_ST_H )
121 typedef st_thread_t      vlc_thread_t;
122 typedef struct
123 {
124     st_mutex_t mutex;
125     vlc_object_t * p_this;
126 } vlc_mutex_t;
127 typedef struct
128 {
129     st_cond_t cond;
130     vlc_object_t * p_this;
131 } vlc_cond_t;
132
133 #elif defined( WIN32 ) || defined( UNDER_CE )
134 typedef HANDLE vlc_thread_t;
135 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
136 typedef unsigned (__stdcall *PTHREAD_START) (void *);
137
138 typedef struct
139 {
140     /* WinNT/2K/XP implementation */
141     HANDLE              mutex;
142     /* Win95/98/ME implementation */
143     CRITICAL_SECTION    csection;
144
145     vlc_object_t * p_this;
146 } vlc_mutex_t;
147
148 typedef struct
149 {
150     volatile int        i_waiting_threads;
151     /* WinNT/2K/XP implementation */
152     HANDLE              event;
153     SIGNALOBJECTANDWAIT SignalObjectAndWait;
154     /* Win95/98/ME implementation */
155     HANDLE              semaphore;
156     CRITICAL_SECTION    csection;
157     int                 i_win9x_cv;
158
159     vlc_object_t * p_this;
160 } vlc_cond_t;
161
162 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
163 typedef pthread_t       vlc_thread_t;
164 typedef struct
165 {
166     pthread_mutex_t mutex;
167     vlc_object_t * p_this;
168 } vlc_mutex_t;
169 typedef struct
170 {
171     pthread_cond_t cond;
172     vlc_object_t * p_this;
173 } vlc_cond_t;
174
175 #elif defined( HAVE_CTHREADS_H )
176 typedef cthread_t       vlc_thread_t;
177
178 /* Those structs are the ones defined in /include/cthreads.h but we need
179  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
180  * foo is a (mutex_t*) */
181 typedef struct
182 {
183     spin_lock_t held;
184     spin_lock_t lock;
185     char *name;
186     struct cthread_queue queue;
187
188     vlc_object_t * p_this;
189 } vlc_mutex_t;
190
191 typedef struct
192 {
193     spin_lock_t lock;
194     struct cthread_queue queue;
195     char *name;
196     struct cond_imp *implications;
197
198     vlc_object_t * p_this;
199 } vlc_cond_t;
200
201 #elif defined( HAVE_KERNEL_SCHEDULER_H )
202 /* This is the BeOS implementation of the vlc threads, note that the mutex is
203  * not a real mutex and the cond_var is not like a pthread cond_var but it is
204  * enough for what wee need */
205
206 typedef thread_id vlc_thread_t;
207
208 typedef struct
209 {
210     int32           init;
211     sem_id          lock;
212
213     vlc_object_t * p_this;
214 } vlc_mutex_t;
215
216 typedef struct
217 {
218     int32           init;
219     thread_id       thread;
220
221     vlc_object_t * p_this;
222 } vlc_cond_t;
223
224 #endif
225