aboutsummaryrefslogtreecommitdiffstats
path: root/erts/lib_src/ose/ethr_event.c
blob: 5905fe22a9d1ffcc466bb8fc0e022930c3c3e12d (plain) (blame)
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
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2009-2010. All Rights Reserved.
 *
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * %CopyrightEnd%
 */

/*
 * Author: Rickard Green
 */

#define ETHR_INLINE_FUNC_NAME_(X) X ## __
#define ETHR_EVENT_IMPL__

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ethread.h"

#ifdef USE_PTHREAD_API

int
ethr_event_init(ethr_event *e)
{
    int res;
    ethr_atomic32_init(&e->state, ETHR_EVENT_OFF__);
    res = pthread_mutex_init(&e->mtx, NULL);
    if (res != 0)
	return res;
    res = pthread_cond_init(&e->cnd, NULL);
    if (res != 0) {
	pthread_mutex_destroy(&e->mtx);
	return res;
    }
    return 0;
}

int
ethr_event_destroy(ethr_event *e)
{
    int res;
    res = pthread_mutex_destroy(&e->mtx);
    if (res != 0)
	return res;
    res = pthread_cond_destroy(&e->cnd);
    if (res != 0)
	return res;
    return 0;
}

static ETHR_INLINE int
wait__(ethr_event *e, int spincount)
{
    int sc = spincount;
    ethr_sint32_t val;
    int res, ulres;
    int until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;

    if (spincount < 0)
	ETHR_FATAL_ERROR__(EINVAL);

    while (1) {
	val = ethr_atomic32_read(&e->state);
	if (val == ETHR_EVENT_ON__)
	    return 0;
	if (sc == 0)
	    break;
	sc--;
	ETHR_SPIN_BODY;
	if (--until_yield == 0) {
	    until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
	    res = ETHR_YIELD();
	    if (res != 0)
		ETHR_FATAL_ERROR__(res);
	}
    }

    if (val != ETHR_EVENT_OFF_WAITER__) {
	val = ethr_atomic32_cmpxchg(&e->state,
				    ETHR_EVENT_OFF_WAITER__,
				    ETHR_EVENT_OFF__);
	if (val == ETHR_EVENT_ON__)
	    return 0;
	ETHR_ASSERT(val == ETHR_EVENT_OFF__);
    }

    ETHR_ASSERT(val == ETHR_EVENT_OFF_WAITER__
		|| val == ETHR_EVENT_OFF__);

    res = pthread_mutex_lock(&e->mtx);
    if (res != 0)
	ETHR_FATAL_ERROR__(res);

    while (1) {

	val = ethr_atomic32_read(&e->state);
	if (val == ETHR_EVENT_ON__)
	    break;

	res = pthread_cond_wait(&e->cnd, &e->mtx);
	if (res == EINTR)
	    break;
	if (res != 0)
	    ETHR_FATAL_ERROR__(res);
    }

    ulres = pthread_mutex_unlock(&e->mtx);
    if (ulres != 0)
	ETHR_FATAL_ERROR__(ulres);

    return res; /* 0 || EINTR */
}

#else
/* --- OSE implementation of events ---------------------------- */

#ifdef DEBUG
union SIGNAL {
  SIGSELECT signo;
};
#endif

int
ethr_event_init(ethr_event *e)
{
    ethr_atomic32_init(&e->state, ETHR_EVENT_OFF__);
    e->proc = current_process();
    return 0;
}

int
ethr_event_destroy(ethr_event *e)
{
  return 0;
}

static ETHR_INLINE int
wait__(ethr_event *e, int spincount)
{
    int sc = spincount;
    int res;
    int until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;

    if (spincount < 0)
	ETHR_FATAL_ERROR__(EINVAL);

    ETHR_ASSERT(e->proc == current_process());
    ETHR_ASSERT(get_fsem(current_process()) == 0);

    while (1) {
      ethr_sint32_t val;
      while (1) {
        val = ethr_atomic32_read(&e->state);
	if (val == ETHR_EVENT_ON__)
	  return 0;
	if (sc == 0)
	  break;
	sc--;
	ETHR_SPIN_BODY;
	if (--until_yield == 0) {
	  until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
	  res = ETHR_YIELD();
	  if (res != 0)
	    ETHR_FATAL_ERROR__(res);
	}
      }
      if (val != ETHR_EVENT_OFF_WAITER__) {
	val = ethr_atomic32_cmpxchg(&e->state,
				    ETHR_EVENT_OFF_WAITER__,
				    ETHR_EVENT_OFF__);
	if (val == ETHR_EVENT_ON__)
	  return 0;
	ETHR_ASSERT(val == ETHR_EVENT_OFF__);
      }

#if defined(DEBUG)
      while (1) {
	/* In debug we also receive any signals to make sure that we do
	   not get any! redir tables should send all to scheduler_1 */
	SIGSELECT sigsel[] = {0};
	union SIGNAL *sig = receive_fsem(OSE_NO_TIMEOUT,sigsel,1);
	//ETHR_ASSERT(sig == OS_RCV_FSEM);
	if (sig != OS_RCV_FSEM) {
	  int i;
	  printf("0x%x: Got signal in wait: %u ",current_process(),sig->signo);
	  for (i = 0; i < (sigsize(&sig) / sizeof(SIGSELECT)) && i < 5; i++) {
	    printf("%x ",sig[i+1]);
	  }
	  printf("\n");
	} else
	  break;
      }
#else
      wait_fsem(1);
#endif
      ETHR_ASSERT(get_fsem(current_process()) == 0);
    }
}

#endif

void
ethr_event_reset(ethr_event *e)
{
    ethr_event_reset__(e);
}

void
ethr_event_set(ethr_event *e)
{
    ethr_event_set__(e);
}

int
ethr_event_wait(ethr_event *e)
{
    return wait__(e, 0);
}

int
ethr_event_swait(ethr_event *e, int spincount)
{
    return wait__(e, spincount);
}