OSDN Git Service

HandBrake 0.5.2
[handbrake-jp/handbrake-jp-git.git] / core / Thread.c
1 /* $Id: Thread.c,v 1.5 2003/11/12 16:09:34 titer Exp $
2
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. */
6
7 #include "Thread.h"
8 #ifdef SYS_CYGWIN
9 #  include <windows.h>
10 #endif
11
12 struct HBThread
13 {
14     char    * name;
15     int       priority;
16     void      (*function) ( void * );
17     void    * arg;
18     
19 #if defined( SYS_BEOS )
20     int       thread;
21 #elif defined( SYS_MACOSX ) || defined( SYS_LINUX )
22     pthread_t thread;
23 #elif defined( SYS_CYGWIN )
24     HANDLE    thread;
25 #endif
26 };
27
28 static void ThreadFunc( void * t );
29
30 HBThread * HBThreadInit( char * name, void (* function)(void *),
31                          void * arg, int priority )
32 {
33     HBThread * t;
34     if( !( t = malloc( sizeof( HBThread ) ) ) )
35     {
36         HBLog( "HBThreadInit: malloc() failed, gonna crash" );
37         return NULL;
38     }
39
40     t->name     = strdup( name );
41     t->priority = priority;
42     t->function = function;
43     t->arg      = arg;
44
45 #if defined( SYS_BEOS )
46     t->thread = spawn_thread( (int32 (*)( void * )) ThreadFunc,
47                               name, priority, t );
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 );
55 #endif
56
57     HBLog( "HBThreadInit: thread %d started (\"%s\")",
58            t->thread, t->name );
59
60     return t;
61 }
62
63 static void ThreadFunc( void * _t )
64 {
65     HBThread * t = (HBThread*) _t;
66
67 #if defined( SYS_MACOSX )
68     struct sched_param param;
69     memset( &param, 0, sizeof( struct sched_param ) );
70     param.sched_priority = t->priority;
71     if( pthread_setschedparam( pthread_self(), SCHED_OTHER, &param ) )
72     {
73         HBLog( "HBThreadInit: couldn't set thread priority" );
74     }
75 #endif
76
77     t->function( t->arg );
78 }
79
80 void HBThreadClose( HBThread ** _t )
81 {
82     HBThread * t = *_t;
83     
84 #if defined( SYS_BEOS )
85     long exitValue;
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 );
91 #endif
92
93     HBLog( "HBThreadClose: thread %d stopped (\"%s\")",
94            t->thread, t->name );
95
96     free( t->name );
97     free( t );
98     *_t = NULL;
99 }
100
101 HBLock * HBLockInit()
102 {
103     HBLock * l;
104     if( !( l = malloc( sizeof( HBLock ) ) ) )
105     {
106         HBLog( "HBLockInit: malloc() failed, gonna crash" );
107         return NULL;
108     }
109
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 )
115     /* TODO */
116 #endif
117
118     return l;
119 }
120
121 void HBLockClose( HBLock ** _l )
122 {
123     HBLock * l = *_l;
124     
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 )
130     /* TODO */
131 #endif
132     free( l );
133
134     *_l = NULL;
135 }
136