]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
move mutex setkind hack to src/
[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 #if !defined( __LIBVLC__ )
29   #error You are not libvlc or one of its plugins. You cannot include this file
30 #endif
31
32 #ifndef _VLC_THREADS_H_
33 #define _VLC_THREADS_H_
34
35 #include <stdio.h>
36
37 #if defined( UNDER_CE )
38                                                                 /* WinCE API */
39 #elif defined( WIN32 )
40 #   include <process.h>                                         /* Win32 API */
41
42 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
43 #   include <kernel/OS.h>
44 #   include <kernel/scheduler.h>
45 #   include <byteorder.h>
46
47 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
48 #   define LIBVLC_USE_PTHREAD 1
49 #   define _APPLE_C_SOURCE    1 /* Proper pthread semantics on OSX */
50
51 #   include <unistd.h> /* _POSIX_SPIN_LOCKS */
52 #   include <pthread.h>
53     /* Needed for pthread_cond_timedwait */
54 #   include <errno.h>
55 #   ifdef DEBUG
56 #      include <time.h>
57 #   endif
58
59 #else
60 #   error no threads available on your system !
61
62 #endif
63
64 /*****************************************************************************
65  * Constants
66  *****************************************************************************/
67
68 /* Thread priorities */
69 #ifdef __APPLE__
70 #   define VLC_THREAD_PRIORITY_LOW (-47)
71 #   define VLC_THREAD_PRIORITY_INPUT 37
72 #   define VLC_THREAD_PRIORITY_AUDIO 37
73 #   define VLC_THREAD_PRIORITY_VIDEO (-47)
74 #   define VLC_THREAD_PRIORITY_OUTPUT 37
75 #   define VLC_THREAD_PRIORITY_HIGHEST 37
76
77 #elif defined(SYS_BEOS)
78 #   define VLC_THREAD_PRIORITY_LOW 5
79 #   define VLC_THREAD_PRIORITY_INPUT 10
80 #   define VLC_THREAD_PRIORITY_AUDIO 10
81 #   define VLC_THREAD_PRIORITY_VIDEO 5
82 #   define VLC_THREAD_PRIORITY_OUTPUT 15
83 #   define VLC_THREAD_PRIORITY_HIGHEST 15
84
85 #elif defined(PTHREAD_COND_T_IN_PTHREAD_H)
86 #   define VLC_THREAD_PRIORITY_LOW 0
87 #   define VLC_THREAD_PRIORITY_INPUT 20
88 #   define VLC_THREAD_PRIORITY_AUDIO 10
89 #   define VLC_THREAD_PRIORITY_VIDEO 0
90 #   define VLC_THREAD_PRIORITY_OUTPUT 30
91 #   define VLC_THREAD_PRIORITY_HIGHEST 40
92
93 #elif defined(WIN32) || defined(UNDER_CE)
94 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
95 #   define VLC_THREAD_PRIORITY_LOW 0
96 #   define VLC_THREAD_PRIORITY_INPUT \
97         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
98 #   define VLC_THREAD_PRIORITY_AUDIO \
99         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
100 #   define VLC_THREAD_PRIORITY_VIDEO \
101         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
102 #   define VLC_THREAD_PRIORITY_OUTPUT \
103         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
104 #   define VLC_THREAD_PRIORITY_HIGHEST \
105         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
106
107 #else
108 #   define VLC_THREAD_PRIORITY_LOW 0
109 #   define VLC_THREAD_PRIORITY_INPUT 0
110 #   define VLC_THREAD_PRIORITY_AUDIO 0
111 #   define VLC_THREAD_PRIORITY_VIDEO 0
112 #   define VLC_THREAD_PRIORITY_OUTPUT 0
113 #   define VLC_THREAD_PRIORITY_HIGHEST 0
114
115 #endif
116
117 /*****************************************************************************
118  * Type definitions
119  *****************************************************************************/
120
121 #if defined( WIN32 ) || defined( UNDER_CE )
122 typedef struct
123 {
124     /* thread id */
125     DWORD  id;
126     /*
127     ** handle to created thread, needs be closed to dispose of it
128     ** even after thread has exited
129     */
130     HANDLE hThread;
131 } vlc_thread_t;
132
133 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
134
135 typedef struct
136 {
137     /* WinNT/2K/XP implementation */
138     HANDLE              mutex;
139     /* Win95/98/ME implementation */
140     CRITICAL_SECTION    csection;
141
142     vlc_object_t * p_this;
143 } vlc_mutex_t;
144
145 typedef struct
146 {
147     volatile int        i_waiting_threads;
148     /* WinNT/2K/XP implementation */
149     HANDLE              event;
150     SIGNALOBJECTANDWAIT SignalObjectAndWait;
151     /* Win95/98/ME implementation */
152     HANDLE              semaphore;
153     CRITICAL_SECTION    csection;
154     int                 i_win9x_cv;
155
156     vlc_object_t * p_this;
157 } vlc_cond_t;
158
159 typedef struct
160 {
161     DWORD   handle;
162 } vlc_threadvar_t;
163
164 #elif defined( HAVE_KERNEL_SCHEDULER_H )
165 /* This is the BeOS implementation of the vlc threads, note that the mutex is
166  * not a real mutex and the cond_var is not like a pthread cond_var but it is
167  * enough for what we need */
168
169 typedef thread_id vlc_thread_t;
170
171 typedef struct
172 {
173     int32_t         init;
174     sem_id          lock;
175
176     vlc_object_t * p_this;
177 } vlc_mutex_t;
178
179 typedef struct
180 {
181     int32_t         init;
182     thread_id       thread;
183
184     vlc_object_t * p_this;
185 } vlc_cond_t;
186
187 typedef struct
188 {
189 } vlc_threadvar_t;
190
191
192 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
193 typedef pthread_t       vlc_thread_t;
194 typedef struct
195 {
196     pthread_mutex_t mutex;
197     vlc_object_t * p_this;
198 } vlc_mutex_t;
199 typedef struct
200 {
201     pthread_cond_t cond;
202     vlc_object_t * p_this;
203 } vlc_cond_t;
204
205 typedef struct
206 {
207     pthread_key_t handle;
208 } vlc_threadvar_t;
209
210 #endif
211
212 #endif /* !_VLC_THREADS_H */