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

/*
 * Copyright (c) 2009-2014 embedded brains GmbH. All rights reserved.
 *
 *  Dornierstr. 4
 *  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.
 */

#include <machine/rtems-bsd-kernel-space.h>
#include <machine/rtems-bsd-support.h>

#include <rtems/score/threaddispatch.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/threadqimpl.h>

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

void
cv_init(struct cv *cv, const char *desc)
{

	cv->cv_description = desc;
	_Thread_queue_Initialize(&cv->cv_waiters, THREAD_QUEUE_DISCIPLINE_PRIORITY,
	    STATES_WAITING_FOR_CONDITION_VARIABLE, EWOULDBLOCK);
}

void
cv_destroy(struct cv *cv)
{

	BSD_ASSERT(_Thread_queue_First(&cv->cv_waiters) == NULL);
}

static int
_cv_wait_support(struct cv *cv, struct lock_object *lock, Watchdog_Interval timo, bool relock)
{
	int error;
	struct lock_class *class;
	int lock_state;
	Thread_Control *executing;

	_Thread_Disable_dispatch();

	class = LOCK_CLASS(lock);
	lock_state = (*class->lc_unlock)(lock);

	_Thread_queue_Enter_critical_section(&cv->cv_waiters);

	executing = _Thread_Executing;
	executing->Wait.return_code = 0;
	executing->Wait.queue = &cv->cv_waiters;

	_Thread_queue_Enqueue(&cv->cv_waiters, executing, timo);

	DROP_GIANT();

	_Thread_Enable_dispatch();

	PICKUP_GIANT();

	error = (int)executing->Wait.return_code;

	if (relock) {
		(*class->lc_lock)(lock, lock_state);
	}

	return (error);
}

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, (Watchdog_Interval)timo, true));
}

void
cv_signal(struct cv *cv)
{

	_Thread_Disable_dispatch();
	_Thread_queue_Dequeue(&cv->cv_waiters);
	_Thread_Enable_dispatch();
}

void
cv_broadcastpri(struct cv *cv, int pri)
{

	_Thread_Disable_dispatch();

	while (_Thread_queue_Dequeue(&cv->cv_waiters) != NULL) {
		/* Again */
	}

	_Thread_Enable_dispatch();
}

int
_cv_wait_sig(struct cv *cv, struct lock_object *lock)
{

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

int
_cv_timedwait_sig(struct cv *cv, struct lock_object *lock, int timo)
{

	if (timo <= 0)
		timo = 1;

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