1 /* $Id: Thread.c,v 1.13 2004/03/16 16:14:03 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
9 /**********************************************************************
10 * HBThread implementation
11 **********************************************************************/
14 /* User-friendly name */
17 /* HB_(LOW|NORMAL)_PRIORITY */
20 /* Thread function and argument */
21 void (*function) ( void * );
24 /* OS-specific thread id */
25 #if defined( HB_BEOS )
27 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
29 #elif defined( HB_CYGWIN )
34 /* HBThreadInit actually starts this routine because
35 pthread_setschedparam() might fail if called from an external
36 thread (typically, because the thread exited immediatly). This isn't
37 really necessary, but I find it nicer that way */
38 static void ThreadFunc( void * _t )
40 HBThread * t = (HBThread*) _t;
42 #if defined( HB_MACOSX )
43 /* Set the thread priority */
44 struct sched_param param;
45 memset( ¶m, 0, sizeof( struct sched_param ) );
46 param.sched_priority = t->priority;
47 if( pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m ) )
49 HBLog( "HBThreadInit: couldn't set thread priority" );
53 /* Start the real routine */
54 t->function( t->arg );
57 HBThread * HBThreadInit( char * name, void (* function)(void *),
58 void * arg, int priority )
62 if( !( t = malloc( sizeof( HBThread ) ) ) )
64 HBLog( "HBThreadInit: malloc() failed, gonna crash" );
67 t->name = strdup( name );
68 t->priority = priority;
69 t->function = function;
72 /* Create and start the thread */
73 #if defined( HB_BEOS )
74 t->thread = spawn_thread( (int32 (*)( void * )) ThreadFunc,
76 resume_thread( t->thread );
77 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
78 pthread_create( &t->thread, NULL,
79 (void * (*)( void * )) ThreadFunc, t );
80 #elif defined( HB_CYGWIN )
81 t->thread = CreateThread( NULL, 0,
82 (LPTHREAD_START_ROUTINE) ThreadFunc, t, 0, NULL );
85 HBLog( "HBThreadInit: thread %d started (\"%s\")",
91 void HBThreadClose( HBThread ** _t )
96 #if defined( HB_BEOS )
98 wait_for_thread( t->thread, &exitValue );
99 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
100 pthread_join( t->thread, NULL );
101 #elif defined( HB_CYGWIN )
102 WaitForSingleObject( t->thread, INFINITE );
105 HBLog( "HBThreadClose: thread %d stopped (\"%s\")",
106 t->thread, t->name );
115 /**********************************************************************
116 * HBLock implementation
117 **********************************************************************/
118 HBLock * HBLockInit()
121 if( !( l = malloc( sizeof( HBLock ) ) ) )
123 HBLog( "HBLockInit: malloc() failed, gonna crash" );
127 #if defined( HB_BEOS )
128 l->sem = create_sem( 1, "sem" );
129 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
130 pthread_mutex_init( &l->mutex, NULL );
131 #elif defined( HB_CYGWIN )
132 l->mutex = CreateMutex( 0, FALSE, 0 );
138 void HBLockClose( HBLock ** _l )
142 #if defined( HB_BEOS )
143 delete_sem( l->sem );
144 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
145 pthread_mutex_destroy( &l->mutex );
146 #elif defined( HB_CYGWIN )
147 CloseHandle( l->mutex );
155 /**********************************************************************
156 * HBCond implementation
157 **********************************************************************/
158 HBCond * HBCondInit()
160 HBCond * c = malloc( sizeof( HBCond ) );
162 #if defined( HB_BEOS )
164 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
165 pthread_cond_init( &c->cond, NULL );
166 #elif defined( HB_CYGWIN )
167 c->event = CreateEvent( NULL, FALSE, FALSE, NULL );
173 void HBCondClose( HBCond ** _c )
177 #if defined( HB_BEOS )
178 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
179 pthread_cond_destroy( &c->cond );
180 #elif defined( HB_CYGWIN )
181 CloseHandle( c->event );