Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #include "eus.h"
00007 #include <errno.h>
00008
00009
00010 pthread_t thread_table[MAXTHREAD][2];
00011
00012 int thr_self()
00013 {
00014 int i;
00015 pthread_t thr_t;
00016 thr_t = pthread_self();
00017 for(i=0;(i<MAXTHREAD)&&(thread_table[i][0] != thr_t);i++);
00018 return(i);
00019 }
00020
00021 int thr_getprio(int tid, int *prio)
00022 {
00023 if (thread_table[tid][1])
00024 *prio = thread_table[tid][0]->attr.prio;
00025 return(0);
00026 else
00027 return (-1);
00028 }
00029
00030 int thr_setprio(int tid, int prio)
00031 {
00032 int stat;
00033
00034 if (!thread_table[tid][1]) return(-1);
00035 else{
00036 if ((piro >= PTHREAD_MIN_PRIORITY) &&
00037 (piro =< PTHREAD_MAX_PRIORITY))
00038 thread_table[tid][0]->attr.prio = prio;
00039 return(0);
00040 else return(EINVAL); }
00041 }
00042
00043 int thr_create(void *base, size_t size, void (*func)(),
00044 void *args, long flags, int *tid)
00045 { int i, stat;
00046 pthread_attrt th_attr;
00047 for(i=0;(i<MAXTHREAD) && thread_table[i][1] ;i++);
00048 thread_table[i][0] = 1;
00049 pthread_attr_init(&th_attr);
00050 if (flag != 0)
00051 fprintf(stderr, "Warning in thr_create(): flags doesn't support!\n");
00052 if (size >= PTHREAD_STACK_MIN) pthread_attr_setstacksize(&th_attr, size);
00053 stat = pthread_create( &thread_table[i], th_attr, func, args);
00054 *tid = i;
00055 return(stat);
00056 }
00057
00058 int thr_continue(int tid)
00059 {
00060 fprintf(stderr, "Error in thr_continue: POSIX thread doesn't supported.\n");
00061 return(-1);
00062 }
00063
00064 int thr_suspend(int tid)
00065 {
00066 fprintf(stderr, "Error in thr_suspend: POSIX thread doesn't supported.\n");
00067 return(-1);
00068 }
00069
00070 int thr_kill(int tid, int sig)
00071 {
00072 return(pthread_kill(thread_table[tid][0], sig));
00073 }
00074
00075 int thr_join(int tid, int *depature, void **status)
00076 {
00077 if ( *depature != NULL){
00078 fprintf(stderr,"Warrning in thr_join: argument 'depature' must be NULL.\n");
00079 *depature == -1;
00080 }
00081 return(pthread_join(thread_table[tid][0], status));
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 int sema_init(sema_t *sem, unsigned int c, int d, void *e)
00112 {
00113 pthread_mutex_init(&(sem->lock), NULL);
00114 pthread_cond_init(&(sem->cond), NULL);
00115 sem->count = c;
00116 }
00117
00118 int sema_destroy(sema_t *sem)
00119 {
00120 pthread_mutex_destroy(&(sem->lock));
00121 pthread_cond_destroy(&(sem->cond));
00122 }
00123
00124 int sema_wait(sema_t *sem)
00125 {
00126 pthread_mutex_lock(&(sem->lock));
00127 while (sem->count == 0){
00128 pthread_cond_wait(&(sem->cond), &(sem->lock));}
00129 sem->count--;
00130 pthread_mutex_unlock(&(sem->lock));
00131 }
00132
00133 int sema_trywait(sema_t *sem)
00134 {
00135 int ret,stat;
00136 stat = pthread_mutex_trylock(&(sem->lock));
00137 if (stat != 0) return (stat);
00138 if (sem->count == 0)
00139 ret = EBUSY;
00140 else{
00141 sem->count--;
00142 ret = 0;
00143 }
00144 pthread_mutex_unlock(&(sem->lock));
00145 return (ret);
00146 }
00147
00148 int sema_post(sema_t *sem)
00149 {
00150 pthread_mutex_lock(&(sem->lock));
00151 sem->count++;
00152 pthread_cond_signal(&(sem->cond));
00153 pthread_mutex_unlock(&(sem->lock));
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 int rwlock_init(rwlock_t *rw, int c, void *d)
00165 {
00166 mutex_init(&(rw->lock), 0, 0);
00167 cond_init(&(rw->r_cond), &(rw->lock),0);
00168 cond_init(&(rw->w_cond), &(rw->lock),0);
00169 rw->readers = 0;
00170 }
00171
00172 int rwlock_destroy(rwlock_t *rw)
00173 {
00174 mutex_destroy(&(rw->lock));
00175 cond_destroy(&(rw->r_cond));
00176 cond_destroy(&(rw->w_cond));
00177 }
00178
00179 int rw_rdlock(rwlock_t *rw)
00180 {
00181 mutex_lock(&(rw->lock));
00182 while (rw->readers == -1)
00183 cond_wait(&(rw->r_cond), 0);
00184 rw->readers++;
00185 mutex_unlock(&(rw->lock));
00186 }
00187
00188 int rw_wrlock(rwlock_t *rw)
00189 {
00190 mutex_lock(&(rw->lock));
00191 while (rw->readers != 0)
00192 cond_wait(&(rw->w_cond), 0);
00193 rw->readers=-1;
00194 mutex_unlock(&(rw->lock));
00195 }
00196
00197 int rw_unlock(rwlock_t *rw)
00198 {
00199 mutex_lock(&(rw->lock));
00200 if (rw->readers == -1)
00201 rw->readers = 0;
00202 else
00203 rw->readers--;
00204 cond_broadcast(&(rw->w_cond));
00205 cond_broadcast(&(rw->r_cond));
00206 mutex_unlock(&(rw->lock));
00207 }
00208