Project

General

Profile

Statistics
| Revision:

root / lab4 / .minix-src / include / minix / mthread.h @ 14

History | View | Annotate | Download (7.69 KB)

1
#ifndef _MTHREAD_H
2
#define _MTHREAD_H
3

    
4
#include <minix/config.h>        /* MUST be first */
5
#include <minix/const.h>
6
#include <sys/types.h>
7
#include <stdio.h>
8
#include <ucontext.h>
9
#include <errno.h>
10
#include <stdlib.h>
11
#include <limits.h>
12
#include <sys/signal.h>
13

    
14
typedef int mthread_thread_t;
15
typedef int mthread_once_t;
16
typedef int mthread_key_t;
17
typedef void * mthread_condattr_t;
18
typedef void * mthread_mutexattr_t;
19

    
20
struct __mthread_tcb;
21
typedef struct {
22
  struct __mthread_tcb *mq_head;
23
  struct __mthread_tcb *mq_tail;
24
} mthread_queue_t;
25

    
26
struct __mthread_mutex {
27
  mthread_queue_t mm_queue;        /* Queue of threads blocked on this mutex */
28
  mthread_thread_t mm_owner;        /* Thread ID that currently owns mutex */
29
#ifdef MTHREAD_STRICT
30
  struct __mthread_mutex *mm_prev;
31
  struct __mthread_mutex *mm_next;
32
#endif
33
  unsigned int mm_magic;
34
};
35
typedef struct __mthread_mutex *mthread_mutex_t;
36

    
37
struct __mthread_cond {
38
  struct __mthread_mutex *mc_mutex;        /* Associate mutex with condition */
39
#ifdef MTHREAD_STRICT
40
  struct __mthread_cond *mc_prev;
41
  struct __mthread_cond *mc_next;
42
#endif
43
  unsigned int mc_magic;
44
};
45
typedef struct __mthread_cond *mthread_cond_t;
46

    
47
struct __mthread_attr {
48
  size_t ma_stacksize;
49
  char *ma_stackaddr;
50
  int ma_detachstate;
51
  struct __mthread_attr *ma_prev;
52
  struct __mthread_attr *ma_next;
53
}; 
54
typedef struct __mthread_attr *mthread_attr_t;
55

    
56
typedef struct {
57
  mthread_mutex_t mutex;
58
  mthread_cond_t cond;
59
} mthread_event_t;
60

    
61
typedef struct {
62
  unsigned int readers;
63
  mthread_thread_t writer;
64
  mthread_mutex_t queue;
65
  mthread_event_t drain;
66
} mthread_rwlock_t; 
67

    
68
#define MTHREAD_CREATE_JOINABLE 001
69
#define MTHREAD_CREATE_DETACHED 002
70
#define MTHREAD_ONCE_INIT 0
71
#define MTHREAD_STACK_MIN MINSIGSTKSZ
72
#define MTHREAD_KEYS_MAX 128
73

    
74
__BEGIN_DECLS
75
/* allocate.c */
76
int mthread_create(mthread_thread_t *thread, mthread_attr_t *tattr, void
77
        *(*proc)(void *), void *arg);
78
int mthread_detach(mthread_thread_t thread);
79
int mthread_equal(mthread_thread_t l, mthread_thread_t r);
80
void mthread_exit(void *value);
81
int mthread_join(mthread_thread_t thread, void **value);
82
int mthread_once(mthread_once_t *once, void (*proc)(void));
83
mthread_thread_t mthread_self(void);
84

    
85
/* attribute.c */
86
int mthread_attr_destroy(mthread_attr_t *tattr);
87
int mthread_attr_getdetachstate(mthread_attr_t *tattr, int
88
        *detachstate);
89
int mthread_attr_getstack(mthread_attr_t *tattr, void **stackaddr,
90
        size_t *stacksize);
91
int mthread_attr_getstacksize(mthread_attr_t *tattr, size_t *stacksize);
92
int mthread_attr_init(mthread_attr_t *tattr);
93
int mthread_attr_setdetachstate(mthread_attr_t *tattr, int detachstate);
94
int mthread_attr_setstack(mthread_attr_t *tattr, void *stackaddr, size_t
95
        stacksize);
96
int mthread_attr_setstacksize(mthread_attr_t *tattr, size_t stacksize);
97

    
98

    
99
/* condition.c */
100
int mthread_cond_broadcast(mthread_cond_t *cond);
101
int mthread_cond_destroy(mthread_cond_t *cond);
102
int mthread_cond_init(mthread_cond_t *cond, mthread_condattr_t *cattr);
103
int mthread_cond_signal(mthread_cond_t *cond);
104
int mthread_cond_wait(mthread_cond_t *cond, mthread_mutex_t *mutex);
105

    
106
/* key.c */
107
int mthread_key_create(mthread_key_t *key, void (*destructor)(void *));
108
int mthread_key_delete(mthread_key_t key);
109
void *mthread_getspecific(mthread_key_t key);
110
int mthread_setspecific(mthread_key_t key, void *value);
111

    
112
/* misc.c */
113
void mthread_stats(void);
114
void mthread_verify_f(char *f, int l);
115
#define mthread_verify() mthread_verify_f(__FILE__, __LINE__)
116
void mthread_stacktrace(mthread_thread_t t);
117
void mthread_stacktraces(void);
118

    
119
/* mutex.c */
120
int mthread_mutex_destroy(mthread_mutex_t *mutex);
121
int mthread_mutex_init(mthread_mutex_t *mutex, mthread_mutexattr_t
122
        *mattr);
