]> git.sesse.net Git - vlc/blob - include/threads.h
Directory browsing and files from file list works. There is one caveat left when...
[vlc] / include / threads.h
1 /*****************************************************************************
2  * threads.h : threads implementation for the VideoLAN client
3  * This header provides a portable threads implementation.
4  *****************************************************************************
5  * Copyright (C) 1999, 2000 VideoLAN
6  * $Id: threads.h,v 1.42.2.2 2002/07/29 16:12:24 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  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 #include <stdio.h>
28
29 #if defined(GPROF) || defined(DEBUG)
30 #   include <sys/time.h>
31 #endif
32
33 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
34 #   include <pth.h>
35
36 #elif defined( ST_INIT_IN_ST_H )                            /* State threads */
37 #   include <st.h>
38
39 #elif defined( WIN32 )
40 #   include <process.h>
41
42 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
43 #   include <pthread.h>
44 #   ifdef DEBUG
45 /* Needed for pthread_cond_timedwait */
46 #       include <errno.h>
47 #   endif
48 /* This is not prototyped under Linux, though it exists. */
49 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
50
51 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
52 #   include <cthreads.h>
53
54 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
55 #   include <kernel/OS.h>
56 #   include <kernel/scheduler.h>
57 #   include <byteorder.h>
58
59 #else
60 #   error no threads available on your system !
61
62 #endif
63
64 /*****************************************************************************
65  * Constants
66  *****************************************************************************
67  * These constants are used by all threads in *_CreateThread() and
68  * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
69  * value is used as a shared flag to represent the status of the thread.
70  *****************************************************************************/
71
72 /* Void status - this value can be used to make sure no operation is currently
73  * in progress on the concerned thread in an array of recorded threads */
74 #define THREAD_NOP          0                            /* nothing happened */
75
76 /* Creation status */
77 #define THREAD_CREATE       10                     /* thread is initializing */
78 #define THREAD_START        11                          /* thread has forked */
79 #define THREAD_READY        19                            /* thread is ready */
80
81 /* Destructions status */
82 #define THREAD_DESTROY      20            /* destruction order has been sent */
83 #define THREAD_END          21        /* destruction order has been received */
84 #define THREAD_OVER         29             /* thread does not exist any more */
85
86 /* Error status */
87 #define THREAD_ERROR        30                           /* an error occured */
88 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
89
90 /*****************************************************************************
91  * Type definitions
92  *****************************************************************************/
93
94 #if defined( PTH_INIT_IN_PTH_H )
95 typedef pth_t            vlc_thread_t;
96 typedef pth_mutex_t      vlc_mutex_t;
97 typedef pth_cond_t       vlc_cond_t;
98
99 #elif defined( ST_INIT_IN_ST_H )
100 typedef st_thread_t *    vlc_thread_t;
101 typedef st_mutex_t *     vlc_mutex_t;
102 typedef st_cond_t *      vlc_cond_t;
103
104 #elif defined( WIN32 )
105 typedef HANDLE vlc_thread_t;
106
107 typedef struct
108 {
109     /* WinNT/2K/XP implementation */
110     HANDLE              mutex;
111     /* Win95/98/ME implementation */
112     CRITICAL_SECTION    csection;
113 } vlc_mutex_t;
114
115 typedef struct
116 {
117     volatile int        i_waiting_threads;
118     /* WinNT/2K/XP implementation */
119     HANDLE              event;
120     /* Win95/98/ME implementation */
121     HANDLE              semaphore;
122     CRITICAL_SECTION    csection;
123 } vlc_cond_t;
124
125 typedef unsigned (__stdcall *PTHREAD_START) (void *);
126
127 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
128 typedef pthread_t        vlc_thread_t;
129 typedef pthread_mutex_t  vlc_mutex_t;
130 typedef pthread_cond_t   vlc_cond_t;
131
132 #elif defined( HAVE_CTHREADS_H )
133 typedef cthread_t        vlc_thread_t;
134
135 /* Those structs are the ones defined in /include/cthreads.h but we need
136  * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
137  * foo is a (mutex_t*) */
138 typedef struct s_mutex {
139     spin_lock_t held;
140     spin_lock_t lock;
141     char *name;
142     struct cthread_queue queue;
143 } vlc_mutex_t;
144
145 typedef struct s_condition {
146     spin_lock_t lock;
147     struct cthread_queue queue;
148     char *name;
149     struct cond_imp *implications;
150 } vlc_cond_t;
151
152 #elif defined( HAVE_KERNEL_SCHEDULER_H )
153 /* This is the BeOS implementation of the vlc threads, note that the mutex is
154  * not a real mutex and the cond_var is not like a pthread cond_var but it is
155  * enough for what wee need */
156
157 typedef thread_id vlc_thread_t;
158
159 typedef struct
160 {
161     int32           init;
162     sem_id          lock;
163 } vlc_mutex_t;
164
165 typedef struct
166 {
167     int32           init;
168     thread_id       thread;
169 } vlc_cond_t;
170
171 #endif
172
173 typedef void *(*vlc_thread_func_t)(void *p_data);
174
175
176 /*****************************************************************************
177  * Prototype for GPROF wrapper
178  *****************************************************************************/
179
180 #ifdef GPROF
181 /* Wrapper function for profiling */
182 static void *      vlc_thread_wrapper ( void *p_wrapper );
183
184 #   ifdef WIN32
185
186 #       define ITIMER_REAL 1
187 #       define ITIMER_PROF 2
188
189 struct itimerval
190 {
191     struct timeval it_value;
192     struct timeval it_interval;
193 };
194
195 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
196
197 #   endif /* WIN32 */
198
199 typedef struct wrapper_s
200 {
201     /* Data lock access */
202     vlc_mutex_t lock;
203     vlc_cond_t  wait;
204
205     /* Data used to spawn the real thread */
206     vlc_thread_func_t func;
207     void *p_data;
208
209     /* Profiling timer passed to the thread */
210     struct itimerval itimer;
211
212 } wrapper_t;
213
214 #endif /* GPROF */