Go to the documentation of this file.00001 #if alpha
00002 #include "eus.h"
00003
00004
00005
00006
00007
00008
00009 static struct {
00010 int using;
00011 thread_t tid;
00012 } thread_table[MAXTHREAD];
00013
00014 int thr_self()
00015 {
00016 int i;
00017 thread_t tid;
00018
00019 tid = pthread_self();
00020 for( i = 0; i < MAXTHREAD && !pthread_equal(thread_table[i].tid,tid); i++ )
00021 ;
00022
00023 return( i );
00024 }
00025
00026 int thr_getprio( int tid, int *prio )
00027 {
00028 *prio = pthread_getprio( thread_table[tid].tid );
00029 if( *prio == -1 )
00030 return errno;
00031 else
00032 return ESUCCESS;
00033 }
00034
00035 int thr_setprio(int tid, int prio)
00036 {
00037 if( pthread_setprio( thread_table[tid].tid, prio ) == -1 )
00038 return errno;
00039 else
00040 return ESUCCESS;
00041 }
00042
00043 struct thr_arg {
00044 int tid;
00045 void (*func)();
00046 void *args;
00047 };
00048
00049 static void thr_cleanup( struct thr_arg *arg )
00050 {
00051 thread_table[arg->tid].using = 0;
00052
00053 free(arg);
00054 }
00055
00056 static void thr_startup( struct thr_arg *arg )
00057 {
00058 printf( "thr_startup:tid=%d\n", arg->tid );
00059
00060 pthread_cleanup_push( thr_cleanup, arg );
00061
00062 pthread_setcancel( CANCEL_ON );
00063 pthread_setasynccancel( CANCEL_ON );
00064
00065 (arg->func)( arg->args );
00066
00067 pthread_cleanup_pop( 1 );
00068 }
00069
00070 int thr_create(void *base, size_t size, void (*func)(), void *args, long flags, int *tid )
00071
00072
00073
00074 {
00075 int i, stat;
00076 struct thr_arg *arg;
00077
00078 for( i = 0; i < MAXTHREAD && thread_table[i].using; i++ )
00079 ;
00080 if( i >= MAXTHREAD )
00081 return -1;
00082
00083 if( (arg = (struct thr_arg *)malloc( sizeof(struct thr_arg) )) == NULL )
00084 return -1;
00085
00086 arg->tid = i;
00087 arg->func = func;
00088 arg->args = args;
00089 stat = pthread_create( &thread_table[i].tid, pthread_attr_default, (pthread_startroutine_t)thr_startup, (pthread_addr_t)arg );
00090 if( stat == 0 )
00091 thread_table[i].using = 1;
00092 *tid = i;
00093 return( stat );
00094 }
00095
00096
00097
00098
00099
00100 int thr_suspend( int tid ) { return -1; }
00101 int thr_continue( int tid ) { return -1; }
00102
00103 int thr_kill( int tid, int sig )
00104
00105 {
00106 if( pthread_cancel( thread_table[tid].tid ) < 0 )
00107 return errno;
00108 else
00109 return ESUCCESS;
00110 }
00111
00112
00113 int rwlock_init(rwlock_t *rwlp, int type, void *arg)
00114 {
00115 pthread_mutex_init(&(rwlp->lock), pthread_mutexattr_default);
00116 pthread_cond_init(&(rwlp->r_cond), pthread_condattr_default);
00117 pthread_cond_init(&(rwlp->w_cond), pthread_condattr_default);
00118
00119 rwlp->readers = 0;
00120
00121 return 0;
00122 }
00123
00124 int rwlock_destroy( rwlock_t *rwlp )
00125 {
00126 pthread_mutex_destroy( &(rwlp->lock) );
00127 pthread_cond_destroy( &(rwlp->r_cond) );
00128 pthread_cond_destroy( &(rwlp->w_cond) );
00129
00130 return 0;
00131 }
00132
00133 int rw_rdlock( rwlock_t *rwlp )
00134 {
00135 pthread_mutex_lock( &(rwlp->lock) );
00136 while( rwlp->readers == -1 )
00137 pthread_cond_wait( &(rwlp->r_cond), &(rwlp->lock) );
00138 rwlp->readers++;
00139 pthread_mutex_unlock( &(rwlp->lock) );
00140
00141 return 0;
00142 }
00143
00144 int rw_wrlock( rwlock_t *rwlp )
00145 {
00146 pthread_mutex_lock( &(rwlp->lock) );
00147 while( rwlp->readers != 0 )
00148 pthread_cond_wait( &(rwlp->w_cond), &(rwlp->lock) );
00149 rwlp->readers=-1;
00150 pthread_mutex_unlock( &(rwlp->lock) );
00151
00152 return 0;
00153 }
00154
00155 int rw_unlock( rwlock_t *rwlp )
00156 {
00157 pthread_mutex_lock( &(rwlp->lock) );
00158 if( rwlp->readers == -1 )
00159 rwlp->readers = 0;
00160 else
00161 rwlp->readers--;
00162 pthread_cond_broadcast( &(rwlp->w_cond) );
00163 pthread_cond_broadcast( &(rwlp->r_cond) );
00164 pthread_mutex_unlock( &(rwlp->lock) );
00165
00166 return 0;
00167 }
00168
00169
00170 int sema_init(sema_t *sem, unsigned int c, int d, void *e)
00171
00172
00173
00174 {
00175 pthread_mutex_init(&(sem->lock), pthread_mutexattr_default );
00176 pthread_cond_init(&(sem->cond), pthread_condattr_default );
00177 sem->count = 0;
00178
00179 return 0;
00180 }
00181
00182 int sema_destroy(sema_t *sem)
00183 {
00184 pthread_mutex_destroy(&(sem->lock));
00185 pthread_cond_destroy(&(sem->cond));
00186
00187 return 0;
00188 }
00189
00190 int sema_wait(sema_t *sem)
00191 {
00192 pthread_mutex_lock(&(sem->lock));
00193 while (sem->count == 0){
00194 pthread_cond_wait(&(sem->cond), &(sem->lock));}
00195 sem->count--;
00196 pthread_mutex_unlock(&(sem->lock));
00197
00198 return 0;
00199 }
00200
00201 int sema_trywait(sema_t *sem)
00202 {
00203 int ret;
00204 pthread_mutex_lock(&(sem->lock));
00205 if (sem->count == 0)
00206 ret = EBUSY;
00207 else{
00208 sem->count--;
00209 ret = 0;
00210 }
00211 pthread_mutex_unlock(&(sem->lock));
00212 return (ret);
00213 }
00214
00215 int sema_post(sema_t *sem)
00216 {
00217 pthread_mutex_lock(&(sem->lock));
00218 sem->count++;
00219 pthread_cond_broadcast(&(sem->cond));
00220 pthread_mutex_unlock(&(sem->lock));
00221
00222 return 0;
00223 }
00224
00225
00226 void mthread_init( context *mainctx )
00227 {
00228 int i;
00229 pthread_t tid;
00230
00231 for( i = 0; i < MAXTHREAD; i++ )
00232 thread_table[i].using = 0;
00233
00234 thread_table[0].tid = pthread_self();
00235 thread_table[0].using = 1;
00236
00237 pthread_mutex_init(&p_mark_lock, pthread_mutexattr_default );
00238 pthread_mutex_init(&alloc_lock, pthread_mutexattr_default );
00239 pthread_mutex_init(&free_thread_lock, pthread_mutexattr_default );
00240 pthread_mutex_init(&qthread_lock, pthread_mutexattr_default );
00241 pthread_mutex_init(&qsort_lock, pthread_mutexattr_default );
00242 sema_init(&free_thread_sem, 0, 0, 0);
00243 rwlock_init(&gc_lock, 0, 0);
00244 }
00245
00246
00247 #endif