mthread_alpha.c
Go to the documentation of this file.
1 #if alpha
2 #include "eus.h"
3 
4 /*
5  * Most functions in thread library of Solaris2 return Zero on success.
6  */
7 
8 /* thread function for OSF/1 */
9 static struct {
10  int using;
11  thread_t tid;
12 } thread_table[MAXTHREAD];
13 
14 int thr_self()
15 {
16  int i;
17  thread_t tid;
18 
19  tid = pthread_self();
20  for( i = 0; i < MAXTHREAD && !pthread_equal(thread_table[i].tid,tid); i++ )
21  ;
22 
23  return( i );
24 }
25 
26 int thr_getprio( int tid, int *prio )
27 {
28  *prio = pthread_getprio( thread_table[tid].tid );
29  if( *prio == -1 )
30  return errno;
31  else
32  return ESUCCESS;
33 }
34 
35 int thr_setprio(int tid, int prio)
36 {
37  if( pthread_setprio( thread_table[tid].tid, prio ) == -1 )
38  return errno;
39  else
40  return ESUCCESS;
41 }
42 
43 struct thr_arg {
44  int tid;
45  void (*func)();
46  void *args;
47 };
48 
49 static void thr_cleanup( struct thr_arg *arg )
50 {
51  thread_table[arg->tid].using = 0;
52 
53  free(arg);
54 }
55 
56 static void thr_startup( struct thr_arg *arg )
57 {
58  printf( "thr_startup:tid=%d\n", arg->tid );
59 
60  pthread_cleanup_push( thr_cleanup, arg );
61 
62  pthread_setcancel( CANCEL_ON );
63  pthread_setasynccancel( CANCEL_ON );
64 
65  (arg->func)( arg->args );
66 
67  pthread_cleanup_pop( 1 );
68 }
69 
70 int thr_create(void *base, size_t size, void (*func)(), void *args, long flags, int *tid )
71 /* base is not used */
72 /* size is not implemented */
73 /* flags is not used */
74 {
75  int i, stat;
76  struct thr_arg *arg;
77 
78  for( i = 0; i < MAXTHREAD && thread_table[i].using; i++ )
79  ;
80  if( i >= MAXTHREAD )
81  return -1;
82 
83  if( (arg = (struct thr_arg *)malloc( sizeof(struct thr_arg) )) == NULL )
84  return -1;
85 
86  arg->tid = i;
87  arg->func = func;
88  arg->args = args;
89  stat = pthread_create( &thread_table[i].tid, pthread_attr_default, (pthread_startroutine_t)thr_startup, (pthread_addr_t)arg );
90  if( stat == 0 )
91  thread_table[i].using = 1;
92  *tid = i;
93  return( stat );
94 }
95 
96 /*
97  thr_suspend() and thr_continue() are not implemented.
98  these routines are only defined to avoid prototype definition error.
99  */
100 int thr_suspend( int tid ) { return -1; }
101 int thr_continue( int tid ) { return -1; }
102 
103 int thr_kill( int tid, int sig )
104 /* sig is not used */
105 {
106  if( pthread_cancel( thread_table[tid].tid ) < 0 )
107  return errno;
108  else
109  return ESUCCESS;
110 }
111 
112 /* readers/writer lock functions for OSF/1 */
113 int rwlock_init(rwlock_t *rwlp, int type, void *arg)
114 {
115  pthread_mutex_init(&(rwlp->lock), pthread_mutexattr_default);
116  pthread_cond_init(&(rwlp->r_cond), pthread_condattr_default);
117  pthread_cond_init(&(rwlp->w_cond), pthread_condattr_default);
118 
119  rwlp->readers = 0;
120 
121  return 0;
122 }
123 
124 int rwlock_destroy( rwlock_t *rwlp )
125 {
126  pthread_mutex_destroy( &(rwlp->lock) );
127  pthread_cond_destroy( &(rwlp->r_cond) );
128  pthread_cond_destroy( &(rwlp->w_cond) );
129 
130  return 0;
131 }
132 
133 int rw_rdlock( rwlock_t *rwlp )
134 {
135  pthread_mutex_lock( &(rwlp->lock) );
136  while( rwlp->readers == -1 )
137  pthread_cond_wait( &(rwlp->r_cond), &(rwlp->lock) );
138  rwlp->readers++;
139  pthread_mutex_unlock( &(rwlp->lock) );
140 
141  return 0;
142 }
143 
144 int rw_wrlock( rwlock_t *rwlp )
145 {
146  pthread_mutex_lock( &(rwlp->lock) );
147  while( rwlp->readers != 0 )
148  pthread_cond_wait( &(rwlp->w_cond), &(rwlp->lock) );
149  rwlp->readers=-1;
150  pthread_mutex_unlock( &(rwlp->lock) );
151 
152  return 0;
153 }
154 
155 int rw_unlock( rwlock_t *rwlp )
156 {
157  pthread_mutex_lock( &(rwlp->lock) );
158  if( rwlp->readers == -1 )
159  rwlp->readers = 0;
160  else
161  rwlp->readers--;
162  pthread_cond_broadcast( &(rwlp->w_cond) );
163  pthread_cond_broadcast( &(rwlp->r_cond) );
164  pthread_mutex_unlock( &(rwlp->lock) );
165 
166  return 0;
167 }
168 
169 /* semaphore function for OSF/1 */
170 int sema_init(sema_t *sem, unsigned int c, int d, void *e)
171 /* c is not used */
172 /* d is not used */
173 /* e is not used */
174 {
175  pthread_mutex_init(&(sem->lock), pthread_mutexattr_default );
176  pthread_cond_init(&(sem->cond), pthread_condattr_default );
177  sem->count = 0;
178 
179  return 0;
180 }
181 
182 int sema_destroy(sema_t *sem)
183 {
184  pthread_mutex_destroy(&(sem->lock));
185  pthread_cond_destroy(&(sem->cond));
186 
187  return 0;
188 }
189 
190 int sema_wait(sema_t *sem)
191 {
192  pthread_mutex_lock(&(sem->lock));
193  while (sem->count == 0){
194  pthread_cond_wait(&(sem->cond), &(sem->lock));}
195  sem->count--;
196  pthread_mutex_unlock(&(sem->lock));
197 
198  return 0;
199 }
200 
201 int sema_trywait(sema_t *sem)
202 {
203  int ret;
204  pthread_mutex_lock(&(sem->lock));
205  if (sem->count == 0)
206  ret = EBUSY;
207  else{
208  sem->count--;
209  ret = 0;
210  }
211  pthread_mutex_unlock(&(sem->lock));
212  return (ret);
213 }
214 
215 int sema_post(sema_t *sem)
216 {
217  pthread_mutex_lock(&(sem->lock));
218  sem->count++;
219  pthread_cond_broadcast(&(sem->cond));
220  pthread_mutex_unlock(&(sem->lock));
221 
222  return 0;
223 }
224 
225 
226 void mthread_init( context *mainctx )
227 {
228  int i;
229  pthread_t tid;
230 
231  for( i = 0; i < MAXTHREAD; i++ )
232  thread_table[i].using = 0;
233 
234  thread_table[0].tid = pthread_self();
235  thread_table[0].using = 1;
236 
237  pthread_mutex_init(&p_mark_lock, pthread_mutexattr_default );
238  pthread_mutex_init(&alloc_lock, pthread_mutexattr_default );
239  pthread_mutex_init(&free_thread_lock, pthread_mutexattr_default );
240  pthread_mutex_init(&qthread_lock, pthread_mutexattr_default );
241  pthread_mutex_init(&qsort_lock, pthread_mutexattr_default );
242  sema_init(&free_thread_sem, 0, 0, 0);
243  rwlock_init(&gc_lock, 0, 0);
244 }
245 
246 
247 #endif
rwlock_t gc_lock
Definition: mthread.c:18
Definition: eus.h:524
int rwlock_destroy(rwlock_t *)
Definition: pthreads.c:172
int thr_continue(int)
Definition: pthreads.c:58
int thr_setprio(int, int)
Definition: pthreads.c:30
cond_t w_cond
Definition: eus_thr.h:105
int sema_init(sema_t *, unsigned int, int, void *)
Definition: pthreads.c:111
cond_t cond
Definition: eus_thr.h:99
context * mainctx
Definition: eus.c:57
mutex_t qthread_lock
Definition: mthread.c:14
unsigned int count
Definition: eus_thr.h:97
mutex_t p_mark_lock
pointer arg
Definition: eus_thr.h:34
Definition: eus_thr.h:96
int thr_kill(int, int)
Definition: pthreads.c:70
int rwlock_init(rwlock_t *, int, void *)
Definition: pthreads.c:164
thread_t tid
Definition: collector.c:50
int thr_suspend(int)
Definition: pthreads.c:64
int rw_wrlock(rwlock_t *)
Definition: pthreads.c:188
int sema_wait(sema_t *)
Definition: pthreads.c:124
mutex_t free_thread_lock
Definition: mthread.c:13
struct @10 thread_table[]
cond_t r_cond
Definition: eus_thr.h:104
int sema_destroy(sema_t *)
Definition: pthreads.c:118
int thr_create(void *, size_t, void(*)(), void *, long, int *)
Definition: pthreads.c:43
int rw_rdlock(rwlock_t *)
Definition: pthreads.c:179
int sema_trywait(sema_t *)
Definition: pthreads.c:133
sema_t free_thread_sem
Definition: mthread.c:11
mutex_t qsort_lock
Definition: mthread.c:21
int rw_unlock(rwlock_t *)
Definition: pthreads.c:197
int thr_getprio(int tid, int *prio)
Definition: pthreads.c:21
unsigned int readers
Definition: eus_thr.h:102
#define NULL
Definition: transargv.c:8
int errno
int sema_post(sema_t *sem)
Definition: pthreads.c:148
unsigned int thr_self()
Definition: eus.c:25
mutex_t lock
Definition: eus_thr.h:98
pointer func
Definition: eus_thr.h:33
mutex_t lock
Definition: eus_thr.h:103
mutex_t alloc_lock
Definition: memory.mutex.c:42


euslisp
Author(s): Toshihiro Matsui
autogenerated on Mon Feb 28 2022 22:18:28