]> git.sesse.net Git - vlc/blob - include/threads.h
modification du deco ac3 pour le rendre plus robuste.
[vlc] / include / threads.h
1 /*****************************************************************************
2  * threads.h : thread implementation for VideoLAN client
3  * This header is supposed to provide a portable threads implementation.
4  * Currently, it is a wrapper to either the POSIX pthreads library, or
5  * the Mach cthreads (for the GNU/Hurd).
6  *****************************************************************************
7  * Copyright (C) 1999, 2000 VideoLAN
8  *
9  * Authors:
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this program; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *****************************************************************************/
26
27 #ifdef SYS_GNU
28 #include <cthreads.h>
29 #else
30 #include <pthread.h>
31 #endif
32
33 /*****************************************************************************
34  * Constants
35  *****************************************************************************
36  * These constants are used by all threads in *_CreateThread() and
37  * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
38  * value is used as a shared flag to represent the status of the thread.
39  *****************************************************************************/
40
41 /* Void status - this value can be used to be sure, in an array of recorded
42  * threads, that no operation is currently in progress on the concerned thread */
43 #define THREAD_NOP          0                            /* nothing happened */
44
45 /* Creation status */
46 #define THREAD_CREATE       10                     /* thread is initializing */
47 #define THREAD_START        11                          /* thread has forked */
48 #define THREAD_READY        19                            /* thread is ready */
49
50 /* Destructions status */
51 #define THREAD_DESTROY      20            /* destruction order has been sent */
52 #define THREAD_END          21        /* destruction order has been received */
53 #define THREAD_OVER         29             /* thread does not exist any more */
54
55 /* Error status */
56 #define THREAD_ERROR        30                           /* an error occured */
57 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
58
59 /*****************************************************************************
60  * Types definition
61  *****************************************************************************/
62
63 #ifdef SYS_GNU
64
65 typedef cthread_t        vlc_thread_t;
66
67 /* those structs are the ones defined in /include/cthreads.h but we need
68  *  * to handle *foo where foo is a mutex_t */
69 typedef struct s_mutex {
70     spin_lock_t held;
71     spin_lock_t lock;
72     char *name;
73     struct cthread_queue queue;
74 } vlc_mutex_t;
75
76 typedef struct s_condition {
77     spin_lock_t lock;
78     struct cthread_queue queue;
79     char *name;
80     struct cond_imp *implications;
81 } vlc_cond_t;
82
83 #else /* SYS_GNU */
84
85 typedef pthread_t        vlc_thread_t;
86 typedef pthread_mutex_t  vlc_mutex_t;
87 typedef pthread_cond_t   vlc_cond_t;
88
89 #endif /* SYS_GNU */
90
91 typedef void *(*vlc_thread_func_t)(void *p_data);
92
93 /*****************************************************************************
94  * Prototypes
95  *****************************************************************************/
96
97 static __inline__ int  vlc_thread_create( vlc_thread_t *p_thread, char *psz_name,
98                                           vlc_thread_func_t func, void *p_data );
99 static __inline__ void vlc_thread_exit  ( void );
100 static __inline__ void vlc_thread_join  ( vlc_thread_t thread );
101
102 static __inline__ int  vlc_mutex_init   ( vlc_mutex_t *p_mutex );
103 static __inline__ int  vlc_mutex_lock   ( vlc_mutex_t *p_mutex );
104 static __inline__ int  vlc_mutex_unlock ( vlc_mutex_t *p_mutex );
105
106 static __inline__ int  vlc_cond_init    ( vlc_cond_t *p_condvar );
107 static __inline__ int  vlc_cond_signal  ( vlc_cond_t *p_condvar );
108 static __inline__ int  vlc_cond_wait    ( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex );
109
110 #if 0
111 static _inline__ int    vlc_cond_timedwait   ( vlc_cond_t * condvar, vlc_mutex_t * mutex,
112                               mtime_t absoute_timeout_time );
113 #endif
114
115 /*****************************************************************************
116  * vlc_thread_create: create a thread
117  *****************************************************************************/
118 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
119                                          char *psz_name, vlc_thread_func_t func,
120                                          void *p_data)
121 {
122 #ifdef SYS_GNU
123     *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
124     return( 0 );
125 #else
126     return pthread_create( p_thread, NULL, func, p_data );
127 #endif
128 }
129
130 /*****************************************************************************
131  * vlc_thread_exit: terminate a thread
132  *****************************************************************************/
133 static __inline__ void vlc_thread_exit( void )
134 {
135 #ifdef SYS_GNU
136     int result;
137     cthread_exit( &result );
138 #else           
139     pthread_exit( 0 );
140 #endif
141 }
142
143 /*****************************************************************************
144  * vlc_thread_join: wait until a thread exits
145  *****************************************************************************/
146 static __inline__ void vlc_thread_join( vlc_thread_t thread )
147 {
148 #ifdef SYS_GNU
149     cthread_join( thread );
150 #else   
151     pthread_join( thread, NULL );
152 #endif
153 }
154
155 /*****************************************************************************
156  * vlc_mutex_init: initialize a mutex
157  *****************************************************************************/
158 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
159 {
160 #ifdef SYS_GNU
161     mutex_init( p_mutex );
162     return( 0 );
163 #else
164     return pthread_mutex_init( p_mutex, NULL );
165 #endif
166 }
167
168 /*****************************************************************************
169  * vlc_mutex_lock: lock a mutex
170  *****************************************************************************/
171 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
172 {
173 #ifdef SYS_GNU
174     mutex_lock( p_mutex );
175     return( 0 );
176 #else
177     return pthread_mutex_lock( p_mutex );
178 #endif
179 }
180
181 /*****************************************************************************
182  * vlc_mutex_unlock: unlock a mutex
183  *****************************************************************************/
184 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
185 {
186 #ifdef SYS_GNU
187     mutex_unlock( p_mutex );
188     return( 0 );
189 #else
190     return pthread_mutex_unlock( p_mutex );
191 #endif
192 }
193
194 /*****************************************************************************
195  * vlc_cond_init: initialize a condition
196  *****************************************************************************/
197 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
198 {
199 #ifdef SYS_GNU
200     /* condition_init() */
201     spin_lock_init( &p_condvar->lock );
202     cthread_queue_init( &p_condvar->queue );
203     p_condvar->name = 0;
204     p_condvar->implications = 0;
205
206     return( 0 );
207 #else                       
208     return pthread_cond_init( p_condvar, NULL );
209 #endif
210 }
211
212 /*****************************************************************************
213  * vlc_cond_signal: start a thread on condition completion
214  *****************************************************************************/
215 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
216 {
217 #ifdef SYS_GNU
218     /* condition_signal() */
219     if ( p_condvar->queue.head || p_condvar->implications )
220     {
221         cond_signal( (condition_t)p_condvar );
222     }
223     return( 0 );
224 #else           
225     return pthread_cond_signal( p_condvar );
226 #endif
227 }
228
229 /*****************************************************************************
230  * vlc_cond_wait: wait until condition completion
231  *****************************************************************************/
232 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
233 {
234 #ifdef SYS_GNU
235     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
236     return( 0 );
237 #else
238     return pthread_cond_wait( p_condvar, p_mutex );
239 #endif
240 }
241