vlc_threads.h 6.95 KB
Newer Older
1
/*****************************************************************************
2
3
 * vlc_threads.h : threads implementation for the VideoLAN client
 * This header provides portable declarations for mutexes & conditions
4
 *****************************************************************************
5
 * Copyright (C) 1999, 2002 VideoLAN
gbazin's avatar
gbazin committed
6
 * $Id$
7
 *
8
9
 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
 *          Samuel Hocevar <sam@via.ecp.fr>
gbazin's avatar
   
gbazin committed
10
 *          Gildas Bazin <gbazin@netcourrier.com>
11
 *          Christophe Massiot <massiot@via.ecp.fr>
12
13
14
15
16
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
17
 *
18
19
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
22
 *
23
24
25
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26
 *****************************************************************************/
27

28
29
#include <stdio.h>

30
31
#if defined(DEBUG) && defined(HAVE_SYS_TIME_H)
#   include <sys/time.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
32
33
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
34
35
#if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
#   include <pth.h>
36

Sam Hocevar's avatar
   
Sam Hocevar committed
37
38
39
#elif defined( ST_INIT_IN_ST_H )                            /* State threads */
#   include <st.h>

40
#elif defined( UNDER_CE )
41
                                                                /* WinCE API */
42
43
#elif defined( WIN32 )
#   include <process.h>                                         /* Win32 API */
Sam Hocevar's avatar
   
Sam Hocevar committed
44

45
46
47
48
49
#elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
#   include <kernel/OS.h>
#   include <kernel/scheduler.h>
#   include <byteorder.h>

Sam Hocevar's avatar
   
Sam Hocevar committed
50
51
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
#   include <pthread.h>
Christophe Massiot's avatar
Christophe Massiot committed
52
#   ifdef DEBUG
53
        /* Needed for pthread_cond_timedwait */
Christophe Massiot's avatar
Christophe Massiot committed
54
55
#       include <errno.h>
#   endif
56
57
    /* This is not prototyped under Linux, though it exists. */
    int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
58

Sam Hocevar's avatar
   
Sam Hocevar committed
59
60
#elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
#   include <cthreads.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
61

62
#else
Sam Hocevar's avatar
   
Sam Hocevar committed
63
64
#   error no threads available on your system !

65
#endif
66

67
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
68
 * Constants
69
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
70

71
72
/* Thread priorities */
#ifdef SYS_DARWIN
73
74
75
76
77
#   define VLC_THREAD_PRIORITY_LOW (-47)
#   define VLC_THREAD_PRIORITY_INPUT 37
#   define VLC_THREAD_PRIORITY_AUDIO 37
#   define VLC_THREAD_PRIORITY_VIDEO (-47)
#   define VLC_THREAD_PRIORITY_OUTPUT 37
78
#   define VLC_THREAD_PRIORITY_HIGHEST 37
Vincent Seguin's avatar
Vincent Seguin committed
79

80
81
82
83
84
85
#elif defined(SYS_BEOS)
#   define VLC_THREAD_PRIORITY_LOW 5
#   define VLC_THREAD_PRIORITY_INPUT 10
#   define VLC_THREAD_PRIORITY_AUDIO 10
#   define VLC_THREAD_PRIORITY_VIDEO 5
#   define VLC_THREAD_PRIORITY_OUTPUT 15
86
#   define VLC_THREAD_PRIORITY_HIGHEST 15
87

88
89
90
91
92
93
#elif defined(PTHREAD_COND_T_IN_PTHREAD_H)
#   define VLC_THREAD_PRIORITY_LOW 0
#   define VLC_THREAD_PRIORITY_INPUT 20
#   define VLC_THREAD_PRIORITY_AUDIO 10
#   define VLC_THREAD_PRIORITY_VIDEO 0
#   define VLC_THREAD_PRIORITY_OUTPUT 30
94
#   define VLC_THREAD_PRIORITY_HIGHEST 40
95

96
#elif defined(WIN32) || defined(UNDER_CE)
gbazin's avatar
   
gbazin committed
97
/* Define different priorities for WinNT/2K/XP and Win9x/Me */
98
#   define VLC_THREAD_PRIORITY_LOW 0
gbazin's avatar
   
