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 $
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
29 #if defined(GPROF) || defined(DEBUG)
30 # include <sys/time.h>
33 #if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */
36 #elif defined( ST_INIT_IN_ST_H ) /* State threads */
39 #elif defined( WIN32 )
42 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) /* pthreads (like Linux & BSD) */
45 /* Needed for pthread_cond_timedwait */
48 /* This is not prototyped under Linux, though it exists. */
49 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
51 #elif defined( HAVE_CTHREADS_H ) /* GNUMach */
52 # include <cthreads.h>
54 #elif defined( HAVE_KERNEL_SCHEDULER_H ) /* BeOS */
55 # include <kernel/OS.h>
56 # include <kernel/scheduler.h>
57 # include <byteorder.h>
60 # error no threads available on your system !
64 /*****************************************************************************
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 *****************************************************************************/
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 */
77 #define THREAD_CREATE 10 /* thread is initializing */
78 #define THREAD_START 11 /* thread has forked */
79 #define THREAD_READY 19 /* thread is ready */
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 */
87 #define THREAD_ERROR 30 /* an error occured */
88 #define THREAD_FATAL 31 /* an fatal error occured - program must end */
90 /*****************************************************************************
92 *****************************************************************************/
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;
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;
104 #elif defined( WIN32 )
105 typedef HANDLE vlc_thread_t;
109 CRITICAL_SECTION csection;
115 int i_waiting_threads;
119 typedef unsigned (__stdcall *PTHREAD_START) (void *);
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;
126 #elif defined( HAVE_CTHREADS_H )
127 typedef cthread_t vlc_thread_t;
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 {
136 struct cthread_queue queue;
139 typedef struct s_condition {
141 struct cthread_queue queue;
143 struct cond_imp *implications;
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 */
151 typedef thread_id vlc_thread_t;
167 typedef void *(*vlc_thread_func_t)(void *p_data);
170 /*****************************************************************************
171 * Prototype for GPROF wrapper
172 *****************************************************************************/
175 /* Wrapper function for profiling */
176 static void * vlc_thread_wrapper ( void *p_wrapper );
180 # define ITIMER_REAL 1
181 # define ITIMER_PROF 2
185 struct timeval it_value;
186 struct timeval it_interval;
189 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
193 typedef struct wrapper_s
195 /* Data lock access */
199 /* Data used to spawn the real thread */
200 vlc_thread_func_t func;
203 /* Profiling timer passed to the thread */
204 struct itimerval itimer;