mthread_sunos4.c
Go to the documentation of this file.
1 #if SunOS4_1
2 
3 #include "eus.h"
4 
5 /* thread function for SunOS 4.1.x added by H.Nakagaki */
6 thread_t thread_table[MAXTHREAD];
7 
8 int thr_self()
9 {
10  int i;
11  thread_t tid;
12  lwp_self(&tid);
13  for(i=0;(i<MAXTHREAD)&&(thread_table[i].thread_key != tid.thread_key);i++);
14  return(i);
15 }
16 
17 int thr_getprio(int tid, int *prio)
18 {
19  int stat;
20  statvec_t prio_vec;
21  stat=lwp_getstate(thread_table[tid], &prio_vec);
22  *prio = prio_vec.stat_prio;
23  return(stat);
24 }
25 
26 int thr_setprio(int tid, int prio)
27 {
28  return( lwp_setpri(thread_table[tid], prio));
29 }
30 
31 int thr_create(void *base, size_t size, void (*func)(), void *args, long flags, int *tid)
32 { int i, stat;
33  for(i=0;(i<MAXTHREAD)&&(thread_table[i].thread_key >=0 );i++);
34 /* lwp_setstkcache(size, 1);*/
35  stat = lwp_create( &thread_table[i], func, MINPRIO, flags, lwp_newstk(), 1, args);
36  *tid = i;
37  return(stat);
38 }
39 
40 int thr_continue(int tid)
41 {
42  return( lwp_resume(thread_table[tid]) );
43 }
44 
45 int thr_suspend(int tid)
46 {
47  return( lwp_suspend(thread_table[tid] ));
48 }
49 
50 int thr_kill(int tid, int sig)
51 {
52  int stat;
53  stat = lwp_destroy(thread_table[tid]);
54  thread_table[tid].thread_key = -1;
55  return(stat);
56 
57 }
58 
59 /* readers/writer lock function for SunOS 4.1.x added by H.Nakagaki */
60 int rwlock_init(rwlock_t *rw, int c, void *d)
61 {
62  mutex_init(&(rw->lock), 0, 0);
63  cond_init(&(rw->r_cond), &(rw->lock));
64  cond_init(&(rw->w_cond), &(rw->lock));
65  rw->readers = 0;
66 }
67 
68 int rwlock_destroy(rwlock_t *rw)
69 {
70  mutex_destroy(&(rw->lock));
71  cond_destroy(&(rw->r_cond));
72  cond_destroy(&(rw->w_cond));
73 }
74 
75 int rw_rdlock(rwlock_t *rw)
76 {
77  mutex_lock(&(rw->lock));
78  while (rw->readers == -1)
79  cond_wait(&(rw->r_cond), 0);
80  rw->readers++;
81  mutex_unlock(&(rw->lock));
82 }
83 
84 int rw_wrlock(rwlock_t *rw)
85 {
86  mutex_lock(&(rw->lock));
87  while (rw->readers != 0)
88  cond_wait(&(rw->w_cond), 0);
89  rw->readers=-1;
90  mutex_unlock(&(rw->lock));
91 }
92 
93 int rw_unlock(rwlock_t *rw)
94 {
95  mutex_lock(&(rw->lock));
96  if (rw->readers == -1)
97  rw->readers = 0;
98  else
99  rw->readers--;
100  cond_broadcast(&(rw->w_cond));
101  cond_broadcast(&(rw->r_cond));
102  mutex_unlock(&(rw->lock));
103 }
104 
105 /* semaphore function for SunOS 4.1.x added by H.Nakagaki */
106 int sema_init(sema_t *sem, unsigned int c, int d, void *e)
107 {
108  mutex_init(&(sem->lock), 0, 0 );
109  cond_init(&(sem->cond), &(sem->lock));
110  sem->count = c;
111 }
112 
113 int sema_destroy(sema_t *sem)
114 {
115  mutex_destroy(&(sem->lock));
116  cond_destroy(&(sem->cond));
117 }
118 
119 int sema_wait(sema_t *sem)
120 {
121  mutex_lock(&(sem->lock));
122  while (sem->count == 0){
123  cond_wait(&(sem->cond), &(sem->lock));}
124  sem->count--;
125  mutex_unlock(&(sem->lock));
126 }
127 
128 int sema_trywait(sema_t *sem)
129 {
130  int ret;
131  mutex_lock(&(sem->lock));
132  if (sem->count == 0)
133  ret = EBUSY;
134  else{
135  sem->count--;
136  ret = 0;
137  }
138  mutex_unlock(&(sem->lock));
139  return (ret);
140 }
141 
142 int sema_post(sema_t *sem)
143 {
144  mutex_lock(&(sem->lock));
145  sem->count++;
146  cond_broadcast(&(sem->cond));
147  mutex_unlock(&(sem->lock));
148 }
149 
150 /* Multi thread scheduler for SunOS 4.1.x. */
151 void scheduler()
152 {
153  struct timeval quantum;
154  quantum.tv_sec = 0;
155  quantum.tv_usec = 10000;
156  for(;;){
157  lwp_sleep(&quantum);
158  lwp_resched(MINPRIO);
159  }
160 }
161 
162 void mthread_init( context *mainctx )
163 {
164  int i;
165 
166  for(i=0;i<MAXTHREAD;i++)
167  thread_table[i].thread_key = -1;
168  lwp_self(&thread_table[0]);
169  mutex_init(&mark_lock, USYNC_THREAD, 0);
170  mutex_init(&p_mark_lock, USYNC_THREAD, 0);
171  mutex_init(&alloc_lock, USYNC_THREAD, 0);
172  mutex_init(&free_thread_lock, USYNC_THREAD, 0);
173  mutex_init(&qthread_lock, USYNC_THREAD, 0);
174  mutex_init(&qsort_lock, USYNC_THREAD, 0);
175  sema_init(&free_thread_sem, 0, USYNC_THREAD, 0);
176  rwlock_init(&gc_lock, USYNC_THREAD, 0);
177  pod_setmaxpri(2);
178  lwp_setstkcache(32*1024*4, MAXTHREAD);
179  lwp_create((thread_t *)0, scheduler, 2, 0, lwp_newstk(), 0);
180  lwp_setpri(SELF,MINPRIO);
181 }
182 
183 #endif /* SunOS4_1 */
_rwlock::w_cond
cond_t w_cond
Definition: eus_thr.h:105
context
Definition: eus.h:524
_rwlock::r_cond
cond_t r_cond
Definition: eus_thr.h:104
thr_kill
int thr_kill(int, int)
Definition: pthreads.c:70
rwlock_init
int rwlock_init(rwlock_t *, int, void *)
Definition: pthreads.c:164
rwlock_destroy
int rwlock_destroy(rwlock_t *)
Definition: pthreads.c:172
free_thread_sem
sema_t free_thread_sem
Definition: mthread.c:11
eus.h
mark_lock
mutex_t mark_lock
Definition: mthread.c:25
sema_init
int sema_init(sema_t *, unsigned int, int, void *)
Definition: pthreads.c:111
mainctx
context * mainctx
Definition: eus.c:57
rw_wrlock
int rw_wrlock(rwlock_t *)
Definition: pthreads.c:188
SELF
pointer SELF
Definition: eus.c:116
qsort_lock
mutex_t qsort_lock
Definition: mthread.c:21
thr_setprio
int thr_setprio(int, int)
Definition: pthreads.c:30
p_mark_lock
mutex_t p_mark_lock
thr_suspend
int thr_suspend(int)
Definition: pthreads.c:64
thread_table
struct @10 thread_table[]
_sema::lock
mutex_t lock
Definition: eus_thr.h:98
sema_trywait
int sema_trywait(sema_t *)
Definition: pthreads.c:133
rw_unlock
int rw_unlock(rwlock_t *)
Definition: pthreads.c:197
thr_getprio
int thr_getprio(int tid, int *prio)
Definition: pthreads.c:21
_sema::cond
cond_t cond
Definition: eus_thr.h:99
free_thread_lock
mutex_t free_thread_lock
Definition: mthread.c:13
sema_wait
int sema_wait(sema_t *)
Definition: pthreads.c:124
_sema::count
unsigned int count
Definition: eus_thr.h:97
_sema
Definition: eus_thr.h:96
sema_destroy
int sema_destroy(sema_t *)
Definition: pthreads.c:118
thr_create
int thr_create(void *, size_t, void(*)(), void *, long, int *)
Definition: pthreads.c:43
sema_post
int sema_post(sema_t *sem)
Definition: pthreads.c:148
rw_rdlock
int rw_rdlock(rwlock_t *)
Definition: pthreads.c:179
alloc_lock
mutex_t alloc_lock
Definition: memory.mutex.c:42
qthread_lock
mutex_t qthread_lock
Definition: mthread.c:14
_rwlock::readers
unsigned int readers
Definition: eus_thr.h:102
tid
thread_t tid
Definition: collector.c:50
_rwlock::lock
mutex_t lock
Definition: eus_thr.h:103
thr_continue
int thr_continue(int)
Definition: pthreads.c:58
gc_lock
rwlock_t gc_lock
Definition: mthread.c:18
thr_self
unsigned int thr_self()
Definition: eus.c:25
_rwlock
Definition: eus_thr.h:101


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:43