00001 #if SunOS4_1
00002
00003 #include "eus.h"
00004
00005
00006 thread_t thread_table[MAXTHREAD];
00007
00008 int thr_self()
00009 {
00010 int i;
00011 thread_t tid;
00012 lwp_self(&tid);
00013 for(i=0;(i<MAXTHREAD)&&(thread_table[i].thread_key != tid.thread_key);i++);
00014 return(i);
00015 }
00016
00017 int thr_getprio(int tid, int *prio)
00018 {
00019 int stat;
00020 statvec_t prio_vec;
00021 stat=lwp_getstate(thread_table[tid], &prio_vec);
00022 *prio = prio_vec.stat_prio;
00023 return(stat);
00024 }
00025
00026 int thr_setprio(int tid, int prio)
00027 {
00028 return( lwp_setpri(thread_table[tid], prio));
00029 }
00030
00031 int thr_create(void *base, size_t size, void (*func)(), void *args, long flags, int *tid)
00032 { int i, stat;
00033 for(i=0;(i<MAXTHREAD)&&(thread_table[i].thread_key >=0 );i++);
00034
00035 stat = lwp_create( &thread_table[i], func, MINPRIO, flags, lwp_newstk(), 1, args);
00036 *tid = i;
00037 return(stat);
00038 }
00039
00040 int thr_continue(int tid)
00041 {
00042 return( lwp_resume(thread_table[tid]) );
00043 }
00044
00045 int thr_suspend(int tid)
00046 {
00047 return( lwp_suspend(thread_table[tid] ));
00048 }
00049
00050 int thr_kill(int tid, int sig)
00051 {
00052 int stat;
00053 stat = lwp_destroy(thread_table[tid]);
00054 thread_table[tid].thread_key = -1;
00055 return(stat);
00056
00057 }
00058
00059
00060 int rwlock_init(rwlock_t *rw, int c, void *d)
00061 {
00062 mutex_init(&(rw->lock), 0, 0);
00063 cond_init(&(rw->r_cond), &(rw->lock));
00064 cond_init(&(rw->w_cond), &(rw->lock));
00065 rw->readers = 0;
00066 }
00067
00068 int rwlock_destroy(rwlock_t *rw)
00069 {
00070 mutex_destroy(&(rw->lock));
00071 cond_destroy(&(rw->r_cond));
00072 cond_destroy(&(rw->w_cond));
00073 }
00074
00075 int rw_rdlock(rwlock_t *rw)
00076 {
00077 mutex_lock(&(rw->lock));
00078 while (rw->readers == -1)
00079 cond_wait(&(rw->r_cond), 0);
00080 rw->readers++;
00081 mutex_unlock(&(rw->lock));
00082 }
00083
00084 int rw_wrlock(rwlock_t *rw)
00085 {
00086 mutex_lock(&(rw->lock));
00087 while (rw->readers != 0)
00088 cond_wait(&(rw->w_cond), 0);
00089 rw->readers=-1;
00090 mutex_unlock(&(rw->lock));
00091 }
00092
00093 int rw_unlock(rwlock_t *rw)
00094 {
00095 mutex_lock(&(rw->lock));
00096 if (rw->readers == -1)
00097 rw->readers = 0;
00098 else
00099 rw->readers--;
00100 cond_broadcast(&(rw->w_cond));
00101 cond_broadcast(&(rw->r_cond));
00102 mutex_unlock(&(rw->lock));
00103 }
00104
00105
00106 int sema_init(sema_t *sem, unsigned int c, int d, void *e)
00107 {
00108 mutex_init(&(sem->lock), 0, 0 );
00109 cond_init(&(sem->cond), &(sem->lock));
00110 sem->count = c;
00111 }
00112
00113 int sema_destroy(sema_t *sem)
00114 {
00115 mutex_destroy(&(sem->lock));
00116 cond_destroy(&(sem->cond));
00117 }
00118
00119 int sema_wait(sema_t *sem)
00120 {
00121 mutex_lock(&(sem->lock));
00122 while (sem->count == 0){
00123 cond_wait(&(sem->cond), &(sem->lock));}
00124 sem->count--;
00125 mutex_unlock(&(sem->lock));
00126 }
00127
00128 int sema_trywait(sema_t *sem)
00129 {
00130 int ret;
00131 mutex_lock(&(sem->lock));
00132 if (sem->count == 0)
00133 ret = EBUSY;
00134 else{
00135 sem->count--;
00136 ret = 0;
00137 }
00138 mutex_unlock(&(sem->lock));
00139 return (ret);
00140 }
00141
00142 int sema_post(sema_t *sem)
00143 {
00144 mutex_lock(&(sem->lock));
00145 sem->count++;
00146 cond_broadcast(&(sem->cond));
00147 mutex_unlock(&(sem->lock));
00148 }
00149
00150
00151 void scheduler()
00152 {
00153 struct timeval quantum;
00154 quantum.tv_sec = 0;
00155 quantum.tv_usec = 10000;
00156 for(;;){
00157 lwp_sleep(&quantum);
00158 lwp_resched(MINPRIO);
00159 }
00160 }
00161
00162 void mthread_init( context *mainctx )
00163 {
00164 int i;
00165
00166 for(i=0;i<MAXTHREAD;i++)
00167 thread_table[i].thread_key = -1;
00168 lwp_self(&thread_table[0]);
00169 mutex_init(&mark_lock, USYNC_THREAD, 0);
00170 mutex_init(&p_mark_lock, USYNC_THREAD, 0);
00171 mutex_init(&alloc_lock, USYNC_THREAD, 0);
00172 mutex_init(&free_thread_lock, USYNC_THREAD, 0);
00173 mutex_init(&qthread_lock, USYNC_THREAD, 0);
00174 mutex_init(&qsort_lock, USYNC_THREAD, 0);
00175 sema_init(&free_thread_sem, 0, USYNC_THREAD, 0);
00176 rwlock_init(&gc_lock, USYNC_THREAD, 0);
00177 pod_setmaxpri(2);
00178 lwp_setstkcache(32*1024*4, MAXTHREAD);
00179 lwp_create((thread_t *)0, scheduler, 2, 0, lwp_newstk(), 0);
00180 lwp_setpri(SELF,MINPRIO);
00181 }
00182
00183 #endif