pthreads.c
Go to the documentation of this file.
00001 /*
00002    POSIX thread functions -> Solaris mthread functions.
00003          writtten by I.Hara
00004 */
00005 
00006 #include "eus.h"
00007 #include <errno.h>
00008 
00009 /* thread function for POSIX added by I.Hara */
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 /* mutex functions with POSIX thread, added by I.Hara 
00085      int mutex_init(mutex_t *mp, int type, void * arg);
00086      int mutex_destroy(mutex_t *mp);
00087      int mutex_lock(mutex_t *mp);
00088      int mutex_trylock(mutex_t *mp);
00089      int mutex_unlock(mutex_t *mp);                                   
00090 */
00091 /* condition variable functions with POSIX thread, added by I.Hara 
00092      int cond_init(cond_t *cvp, int type, int arg);
00093      int cond_destroy(cond_t *cvp);
00094      int cond_wait(cond_t *cvp, mutex_t *mp);
00095      int cond_timedwait(cond_t *cvp, mutex_t *mp, timestruc_t *abstime);
00096      int cond_signal(cond_t *cvp);
00097      int cond_broadcast(cond_t *cvp);                                          
00098 */
00099 
00100 /* semaphore functions with POSIX thread, added by I.Hara 
00101 typedef pthread_mutex_t mutex_t;
00102 typedef pthread_cond_t cond_t;
00103 
00104 typedef struct _sema {
00105         unsigned int count;
00106         mutex_t      lock;
00107         cond_t       cond;
00108       } sema_t;
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 /* readers/writer lock function for Linux added by I.Hara 
00157 typedef struct _rwlock {
00158         unsigned int readers;
00159         mutex_t      lock;
00160         cond_t       r_cond;
00161         cond_t       w_cond;
00162       } rwlock_t;
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 


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Sep 3 2015 10:36:20