]> granicus.if.org Git - handbrake/blob - core/Thread.c
HandBrake 0.6.0
[handbrake] / core / Thread.c
1 /* $Id: Thread.c,v 1.13 2004/03/16 16:14:03 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
9 /**********************************************************************
10  * HBThread implementation
11  **********************************************************************/
12 struct HBThread
13 {
14     /* User-friendly name */
15     char    * name;
16
17     /* HB_(LOW|NORMAL)_PRIORITY */
18     int       priority;
19
20     /* Thread function and argument */
21     void      (*function) ( void * );
22     void    * arg;
23
24     /* OS-specific thread id */
25 #if defined( HB_BEOS )
26     int       thread;
27 #elif defined( HB_MACOSX ) || defined( HB_LINUX )
28     pthread_t thread;
29 #elif defined( HB_CYGWIN )
30     HANDLE    thread;
31 #endif
32 };
33
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 )
39 {
40     HBThread * t = (HBThread*) _t;
41
42 #if defined( HB_MACOSX )
43     /* Set the thread priority */
44     struct sched_param param;
45     memset( &param, 0, sizeof( struct sched_param ) );
46     param.sched_priority = t->priority;
47     if( pthread_setschedparam( pthread_self(), SCHED_OTHER, &param ) )
48     {
49         HBLog( "HBThreadInit: couldn't set thread priority" );
50     }
51 #endif
52
53     /* Start the real routine */
54     t->function( t->arg );
55 }
56
57 HBThread * HBThreadInit( char * name, void (* function)(void *),
58                          void * arg, int priority )
59 {
60     /* Initializations */
61     HBThread * t;
62     if( !( t = malloc( sizeof( HBThread ) ) ) )
63     {
64         HBLog( "HBThreadInit: malloc() failed, gonna crash" );
65         return NULL;
66     }
67     t->name     = strdup( name );
68     t->priority = priority;
69     t->function = function;
70     t->arg      = arg;
71
72     /* Create and start the thread */
73 #if defined( HB_BEOS )
74     t->thread = spawn_thread( (int32 (*)( void * )) ThreadFunc,
75                               name, priority, t );
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 );
83 #endif
84
85     HBLog( "HBThreadInit: thread %d started (\"%s\")",
86            t->thread, t->name );
87
88     return t;
89 }
90
91 void HBThreadClose( HBThread ** _t )
92 {
93     HBThread * t = *_t;
94
95     /* Join the thread */
96 #if defined( HB_BEOS )
97     long exitValue;
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 );
103 #endif
104
105     HBLog( "HBThreadClose: thread %d stopped (\"%s\")",
106            t->thread, t->name );
107
108     /* Clean up */
109     free( t->name );
110     free( t );
111     *_t = NULL;
112 }
113
114
115 /**********************************************************************
116  * HBLock implementation
117  **********************************************************************/
118 HBLock * HBLockInit()
119 {
120     HBLock * l;
121     if( !( l = malloc( sizeof( HBLock ) ) ) )
122     {
123         HBLog( "HBLockInit: malloc() failed, gonna crash" );
124         return NULL;
125     }
126
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 );
133 #endif
134
135     return l;
136 }
137
138 void HBLockClose( HBLock ** _l )
139 {
140     HBLock * l = *_l;
141
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 );
148 #endif
149     free( l );
150
151     *_l = NULL;
152 }
153
154
155 /**********************************************************************
156  * HBCond implementation
157  **********************************************************************/
158 HBCond * HBCondInit()
159 {
160     HBCond * c = malloc( sizeof( HBCond ) );
161
162 #if defined( HB_BEOS )
163     c->thread = -1;
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 );
168 #endif
169
170     return c;
171 }
172
173 void HBCondClose( HBCond ** _c )
174 {
175     HBCond * c = *_c;
176
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 );
182 #endif
183     free( c );
184
185     *_c = NULL;
186 }
187