1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/* $Id: Thread.c,v 1.10 2004/01/14 21:37:25 titer Exp $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.m0k.org/>.
It may be used under the terms of the GNU General Public License. */
#include "Thread.h"
struct HBThread
{
char * name;
int priority;
void (*function) ( void * );
void * arg;
#if defined( HB_BEOS )
int thread;
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
pthread_t thread;
#elif defined( HB_CYGWIN )
HANDLE thread;
#endif
};
static void ThreadFunc( void * t );
HBThread * HBThreadInit( char * name, void (* function)(void *),
void * arg, int priority )
{
HBThread * t;
if( !( t = malloc( sizeof( HBThread ) ) ) )
{
HBLog( "HBThreadInit: malloc() failed, gonna crash" );
return NULL;
}
t->name = strdup( name );
t->priority = priority;
t->function = function;
t->arg = arg;
#if defined( HB_BEOS )
t->thread = spawn_thread( (int32 (*)( void * )) ThreadFunc,
name, priority, t );
resume_thread( t->thread );
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
pthread_create( &t->thread, NULL,
(void * (*)( void * )) ThreadFunc, t );
#elif defined( HB_CYGWIN )
t->thread = CreateThread( NULL, 0,
(LPTHREAD_START_ROUTINE) ThreadFunc, t, 0, NULL );
#endif
HBLog( "HBThreadInit: thread %d started (\"%s\")",
t->thread, t->name );
return t;
}
static void ThreadFunc( void * _t )
{
HBThread * t = (HBThread*) _t;
#if defined( HB_MACOSX )
struct sched_param param;
memset( ¶m, 0, sizeof( struct sched_param ) );
param.sched_priority = t->priority;
if( pthread_setschedparam( pthread_self(), SCHED_OTHER, ¶m ) )
{
HBLog( "HBThreadInit: couldn't set thread priority" );
}
#endif
t->function( t->arg );
}
void HBThreadClose( HBThread ** _t )
{
HBThread * t = *_t;
#if defined( HB_BEOS )
long exitValue;
wait_for_thread( t->thread, &exitValue );
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
pthread_join( t->thread, NULL );
#elif defined( HB_CYGWIN )
WaitForSingleObject( t->thread, INFINITE );
#endif
HBLog( "HBThreadClose: thread %d stopped (\"%s\")",
t->thread, t->name );
free( t->name );
free( t );
*_t = NULL;
}
HBLock * HBLockInit()
{
HBLock * l;
if( !( l = malloc( sizeof( HBLock ) ) ) )
{
HBLog( "HBLockInit: malloc() failed, gonna crash" );
return NULL;
}
#if defined( HB_BEOS )
l->sem = create_sem( 1, "sem" );
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
pthread_mutex_init( &l->mutex, NULL );
#elif defined( HB_CYGWIN )
l->mutex = CreateMutex( 0, FALSE, 0 );
#endif
return l;
}
void HBLockClose( HBLock ** _l )
{
HBLock * l = *_l;
#if defined( HB_BEOS )
delete_sem( l->sem );
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
pthread_mutex_destroy( &l->mutex );
#elif defined( HB_CYGWIN )
CloseHandle( l->mutex );
#endif
free( l );
*_l = NULL;
}
HBCond * HBCondInit()
{
HBCond * c = malloc( sizeof( HBCond ) );
#if defined( HB_BEOS )
c->thread = -1;
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
pthread_cond_init( &c->cond, NULL );
#elif defined( HB_CYGWIN )
/* TODO */
#endif
return c;
}
void HBCondClose( HBCond ** _c )
{
HBCond * c = *_c;
#if defined( HB_BEOS )
#elif defined( HB_MACOSX ) || defined( HB_LINUX )
pthread_cond_destroy( &c->cond );
#elif defined( HB_CYGWIN )
/* TODO */
#endif
free( c );
*_c = NULL;
}
|