]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
* ALL: the first libvlc commit.
[vlc] / include / vlc_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: vlc_threads.h,v 1.1 2002/06/01 12:31:58 sam 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 )                                          /* Win32 API */
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     SIGNALOBJECTANDWAIT SignalObjectAndWait;
112 } vlc_mutex_t;
113
114 typedef struct
115 {
116     int             i_waiting_threads;
117     HANDLE          signal;
118 } vlc_cond_t;
119
120 typedef unsigned (__stdcall *PTHREAD_START) (void *);
121
122 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
123 typedef pthread_t        vlc_thread_t;
124 typedef pthread_mutex_t  vlc_mutex_t;
125 typedef pthread_cond_t   vlc_cond_t;
126
127 #elif defined( HAVE_CTHREADS_H )
128 typedef cthread_t        vlc_thread_t;
129
130 /* Those structs are the ones defined in /include/cthreads.h but we need
131  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
132  * foo is a (mutex_t*) */
133 typedef struct
134 {
135     spin_lock_t held;
136     spin_lock_t lock;
137     char *name;
138     struct cthread_queue queue;
139 } vlc_mutex_t;
140
141 typedef struct
142 {
143     spin_lock_t lock;
144     struct cthread_queue queue;
145     char *name;
146     struct cond_imp *implications;
147 } vlc_cond_t;
148
149 #elif defined( HAVE_KERNEL_SCHEDULER_H )
150 /* This is the BeOS implementation of the vlc threads, note that the mutex is
151  * not a real mutex and the cond_var is not like a pthread cond_var but it is
152  * enough for what wee need */
153
154 typedef thread_id vlc_thread_t;
155
156 typedef struct
157 {
158     int32           init;
159     sem_id          lock;
160 } vlc_mutex_t;
161
162 typedef struct
163 {
164     int32           init;
165     thread_id       thread;
166 } vlc_cond_t;
167
168 #endif
169