gbazin committed
99
#   define VLC_THREAD_PRIORITY_INPUT \
gbazin's avatar
gbazin committed
100
        (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
gbazin's avatar
   
gbazin committed
101
102
#   define VLC_THREAD_PRIORITY_AUDIO \
        (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
gbazin's avatar
   
gbazin committed
103
104
#   define VLC_THREAD_PRIORITY_VIDEO \
        (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
gbazin's avatar
   
gbazin committed
105
106
107
108
#   define VLC_THREAD_PRIORITY_OUTPUT \
        (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
#   define VLC_THREAD_PRIORITY_HIGHEST \
        (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
109

110
111
112
113
#else
#   define VLC_THREAD_PRIORITY_LOW 0
#   define VLC_THREAD_PRIORITY_INPUT 0
#   define VLC_THREAD_PRIORITY_AUDIO 0
114
#   define VLC_THREAD_PRIORITY_VIDEO 0
115
#   define VLC_THREAD_PRIORITY_OUTPUT 0
116
#   define VLC_THREAD_PRIORITY_HIGHEST 0
Vincent Seguin's avatar
Vincent Seguin committed
117

118
#endif
Vincent Seguin's avatar
Vincent Seguin committed
119

120
/*****************************************************************************
gbazin's avatar
   
gbazin committed
121
 * Type definitions
122
 *****************************************************************************/
123

Sam Hocevar's avatar
   
Sam Hocevar committed
124
125
#if defined( PTH_INIT_IN_PTH_H )
typedef pth_t            vlc_thread_t;
126
127
128
129
130
131
132
133
134
135
typedef struct
{
    pth_mutex_t mutex;
    vlc_object_t * p_this;
} vlc_mutex_t;
typedef struct
{
    pth_cond_t cond;
    vlc_object_t * p_this;
} vlc_cond_t;
Sam Hocevar's avatar
   
Sam Hocevar committed
136

Sam Hocevar's avatar
   
Sam Hocevar committed
137
#elif defined( ST_INIT_IN_ST_H )
138
typedef st_thread_t      vlc_thread_t;
139
140
typedef struct
{
141
    st_mutex_t mutex;
142
143
144
145
    vlc_object_t * p_this;
} vlc_mutex_t;
typedef struct
{
146
    st_cond_t cond;
147
148
    vlc_object_t * p_this;
} vlc_cond_t;
Sam Hocevar's avatar
   
Sam Hocevar committed
149

150
#elif defined( WIN32 ) || defined( UNDER_CE )
Sam Hocevar's avatar
   
Sam Hocevar committed
151
typedef HANDLE vlc_thread_t;
gbazin's avatar
   
gbazin committed
152
typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
gbazin's avatar
gbazin committed
153
typedef unsigned (WINAPI *PTHREAD_START) (void *);
Sam Hocevar's avatar
   
Sam Hocevar committed
154
155
156

typedef struct
{
157
    /* WinNT/2K/XP implementation */
158
    HANDLE              mutex;
159
160
    /* Win95/98/ME implementation */
    CRITICAL_SECTION    csection;
161
162

    vlc_object_t * p_this;
Sam Hocevar's avatar
   
Sam Hocevar committed
163
164
165
166
} vlc_mutex_t;

typedef struct
{
gbazin's avatar
   
gbazin committed
167
    volatile int        i_waiting_threads;
168
    /* WinNT/2K/XP implementation */
gbazin's avatar
   
gbazin committed
169
    HANDLE              event;
170
171
    SIGNALOBJECTANDWAIT SignalObjectAndWait;
    /* Win95/98/ME implementation */
gbazin's avatar
   
gbazin committed
172
173
174
    HANDLE              semaphore;
    CRITICAL_SECTION    csection;
    int                 i_win9x_cv;
175
176

    vlc_object_t * p_this;
Sam Hocevar's avatar
   
Sam Hocevar committed
177
178
} vlc_cond_t;

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#elif defined( HAVE_KERNEL_SCHEDULER_H )
/* This is the BeOS implementation of the vlc threads, note that the mutex is
 * not a real mutex and the cond_var is not like a pthread cond_var but it is
 * enough for what wee need */

typedef thread_id vlc_thread_t;

typedef struct
{
    int32_t         init;
    sem_id          lock;

    vlc_object_t * p_this;
} vlc_mutex_t;

typedef struct
{
    int32_t         init;
    thread_id       thread;

    vlc_object_t * p_this;
} vlc_cond_t;

Sam Hocevar's avatar
   
Sam Hocevar committed
202
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
203
204
205
206
207
208
209
210
211
212
213
typedef pthread_t       vlc_thread_t;
typedef struct
{
    pthread_mutex_t mutex;
    vlc_object_t * p_this;
} vlc_mutex_t;
typedef struct
{
    pthread_cond_t cond;
    vlc_object_t * p_this;
} vlc_cond_t;
Henri Fallon's avatar
   
Henri Fallon committed
214

Sam Hocevar's avatar
   
Sam Hocevar committed
215
#elif defined( HAVE_CTHREADS_H )
216
typedef cthread_t       vlc_thread_t;
217

Sam Hocevar's avatar
   
Sam Hocevar committed
218
/* Those structs are the ones defined in /include/cthreads.h but we need
219
 * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
220
 * foo is a (mutex_t*) */
221
222
typedef struct
{
223
224
225
226
    spin_lock_t held;
    spin_lock_t lock;
    char *name;
    struct cthread_queue queue;
227
228

    vlc_object_t * p_this;
229
230
} vlc_mutex_t;

231
232
typedef struct
{
233
234
235
236
    spin_lock_t lock;
    struct cthread_queue queue;
    char *name;
    struct cond_imp *implications;
237
238

    vlc_object_t * p_this;
239
240
} vlc_cond_t;

gbazin's avatar
   
gbazin committed
241
242
#endif