summaryrefslogtreecommitdiff
path: root/condvar.c
blob: e8f858c03eaf8fd6b0f2d638e7594d76dee3308b (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
/*
 * condvar.c
 *
 * Description:
 * This translation unit implements condition variables and their primitives.
 */

#include "pthread.h"

int
pthread_condattr_init(pthread_condattr_t *attr)
{
  return (attr == NULL) ? EINVAL : 0;
}

int
pthread_condattr_destroy(pthread_condattr_t *attr)
{
  return (attr == NULL) ? EINVAL : 0;
}

int
pthread_condattr_setpshared(pthread_condattr_t *attr,
			    int pshared)
{
  return (attr == NULL) ? EINVAL : ENOSYS;
}

int
pthread_condattr_getpshared(pthread_condattr_t *attr,
			    int *pshared)
{
  return (attr == NULL) ? EINVAL : ENOSYS;
}

int
pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *attr)
{
  /* Ensure we have a valid cond_t variable. */
  if (cv == NULL)
    {
      return EINVAL;
    }

  /* Initialize the count to 0. */
  cv->waiters_count = 0;

  /* Initialize the "mutex". */
  pthread_mutex_init(cv->waiters_count_lock);

  /* Create an auto-reset event. */
  cv->events[SIGNAL] = CreateEvent (NULL,     /* no security */
				    FALSE,    /* auto-reset event */
				    FALSE,    /* non-signaled initially */
				    NULL);    /* unnamed */

  /* Create a manual-reset event. */
  cv->events[BROADCAST] = CreateEvent (NULL,  /* no security */
				       TRUE,  /* manual-reset */
				       FALSE, /* non-signaled initially */
				       NULL); /* unnamed */

  return 0;
}

int 
pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex)
{
  int result, last_waiter;

  /* Ensure we have a valid cond_t variable. */
  if (cv == NULL)
    {
      return EINVAL;
    }

  /* Avoid race conditions. */
  EnterCriticalSection (&cv->waiters_count_lock);
  cv->waiters_count_++;
  LeaveCriticalSection (&cv->waiters_count_lock);

  /* It's okay to release the mutex here since Win32 manual-reset
     events maintain state when used with SetEvent().  This avoids the
     "lost wakeup" bug. */

  pthread_mutex_unlock(mutex);

  /* Wait for either event to become signaled due to
     pthread_cond_signal() being called or pthread_cond_broadcast()
     being called. */
 
  result = WaitForMultipleObjects (2, ev->events, FALSE, INFINITE);

  EnterCriticalSection (&cv->waiters_count_lock);
  cv->waiters_count--;
  last_waiter = cv->waiters_count == 0;
  LeaveCriticalSection (&cv->waiters_count_lock);

  /* Some thread called pthread_cond_broadcast(). */
  if ((result = WAIT_OBJECT_0 + BROADCAST) && last_waiter)
    {
      /* We're the last waiter to be notified, so reset the manual
	 event. */
      ResetEvent(cv->events[BROADCAST]);
    }

  /* Reacquire the mutex. */
  pthread_mutex_lock(mutex);

  return 0;
}

int
pthread_cond_timedwait(pthread_cond_t *cv, 
		       pthread_mutex_t *mutex,
		       const struct timespec *abstime)
{
  /* Yet to be implemented.  This will be identical to cond_wait(),
     but we will need to get the timeout parameter in the call to
     WaitForMultipleObject() as close to the time specified in
     `abstime' as possible. */

  return 0;
}

int 
pthread_cond_broadcast (pthread_cond_t *cv)
{
  int have_waiters;

  /* Ensure we have a valid cond_t variable. */
  if (cv == NULL)
    {
      return EINVAL;
    }

  /* Avoid race conditions. */
  EnterCriticalSection (&cv->waiters_count_lock_);
  have_waiters = (cv->waiters_count > 0);
  LeaveCriticalSection (&cv->waiters_count_lock_);

  if (have_waiters) {
    SetEvent(cv->events[BROADCAST]);
  }

  return 0;
}

int 
pthread_cond_signal (pthread_cond_t *cv)
{
  int have_waiters;

  /* Ensure we have a valid cond_t variable. */
  if (cv == NULL)
    {
      return EINVAL;
    }

  /* Avoid race conditions. */
  EnterCriticalSection (&cv->waiters_count_lock);
  have_waiters = (cv->waiters_count > 0);
  LeaveCriticalSection (&cv->waiters_count_lock);

  if (have_waiters) {
    SetEvent(cv->events[SIGNAL]);
  }

  return 0;
}

int
pthread_cond_destroy(pthread_cond_t *cv)
{
  if (cv == NULL)
    {
	return EINVAL;
    }

  return pthread_mutex_destroy(cv->waiters_count_lock);
}