21 for( i = 0; i < MAXTHREAD && !pthread_equal(
thread_table[i].tid,tid); i++ )
30 struct sched_param param;
31 err = pthread_getschedparam(
thread_table[tid].tid, &policy, ¶m );
33 *prio = param.sched_priority;
42 struct sched_param param;
43 param.sched_priority = prio;
44 err = pthread_setschedparam(
thread_table[tid].tid, SCHED_RR, ¶m );
57 static void thr_cleanup(
struct thr_arg *arg )
64 static int thr_create_lock=0;
66 static void thr_startup(
struct thr_arg *arg )
71 pthread_cleanup_push((
void(*)(
void *))thr_cleanup, arg );
77 pthread_setcancel( CANCEL_ON );
78 pthread_setasynccancel( CANCEL_ON );
80 while(thr_create_lock) usleep(1000);
81 (arg->func)( arg->args );
83 pthread_cleanup_pop( 1 );
86 int thr_create(
void *base,
size_t size,
void (*func)(
void *),
void *args,
long flags,
int *tid )
94 for( i = 0; i < MAXTHREAD &&
thread_table[i].using; i++ )
99 if( (arg = (
struct thr_arg *)malloc(
sizeof(
struct thr_arg) )) ==
NULL )
106 stat = pthread_create( &
thread_table[i].tid,
NULL, (
void*(*)(
void *))thr_startup, arg );
120 pthread_mutex_t susp_the_mutex = PTHREAD_MUTEX_INITIALIZER;
121 pthread_mutex_t susp_mut = PTHREAD_MUTEX_INITIALIZER;
122 volatile int susp_sentinel = 0;
123 pthread_once_t susp_once = PTHREAD_ONCE_INIT;
124 pthread_t susp_null_pthread = {0};
125 pthread_t susp_array[MAXTHREAD];
126 int susp_bottom = MAXTHREAD;
134 suspend_signal_handler (
int sig)
141 sigfillset (&signal_set);
142 sigdelset (&signal_set, SIGUSR2);
144 sigsuspend (&signal_set);
159 resume_signal_handler (
int sig)
169 suspend_init_routine (
void)
172 struct sigaction sigusr1, sigusr2;
184 sigusr1.sa_flags = 0;
185 sigusr1.sa_handler = suspend_signal_handler;
187 sigemptyset (&sigusr1.sa_mask);
188 sigusr2.sa_flags = 0;
189 sigusr2.sa_handler = resume_signal_handler;
190 sigusr2.sa_mask = sigusr1.sa_mask;
192 status = sigaction (SIGUSR1, &sigusr1,
NULL);
194 {fprintf (stderr,
"Installing suspend handler: %s\n", strerror(
errno));
abort();}
196 status = sigaction (SIGUSR2, &sigusr2,
NULL);
198 {fprintf (stderr,
"Installing resume handler : %s\n", strerror(
errno));
abort(); }
214 pthread_suspend (pthread_t target_thread)
223 status = pthread_once (&susp_once, suspend_init_routine);
230 status = pthread_mutex_lock (&susp_mut);
240 while (i < susp_bottom)
241 if (susp_array[i++] == target_thread) {
242 status = pthread_mutex_unlock (&susp_mut);
252 while (susp_array[i] != 0)
255 if (i == susp_bottom) {
259 pthread_mutex_unlock (&susp_mut);
270 status = pthread_kill (target_thread, SIGUSR1);
272 pthread_mutex_unlock (&susp_mut);
279 while (susp_sentinel == 0)
282 susp_array[i] = target_thread;
284 status = pthread_mutex_unlock (&susp_mut);
295 pthread_continue (pthread_t target_thread)
302 status = pthread_mutex_lock (&susp_mut);
311 status = pthread_mutex_unlock (&susp_mut);
320 while (susp_array[i] != target_thread && i < susp_bottom)
323 if (i >= susp_bottom) {
324 pthread_mutex_unlock (&susp_mut);
332 status = pthread_kill (target_thread, SIGUSR2);
334 pthread_mutex_unlock (&susp_mut);
339 status = pthread_mutex_unlock (&susp_mut);
364 pthread_mutex_init(&(rwlp->
lock),
NULL);
375 pthread_mutex_destroy( &(rwlp->
lock) );
376 pthread_cond_destroy( &(rwlp->
r_cond) );
377 pthread_cond_destroy( &(rwlp->
w_cond) );
384 pthread_mutex_lock( &(rwlp->
lock) );
385 while( rwlp->
readers == (
unsigned int)-1 )
386 pthread_cond_wait( &(rwlp->
r_cond), &(rwlp->
lock) );
388 pthread_mutex_unlock( &(rwlp->
lock) );
395 pthread_mutex_lock( &(rwlp->
lock) );
397 pthread_cond_wait( &(rwlp->
w_cond), &(rwlp->
lock) );
399 pthread_mutex_unlock( &(rwlp->
lock) );
406 pthread_mutex_lock( &(rwlp->
lock) );
407 if( rwlp->
readers == (
unsigned int)-1 )
411 pthread_cond_broadcast( &(rwlp->
w_cond) );
412 pthread_cond_broadcast( &(rwlp->
r_cond) );
413 pthread_mutex_unlock( &(rwlp->
lock) );
424 pthread_mutex_init(&(sem->
lock),
NULL);
425 pthread_cond_init(&(sem->
cond),
NULL);
433 pthread_mutex_destroy(&(sem->
lock));
434 pthread_cond_destroy(&(sem->
cond));
441 pthread_mutex_lock(&(sem->
lock));
442 while (sem->
count == 0){
443 pthread_cond_wait(&(sem->
cond), &(sem->
lock));}
445 pthread_mutex_unlock(&(sem->
lock));
453 pthread_mutex_lock(&(sem->
lock));
460 pthread_mutex_unlock(&(sem->
lock));
466 pthread_mutex_lock(&(sem->
lock));
468 pthread_cond_broadcast(&(sem->
cond));
469 pthread_mutex_unlock(&(sem->
lock));
480 for( i = 0; i < MAXTHREAD; i++ )
int rwlock_destroy(rwlock_t *)
int thr_setprio(int, int)
int sema_init(sema_t *, unsigned int, int, void *)
int rwlock_init(rwlock_t *, int, void *)
int rw_wrlock(rwlock_t *)
static void abort(int sig)
struct @10 thread_table[]
int sema_destroy(sema_t *)
int thr_create(void *, size_t, void(*)(), void *, long, int *)
int rw_rdlock(rwlock_t *)
int sema_trywait(sema_t *)
int rw_unlock(rwlock_t *)
int thr_getprio(int tid, int *prio)
int sema_post(sema_t *sem)