]> git.sesse.net Git - vlc/blob - include/threads.h
* moved the function definitions from threads.h into threads_funcs.h. This
[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 2002/04/27 22:11:22 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     CRITICAL_SECTION csection;
110     HANDLE           mutex;
111 } vlc_mutex_t;
112
113 typedef struct
114 {
115     int             i_waiting_threads;
116     HANDLE          signal;
117 } vlc_cond_t;
118
119 typedef unsigned (__stdcall *PTHREAD_START) (void *);
120
121 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
122 typedef pthread_t        vlc_thread_t;
123 typedef pthread_mutex_t  vlc_mutex_t;
124 typedef pthread_cond_t   vlc_cond_t;
125
126 #elif defined( HAVE_CTHREADS_H )
127 typedef cthread_t        vlc_thread_t;
128
129 /* Those structs are the ones defined in /include/cthreads.h but we need
130  * to handle (*foo) where foo is a (mutex_t) while they handle (foo) where
131  * foo is a (mutex_t*) */
132 typedef struct s_mutex {
133     spin_lock_t held;
134     spin_lock_t lock;
135     char *name;
136     struct cthread_queue queue;
137 } vlc_mutex_t;
138
139 typedef struct s_condition {
140     spin_lock_t lock;
141     struct cthread_queue queue;
142     char *name;
143     struct cond_imp *implications;
144 } vlc_cond_t;
145
146 #elif defined( HAVE_KERNEL_SCHEDULER_H )
147 /* This is the BeOS implementation of the vlc threads, note that the mutex is
148  * not a real mutex and the cond_var is not like a pthread cond_var but it is
149  * enough for what wee need */
150
151 typedef thread_id vlc_thread_t;
152
153 typedef struct
154 {
155     int32           init;
156     sem_id          lock;
157 } vlc_mutex_t;
158
159 typedef struct
160 {
161     int32           init;
162     thread_id       thread;
163 } vlc_cond_t;
164
165 #endif
166
167 typedef void *(*vlc_thread_func_t)(void *p_data);
168
169
170 /*****************************************************************************
171  * Prototype for GPROF wrapper
172  *****************************************************************************/
173
174 #ifdef GPROF
175 /* Wrapper function for profiling */
176 static void *      vlc_thread_wrapper ( void *p_wrapper );
177
178 #   ifdef WIN32
179
180 #       define ITIMER_REAL 1
181 #       define ITIMER_PROF 2
182
183 struct itimerval
184 {
185     struct timeval it_value;
186     struct timeval it_interval;
187 };
188
189 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
190
191 #   endif /* WIN32 */
192
193 typedef struct wrapper_s
194 {
195     /* Data lock access */
196     vlc_mutex_t lock;
197     vlc_cond_t  wait;
198
199     /* Data used to spawn the real thread */
200     vlc_thread_func_t func;
201     void *p_data;
202
203     /* Profiling timer passed to the thread */
204     struct itimerval itimer;
205
206 } wrapper_t;
207
208 #endif /* GPROF */