summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/src/rtems-bsd-condvar.c
blob: 7c16940d9787c7e292901aafa8e21f33162673c3 (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
/**
 * @file
 *
 * @ingroup rtems_bsd_rtems
 *
 * @brief TODO.
 */

/*
 * Copyright (c) 2009, 2010 embedded brains GmbH.  
 * All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* Necessary to obtain some internal functions */
#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__

#include <freebsd/machine/rtems-bsd-config.h>

#include <rtems/posix/cond.h>

#include <freebsd/sys/param.h>
#include <freebsd/sys/types.h>
#include <freebsd/sys/systm.h>
#include <freebsd/sys/lock.h>
#include <freebsd/sys/mutex.h>
#include <freebsd/sys/condvar.h>

RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_condvar_chain);

void
cv_init(struct cv *cv, const char *desc)
{
	int rv = pthread_cond_init(&cv->cv_id, NULL);

	BSD_ASSERT_RV(rv);

	cv->cv_description = desc;

	rtems_chain_append(&rtems_bsd_condvar_chain, &cv->cv_node);
}

void
cv_destroy(struct cv *cv)
{
	int rv = pthread_cond_destroy(&cv->cv_id);

	BSD_ASSERT_RV(rv);

	rtems_chain_extract(&cv->cv_node);
}

static int _cv_wait_support(struct cv *cv, struct lock_object *lock, int timo, bool relock)
{
	rtems_status_code sc = RTEMS_SUCCESSFUL;
	int eno = 0;
	Objects_Locations location = OBJECTS_ERROR;
	POSIX_Condition_variables_Control *pcv = _POSIX_Condition_variables_Get(&cv->cv_id, &location);

	if (location == OBJECTS_LOCAL) {
		if (pcv->Mutex != POSIX_CONDITION_VARIABLES_NO_MUTEX && pcv->Mutex != lock->lo_id) {
			_Thread_Enable_dispatch();

			BSD_ASSERT(false);

			return EINVAL;
		}

		sc = rtems_semaphore_release(lock->lo_id);
		if (sc != RTEMS_SUCCESSFUL) {
			_Thread_Enable_dispatch();

			BSD_ASSERT(false);

			return EINVAL;
		}

		pcv->Mutex = lock->lo_id;

		_Thread_queue_Enter_critical_section(&pcv->Wait_queue);
		_Thread_Executing->Wait.return_code = 0;
		_Thread_Executing->Wait.queue = &pcv->Wait_queue;
		_Thread_Executing->Wait.id = cv->cv_id;

		/* FIXME: Integer conversion */
		_Thread_queue_Enqueue(&pcv->Wait_queue, (Watchdog_Interval) timo);

		DROP_GIANT();

		_Thread_Enable_dispatch();

		PICKUP_GIANT();

		eno = (int) _Thread_Executing->Wait.return_code;
		if (eno != 0) {
			if (eno == ETIMEDOUT) {
				eno = EWOULDBLOCK;
			} else {
				BSD_ASSERT(false);

				eno = EINVAL;
			}
		}

		if (relock) {
			sc = rtems_semaphore_obtain(lock->lo_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
			if (sc != RTEMS_SUCCESSFUL) {
				BSD_ASSERT(false);

				eno = EINVAL;
			}
		}

		return eno;
	}

	BSD_PANIC("unexpected object location");
}

void
_cv_wait(struct cv *cv, struct lock_object *lock)
{
	_cv_wait_support(cv, lock, 0, true);
}

void
_cv_wait_unlock(struct cv *cv, struct lock_object *lock)
{
	_cv_wait_support(cv, lock, 0, false);
}

int
_cv_timedwait(struct cv *cv, struct lock_object *lock, int timo)
{
	if (timo <= 0) {
		timo = 1;
	}

	return _cv_wait_support(cv, lock, timo, true);
}

void
cv_signal(struct cv *cv)
{
	int rv = pthread_cond_signal(&cv->cv_id);

	BSD_ASSERT_RV(rv);
}

void
cv_broadcastpri(struct cv *cv, int pri)
{
	int rv = 0;

	BSD_ASSERT(pri == 0);

	rv = pthread_cond_broadcast(&cv->cv_id);
	BSD_ASSERT_RV(rv);
}