pthreads.c
Go to the documentation of this file.
1 /*
2  POSIX thread functions -> Solaris mthread functions.
3  writtten by I.Hara
4 */
5 
6 #include "eus.h"
7 #include <errno.h>
8 
9 /* thread function for POSIX added by I.Hara */
10 pthread_t thread_table[MAXTHREAD][2];
11 
12 int thr_self()
13 {
14  int i;
15  pthread_t thr_t;
16  thr_t = pthread_self();
17  for(i=0;(i<MAXTHREAD)&&(thread_table[i][0] != thr_t);i++);
18  return(i);
19 }
20 
21 int thr_getprio(int tid, int *prio)
22 {
23  if (thread_table[tid][1])
24  *prio = thread_table[tid][0]->attr.prio;
25  return(0);
26  else
27  return (-1);
28 }
29 
30 int thr_setprio(int tid, int prio)
31 {
32  int stat;
33 
34  if (!thread_table[tid][1]) return(-1);
35  else{
36  if ((piro >= PTHREAD_MIN_PRIORITY) &&
37  (piro =< PTHREAD_MAX_PRIORITY))
38  thread_table[tid][0]->attr.prio = prio;
39  return(0);
40  else return(EINVAL); }
41 }
42 
43 int thr_create(void *base, size_t size, void (*func)(),
44  void *args, long flags, int *tid)
45 { int i, stat;
46  pthread_attrt th_attr;
47  for(i=0;(i<MAXTHREAD) && thread_table[i][1] ;i++);
48  thread_table[i][0] = 1;
49  pthread_attr_init(&th_attr);
50  if (flag != 0)
51  fprintf(stderr, "Warning in thr_create(): flags doesn't support!\n");
52  if (size >= PTHREAD_STACK_MIN) pthread_attr_setstacksize(&th_attr, size);
53  stat = pthread_create( &thread_table[i], th_attr, func, args);
54  *tid = i;
55  return(stat);
56 }
57 
58 int thr_continue(int tid)
59 {
60  fprintf(stderr, "Error in thr_continue: POSIX thread doesn't supported.\n");
61  return(-1);
62 }
63 
64 int thr_suspend(int tid)
65 {
66  fprintf(stderr, "Error in thr_suspend: POSIX thread doesn't supported.\n");
67  return(-1);
68 }
69 
70 int thr_kill(int tid, int sig)
71 {
72  return(pthread_kill(thread_table[tid][0], sig));
73 }
74 
75 int thr_join(int tid, int *depature, void **status)
76 {
77  if ( *depature != NULL){
78  fprintf(stderr,"Warrning in thr_join: argument 'depature' must be NULL.\n");
79  *depature == -1;
80  }
81  return(pthread_join(thread_table[tid][0], status));
82 }
83 
84 /* mutex functions with POSIX thread, added by I.Hara
85  int mutex_init(mutex_t *mp, int type, void * arg);
86  int mutex_destroy(mutex_t *mp);
87  int mutex_lock(mutex_t *mp);
88  int mutex_trylock(mutex_t *mp);
89  int mutex_unlock(mutex_t *mp);
90 */
91 /* condition variable functions with POSIX thread, added by I.Hara
92  int cond_init(cond_t *cvp, int type, int arg);
93  int cond_destroy(cond_t *cvp);
94  int cond_wait(cond_t *cvp, mutex_t *mp);
95  int cond_timedwait(cond_t *cvp, mutex_t *mp, timestruc_t *abstime);
96  int cond_signal(cond_t *cvp);
97  int cond_broadcast(cond_t *cvp);
98 */
99 
100 /* semaphore functions with POSIX thread, added by I.Hara
101 typedef pthread_mutex_t mutex_t;
102 typedef pthread_cond_t cond_t;
103 
104 typedef struct _sema {
105  unsigned int count;
106  mutex_t lock;
107  cond_t cond;
108  } sema_t;
109 
110 */
111 int sema_init(sema_t *sem, unsigned int c, int d, void *e)
112 {
113  pthread_mutex_init(&(sem->lock), NULL);
114  pthread_cond_init(&(sem->cond), NULL);
115  sem->count = c;
116 }
117 
119 {
120  pthread_mutex_destroy(&(sem->lock));
121  pthread_cond_destroy(&(sem->cond));
122 }
123 
124 int sema_wait(sema_t *sem)
125 {
126  pthread_mutex_lock(&(sem->lock));
127  while (sem->count == 0){
128  pthread_cond_wait(&(sem->cond), &(sem->lock));}
129  sem->count--;
130  pthread_mutex_unlock(&(sem->lock));
131 }
132 
134 {
135  int ret,stat;
136  stat = pthread_mutex_trylock(&(sem->lock));
137  if (stat != 0) return (stat);
138  if (sem->count == 0)
139  ret = EBUSY;
140  else{
141  sem->count--;
142  ret = 0;
143  }
144  pthread_mutex_unlock(&(sem->lock));
145  return (ret);
146 }
147 
148 int sema_post(sema_t *sem)
149 {
150  pthread_mutex_lock(&(sem->lock));
151  sem->count++;
152  pthread_cond_signal(&(sem->cond));
153  pthread_mutex_unlock(&(sem->lock));
154 }
155 
156 /* readers/writer lock function for Linux added by I.Hara
157 typedef struct _rwlock {
158  unsigned int readers;
159  mutex_t lock;
160  cond_t r_cond;
161  cond_t w_cond;
162  } rwlock_t;
163 */
164 int rwlock_init(rwlock_t *rw, int c, void *d)
165 {
166  mutex_init(&(rw->lock), 0, 0);
167  cond_init(&(rw->r_cond), &(rw->lock),0);
168  cond_init(&(rw->w_cond), &(rw->lock),0);
169  rw->readers = 0;
170 }
171 
173 {
174  mutex_destroy(&(rw->lock));
175  cond_destroy(&(rw->r_cond));
176  cond_destroy(&(rw->w_cond));
177 }
178 
180 {
181  mutex_lock(&(rw->lock));
182  while (rw->readers == -1)
183  cond_wait(&(rw->r_cond), 0);
184  rw->readers++;
185  mutex_unlock(&(rw->lock));
186 }
187 
189 {
190  mutex_lock(&(rw->lock));
191  while (rw->readers != 0)
192  cond_wait(&(rw->w_cond), 0);
193  rw->readers=-1;
194  mutex_unlock(&(rw->lock));
195 }
196 
198 {
199  mutex_lock(&(rw->lock));
200  if (rw->readers == -1)
201  rw->readers = 0;
202  else
203  rw->readers--;
204  cond_broadcast(&(rw->w_cond));
205  cond_broadcast(&(rw->r_cond));
206  mutex_unlock(&(rw->lock));
207 }
208 
int thr_setprio(int tid, int prio)
Definition: pthreads.c:30
cond_t w_cond
Definition: eus_thr.h:105
cond_t cond
Definition: eus_thr.h:99
int thr_join(int tid, int *depature, void **status)
Definition: pthreads.c:75
unsigned int count
Definition: eus_thr.h:97
pthread_t thread_table[MAXTHREAD][2]
Definition: pthreads.c:10
Definition: eus_thr.h:96
int sema_init(sema_t *sem, unsigned int c, int d, void *e)
Definition: pthreads.c:111
int thr_continue(int tid)
Definition: pthreads.c:58
int thr_getprio(int tid, int *prio)
Definition: pthreads.c:21
int sema_destroy(sema_t *sem)
Definition: pthreads.c:118
thread_t tid
Definition: collector.c:50
int thr_create(void *base, size_t size, void(*func)(), void *args, long flags, int *tid)
Definition: pthreads.c:43
int rw_rdlock(rwlock_t *rw)
Definition: pthreads.c:179
int thr_kill(int tid, int sig)
Definition: pthreads.c:70
cond_t r_cond
Definition: eus_thr.h:104
int thr_suspend(int tid)
Definition: pthreads.c:64
int rw_unlock(rwlock_t *rw)
Definition: pthreads.c:197
int rwlock_destroy(rwlock_t *rw)
Definition: pthreads.c:172
unsigned int readers
Definition: eus_thr.h:102
int sema_trywait(sema_t *sem)
Definition: pthreads.c:133
#define NULL
Definition: transargv.c:8
int sema_post(sema_t *sem)
Definition: pthreads.c:148
int rw_wrlock(rwlock_t *rw)
Definition: pthreads.c:188
mutex_t lock
Definition: eus_thr.h:98
int sema_wait(sema_t *sem)
Definition: pthreads.c:124
int thr_self()
Definition: pthreads.c:12
mutex_t lock
Definition: eus_thr.h:103
int rwlock_init(rwlock_t *rw, int c, void *d)
Definition: pthreads.c:164


euslisp
Author(s): Toshihiro Matsui
autogenerated on Fri Feb 21 2020 03:20:54