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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
POSIX thread functions -> Solaris mthread functions.
writtten by I.Hara
*/
#include "eus.h"
#include <errno.h>
/* thread function for POSIX added by I.Hara */
pthread_t thread_table[MAXTHREAD][2];
int thr_self()
{
int i;
pthread_t thr_t;
thr_t = pthread_self();
for(i=0;(i<MAXTHREAD)&&(thread_table[i][0] != thr_t);i++);
return(i);
}
int thr_getprio(int tid, int *prio)
{
if (thread_table[tid][1])
*prio = thread_table[tid][0]->attr.prio;
return(0);
else
return (-1);
}
int thr_setprio(int tid, int prio)
{
int stat;
if (!thread_table[tid][1]) return(-1);
else{
if ((piro >= PTHREAD_MIN_PRIORITY) &&
(piro =< PTHREAD_MAX_PRIORITY))
thread_table[tid][0]->attr.prio = prio;
return(0);
else return(EINVAL); }
}
int thr_create(void *base, size_t size, void (*func)(),
void *args, long flags, int *tid)
{ int i, stat;
pthread_attrt th_attr;
for(i=0;(i<MAXTHREAD) && thread_table[i][1] ;i++);
thread_table[i][0] = 1;
pthread_attr_init(&th_attr);
if (flag != 0)
fprintf(stderr, "Warning in thr_create(): flags doesn't support!\n");
if (size >= PTHREAD_STACK_MIN) pthread_attr_setstacksize(&th_attr, size);
stat = pthread_create( &thread_table[i], th_attr, func, args);
*tid = i;
return(stat);
}
int thr_continue(int tid)
{
fprintf(stderr, "Error in thr_continue: POSIX thread doesn't supported.\n");
return(-1);
}
int thr_suspend(int tid)
{
fprintf(stderr, "Error in thr_suspend: POSIX thread doesn't supported.\n");
return(-1);
}
int thr_kill(int tid, int sig)
{
return(pthread_kill(thread_table[tid][0], sig));
}
int thr_join(int tid, int *depature, void **status)
{
if ( *depature != NULL){
fprintf(stderr,"Warrning in thr_join: argument 'depature' must be NULL.\n");
*depature == -1;
}
return(pthread_join(thread_table[tid][0], status));
}
/* mutex functions with POSIX thread, added by I.Hara
int mutex_init(mutex_t *mp, int type, void * arg);
int mutex_destroy(mutex_t *mp);
int mutex_lock(mutex_t *mp);
int mutex_trylock(mutex_t *mp);
int mutex_unlock(mutex_t *mp);
*/
/* condition variable functions with POSIX thread, added by I.Hara
int cond_init(cond_t *cvp, int type, int arg);
int cond_destroy(cond_t *cvp);
int cond_wait(cond_t *cvp, mutex_t *mp);
int cond_timedwait(cond_t *cvp, mutex_t *mp, timestruc_t *abstime);
int cond_signal(cond_t *cvp);
int cond_broadcast(cond_t *cvp);
*/
/* semaphore functions with POSIX thread, added by I.Hara
typedef pthread_mutex_t mutex_t;
typedef pthread_cond_t cond_t;
typedef struct _sema {
unsigned int count;
mutex_t lock;
cond_t cond;
} sema_t;
*/
int sema_init(sema_t *sem, unsigned int c, int d, void *e)
{
pthread_mutex_init(&(sem->lock), NULL);
pthread_cond_init(&(sem->cond), NULL);
sem->count = c;
}
int sema_destroy(sema_t *sem)
{
pthread_mutex_destroy(&(sem->lock));
pthread_cond_destroy(&(sem->cond));
}
int sema_wait(sema_t *sem)
{
pthread_mutex_lock(&(sem->lock));
while (sem->count == 0){
pthread_cond_wait(&(sem->cond), &(sem->lock));}
sem->count--;
pthread_mutex_unlock(&(sem->lock));
}
int sema_trywait(sema_t *sem)
{
int ret,stat;
stat = pthread_mutex_trylock(&(sem->lock));
if (stat != 0) return (stat);
if (sem->count == 0)
ret = EBUSY;
else{
sem->count--;
ret = 0;
}
pthread_mutex_unlock(&(sem->lock));
return (ret);
}
int sema_post(sema_t *sem)
{
pthread_mutex_lock(&(sem->lock));
sem->count++;
pthread_cond_signal(&(sem->cond));
pthread_mutex_unlock(&(sem->lock));
}
/* readers/writer lock function for Linux added by I.Hara
typedef struct _rwlock {
unsigned int readers;
mutex_t lock;
cond_t r_cond;
cond_t w_cond;
} rwlock_t;
*/
int rwlock_init(rwlock_t *rw, int c, void *d)
{
mutex_init(&(rw->lock), 0, 0);
cond_init(&(rw->r_cond), &(rw->lock),0);
cond_init(&(rw->w_cond), &(rw->lock),0);
rw->readers = 0;
}
int rwlock_destroy(rwlock_t *rw)
{
mutex_destroy(&(rw->lock));
cond_destroy(&(rw->r_cond));
cond_destroy(&(rw->w_cond));
}
int rw_rdlock(rwlock_t *rw)
{
mutex_lock(&(rw->lock));
while (rw->readers == -1)
cond_wait(&(rw->r_cond), 0);
rw->readers++;
mutex_unlock(&(rw->lock));
}
int rw_wrlock(rwlock_t *rw)
{
mutex_lock(&(rw->lock));
while (rw->readers != 0)
cond_wait(&(rw->w_cond), 0);
rw->readers=-1;
mutex_unlock(&(rw->lock));
}
int rw_unlock(rwlock_t *rw)
{
mutex_lock(&(rw->lock));
if (rw->readers == -1)
rw->readers = 0;
else
rw->readers--;
cond_broadcast(&(rw->w_cond));
cond_broadcast(&(rw->r_cond));
mutex_unlock(&(rw->lock));
}
|