summaryrefslogtreecommitdiff
path: root/minix/lib/libmthread/condition.c
blob: 3488a5f7c5530879f754bc0c8d3bd4790aa354b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <minix/mthread.h>
#include "global.h"
#include "proto.h"

#ifdef MTHREAD_STRICT
static struct __mthread_cond *vc_front, *vc_rear;
static void mthread_cond_add(mthread_cond_t *c);
static void mthread_cond_remove(mthread_cond_t *c);
static int mthread_cond_valid(mthread_cond_t *c);
#else
# define mthread_cond_add(c)		((*c)->mc_magic = MTHREAD_INIT_MAGIC)
# define mthread_cond_remove(c)		((*c)->mc_magic = MTHREAD_NOT_INUSE)
# define mthread_cond_valid(c)		((*c)->mc_magic == MTHREAD_INIT_MAGIC)
#endif
#define MAIN_COND mainthread.m_cond

/*===========================================================================*
 *				mthread_init_valid_conditions		     *
 *===========================================================================*/
void mthread_init_valid_conditions(void)
{
#ifdef MTHREAD_STRICT
/* Initialize condition variable list */
  vc_front = vc_rear = NULL;
#endif
}


/*===========================================================================*
 *				mthread_cond_add			     *
 *===========================================================================*/
#ifdef MTHREAD_STRICT
static void mthread_cond_add(c) 
mthread_cond_t *c;
{
/* Add condition to list of valid, initialized conditions */

  if (vc_front == NULL) {	/* Empty list */
  	vc_front = *c;
  	(*c)->mc_prev = NULL;
  } else {
  	vc_rear->mc_next = *c;
  	(*c)->mc_prev = vc_rear;
  }

  (*c)->mc_next = NULL;
  vc_rear = *c;
}
#endif

/*===========================================================================*
 *				mthread_cond_broadcast			     *
 *===========================================================================*/
int mthread_cond_broadcast(cond)
mthread_cond_t *cond;
{
/* Signal all threads waiting for condition 'cond'. */
  mthread_thread_t t;
  mthread_tcb_t *tcb;

  if (cond == NULL) 
  	return(EINVAL);
  else if (!mthread_cond_valid(cond))
  	return(EINVAL);

  tcb = mthread_find_tcb(MAIN_THREAD);
  if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
  	mthread_unsuspend(MAIN_THREAD);

  for (t = (mthread_thread_t) 0; t < no_threads; t++) {
  	tcb = mthread_find_tcb(t);
	if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond) 
		mthread_unsuspend(t);
  }

  return(0);
}


/*===========================================================================*
 *				mthread_cond_destroy			     *
 *===========================================================================*/
int mthread_cond_destroy(cond)
mthread_cond_t *cond;
{
/* Destroy a condition variable. Make sure it's not in use */
  mthread_thread_t t;
  mthread_tcb_t *tcb;

  if (cond == NULL)
  	return(EINVAL);
  else if (!mthread_cond_valid(cond))
  	return(EINVAL);

  /* Is another thread currently using this condition variable? */
  tcb = mthread_find_tcb(MAIN_THREAD);
  if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
  	return(EBUSY);

  for (t = (mthread_thread_t) 0; t < no_threads; t++) {
  	tcb = mthread_find_tcb(t);
	if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
		return(EBUSY);
  }

  /* Not in use; invalidate it. */
  mthread_cond_remove(cond);
  free(*cond);
  *cond = NULL;

  return(0);
}


/*===========================================================================*
 *				mthread_cond_init			     *
 *===========================================================================*/
int mthread_cond_init(cond, cattr)
mthread_cond_t *cond;
mthread_condattr_t *cattr;
{
/* Initialize condition variable to a known state. cattr is ignored */
  struct __mthread_cond *c;

  if (cond == NULL) 
	return(EINVAL);
  else if (cattr != NULL) 
  	return(ENOSYS);

#ifdef MTHREAD_STRICT
  else if (mthread_cond_valid(cond)) 
	/* Already initialized */
  	return(EBUSY);
#endif
  else if ((c = malloc(sizeof(struct __mthread_cond))) == NULL) 
  	return(ENOMEM);

  c->mc_mutex = NULL;
  *cond = (mthread_cond_t) c;
  mthread_cond_add(cond);

  return(0);
}


