1 /* $Id: Thread.c,v 1.5 2003/11/12 16:09:34 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. */
16 void (*function) ( void * );
19 #if defined( SYS_BEOS )
21 #elif defined( SYS_MACOSX ) || defined( SYS_LINUX )
23 #elif defined( SYS_CYGWIN )
28 static void ThreadFunc( void * t );
30 HBThread * HBThreadInit( char * name, void (* function)(void *),
31 void * arg, int priority )
34 if( !( t = malloc( sizeof( HBThread ) ) ) )
36 HBLog( "HBThreadInit: malloc() failed, gonna crash" );
40 t->name = strdup( name );
41 t->priority = priority;
42 t->function = function;
45 #if defined( SYS_BEOS )
46 t->thread = spawn_thread( (int32 (*)( void * )) ThreadFunc,
48 resume_thread( t->thread );
49 #elif defined( SYS_MACOSX ) || defined( SYS_LINUX )
50 pthread_create( &t->thread, NULL,
51 (void * (*)( void * )) ThreadFunc, t );
52 #elif defined( SYS_CYGWIN )
53 t->thread = CreateThread( NULL, 0,
54 (LPTHREAD_START_ROUTINE) ThreadFunc, t, 0, NULL );
57 HBLog( "HBThreadInit: thread %d started (\"%s\")",
63 static void ThreadFunc( void * _t )
65 HBThread * t = (HBThread*) _t;
67 #if defined( SYS_MACOSX )
68 struct sched_param param;
69 memset( ¶m, 0, sizeof( struct sched_param ) );
70 param.sched_priority = t->priority;
71 if( pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m ) )
73 HBLog( "HBThreadInit: couldn't set thread priority" );
77 t->function( t->arg );
80 void HBThreadClose( HBThread ** _t )
84 #if defined( SYS_BEOS )
86 wait_for_thread( t->thread, &exitValue );
87 #elif defined( SYS_MACOSX ) || defined( SYS_LINUX )
88 pthread_join( t->thread, NULL );
89 #elif defined( SYS_CYGWIN )
90 WaitForSingleObject( t->thread, INFINITE );
93 HBLog( "HBThreadClose: thread %d stopped (\"%s\")",
101 HBLock * HBLockInit()
104 if( !( l = malloc( sizeof( HBLock ) ) ) )
106 HBLog( "HBLockInit: malloc() failed, gonna crash" );
110 #if defined( SYS_BEOS )
111 l->sem = create_sem( 1, "sem" );
112 #elif defined( SYS_MACOSX ) || defined( SYS_LINUX )
113 pthread_mutex_init( &l->mutex, NULL );
114 #elif defined( SYS_CYGWIN )
121 void HBLockClose( HBLock ** _l )
125 #if defined( SYS_BEOS )
126 delete_sem( l->sem );
127 #elif defined( SYS_MACOSX ) || defined( SYS_LINUX )
128 pthread_mutex_destroy( &l->mutex );
129 #elif defined( SYS_CYGWIN )