123
int mthread_mutex_lock(mthread_mutex_t *mutex);
124
int mthread_mutex_trylock(mthread_mutex_t *mutex);
125
int mthread_mutex_unlock(mthread_mutex_t *mutex);
126

    
127
/* event.c */
128
int mthread_event_destroy(mthread_event_t *event);
129
int mthread_event_init(mthread_event_t *event);
130
int mthread_event_wait(mthread_event_t *event);
131
int mthread_event_fire(mthread_event_t *event);
132
int mthread_event_fire_all(mthread_event_t *event);
133

    
134
/* rwlock.c */
135
int mthread_rwlock_destroy(mthread_rwlock_t *rwlock);
136
int mthread_rwlock_init(mthread_rwlock_t *rwlock);
137
int mthread_rwlock_rdlock(mthread_rwlock_t *rwlock);
138
int mthread_rwlock_wrlock(mthread_rwlock_t *rwlock);
139
int mthread_rwlock_unlock(mthread_rwlock_t *rwlock);
140

    
141
/* schedule.c */
142
int mthread_yield(void);
143
void mthread_yield_all(void);
144
__END_DECLS
145

    
146
#if defined(_MTHREADIFY_PTHREADS)
147
typedef mthread_thread_t pthread_t;
148
typedef mthread_once_t pthread_once_t;
149
typedef mthread_key_t pthread_key_t;
150
typedef mthread_cond_t pthread_cond_t;
151
typedef mthread_mutex_t pthread_mutex_t;
152
typedef mthread_condattr_t pthread_condattr_t;
153
typedef mthread_mutexattr_t pthread_mutexattr_t;
154
typedef mthread_attr_t pthread_attr_t;
155
typedef mthread_event_t pthread_event_t;
156
typedef mthread_rwlock_t pthread_rwlock_t;
157

    
158
/* LSC: No equivalent, so void* for now. */
159
typedef void *pthread_rwlockattr_t;
160

    
161
#define PTHREAD_ONCE_INIT 0
162
#define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
163
#define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
164

    
165
__BEGIN_DECLS
166
/* allocate.c */
167
int pthread_create(pthread_t *thread, pthread_attr_t *tattr, void
168
        *(*proc)(void *), void *arg);
169
int pthread_detach(pthread_t thread);
170
int pthread_equal(pthread_t l, pthread_t r);
171
void pthread_exit(void *value);
172
int pthread_join(pthread_t thread, void **value);
173
int pthread_once(pthread_once_t *once, void (*proc)(void));
174
pthread_t pthread_self(void);
175

    
176
/* attribute.c */
177
int pthread_attr_destroy(pthread_attr_t *tattr);
178
int pthread_attr_getdetachstate(pthread_attr_t *tattr, int
179
        *detachstate);
180
int pthread_attr_getstack(pthread_attr_t *tattr, void **stackaddr,
181
        size_t *stacksize);
182
int pthread_attr_getstacksize(pthread_attr_t *tattr, size_t *stacksize);
183
int pthread_attr_init(pthread_attr_t *tattr);
184
int pthread_attr_setdetachstate(pthread_attr_t *tattr, int detachstate);
185
int pthread_attr_setstack(pthread_attr_t *tattr, void *stackaddr, size_t
186
        stacksize);
187
int pthread_attr_setstacksize(pthread_attr_t *tattr, size_t stacksize);
188

    
189
/* condition.c */
190
int pthread_cond_broadcast(pthread_cond_t *cond);
191
int pthread_cond_destroy(pthread_cond_t *cond);
192
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cattr);
193
int pthread_cond_signal(pthread_cond_t *cond);
194
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
195

    
196
/* key.c */
197
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
198
int pthread_key_delete(pthread_key_t key);
199
void *pthread_getspecific(pthread_key_t key);
200
int pthread_setspecific(pthread_key_t key, void *value);
201

    
202
/* mutex.c */
203
int pthread_mutex_destroy(pthread_mutex_t *mutex);
204
int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t
205
        *mattr);
206
int pthread_mutex_lock(pthread_mutex_t *mutex);
207
int pthread_mutex_trylock(pthread_mutex_t *mutex);
208
int pthread_mutex_unlock(pthread_mutex_t *mutex);
209

    
210
/* event.c */
211
int pthread_event_destroy(pthread_event_t *event);
212
int pthread_event_init(pthread_event_t *event);
213
int pthread_event_wait(pthread_event_t *event);
214
int pthread_event_fire(pthread_event_t *event);
215
int pthread_event_fire_all(pthread_event_t *event);
216

    
217
/* rwlock.c */
218
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
219
int pthread_rwlock_init(pthread_rwlock_t *rwlock,
220
        pthread_rwlockattr_t *UNUSED(attr));
221
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
222
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
223
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
224

    
225
/* schedule.c */
226
int pthread_yield(void);
227
int sched_yield(void);
228
void pthread_yield_all(void);
229

    
230
/* LSC: FIXME: Maybe we should really do something with those... */
231
#define pthread_mutexattr_init(u) (0)
232
#define pthread_mutexattr_destroy(u) (0)
233

    
234
#define PTHREAD_MUTEX_RECURSIVE 0
235
#define pthread_mutexattr_settype(x, y) (EINVAL)
236
__END_DECLS
237

    
238
#endif /* defined(_MTHREADIFY_PTHREADS) */
239
#endif