/*===========================================================================*
 *				mthread_cond_remove			     *
 *===========================================================================*/
#ifdef MTHREAD_STRICT
static void mthread_cond_remove(c)
mthread_cond_t *c;
{
/* Remove condition from list of valid, initialized conditions */

  if ((*c)->mc_prev == NULL)
  	vc_front = (*c)->mc_next;
  else
  	(*c)->mc_prev->mc_next = (*c)->mc_next;

  if ((*c)->mc_next == NULL)
  	vc_rear = (*c)->mc_prev;
  else
  	(*c)->mc_next->mc_prev = (*c)->mc_prev;

}
#endif

/*===========================================================================*
 *				mthread_cond_signal			     *
 *===========================================================================*/
int mthread_cond_signal(cond)
mthread_cond_t *cond;
{
/* Signal a thread that condition 'cond' was met. Just a single thread. */
  mthread_thread_t t;
  mthread_tcb_t *tcb;

  if (cond == NULL)
	return(EINVAL);
  else if (!mthread_cond_valid(cond))
	return(EINVAL);

  tcb = mthread_find_tcb(MAIN_THREAD);
  if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
  	mthread_unsuspend(MAIN_THREAD);

  for (t = (mthread_thread_t) 0; t < no_threads; t++) {
  	tcb = mthread_find_tcb(t);
	if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond){
		mthread_unsuspend(t);
		break;
	}
  }

  return(0);
}


/*===========================================================================*
 *				mthread_cond_valid			     *
 *===========================================================================*/
#ifdef MTHREAD_STRICT
static int mthread_cond_valid(c)
mthread_cond_t *c;
{
/* Check to see if cond is on the list of valid conditions */
  struct __mthread_cond *loopitem;

  loopitem = vc_front;

  while (loopitem != NULL) {
  	if (loopitem == *c)
  		return(1);

  	loopitem = loopitem->mc_next;
  }

  return(0);
}
#endif

/*===========================================================================*
 *				mthread_cond_verify			     *
 *===========================================================================*/
#ifdef MDEBUG
int mthread_cond_verify(void)
{
/* Return true in case no condition variables are in use. */

  return(vc_front == NULL);
}
#endif


/*===========================================================================*
 *				mthread_cond_wait			     *
 *===========================================================================*/
int mthread_cond_wait(cond, mutex)
mthread_cond_t *cond;
mthread_mutex_t *mutex;
{
/* Wait for a condition to be signaled */
  mthread_tcb_t *tcb;
  struct __mthread_cond *c;
  struct __mthread_mutex *m;

  if (cond == NULL || mutex == NULL)
	return(EINVAL);
  
  c = (struct __mthread_cond *) *cond;
  m = (struct __mthread_mutex *) *mutex;

  if (!mthread_cond_valid(cond) || !mthread_mutex_valid(mutex)) 
	return(EINVAL);

  c->mc_mutex = m;	/* Remember we're using this mutex in a cond_wait */
  if (mthread_mutex_unlock(mutex) != 0) /* Fails when we're not the owner */
  	return(-1);

  tcb = mthread_find_tcb(current_thread);
  tcb->m_cond = c; /* Register condition variable. */
  mthread_suspend(MS_CONDITION);

  /* When execution returns here, the condition was met. Lock mutex again. */
  c->mc_mutex = NULL;				/* Forget about this mutex */
  tcb->m_cond = NULL;				/* ... and condition var */
  if (mthread_mutex_lock(mutex) != 0)
  	return(-1);

  return(0);
}

/* pthread compatibility layer. */
__weak_alias(pthread_cond_init, mthread_cond_init)