summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/include/machine/rtems-bsd-muteximpl.h
blob: 644f6f5db4094ea92b6502b96d49dba97383108f (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
/**
 * @file
 *
 * @ingroup rtems_bsd_machine
 *
 * @brief Implementation of a mutex with a simple priority inheritance
 * protocol.
 */

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

#ifndef _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_
#define _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_

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

#include <rtems/bsd/sys/types.h>
#include <rtems/bsd/sys/lock.h>

#include <rtems/score/threadimpl.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

static inline void
rtems_bsd_mutex_init(struct lock_object *lock, rtems_bsd_mutex *m,
    struct lock_class *class, const char *name, const char *type, int flags)
{
	_ISR_lock_Initialize(&m->lock, name);
	m->owner = NULL;
	m->nest_level = 0;
	_RBTree_Initialize_empty(&m->rivals);

	lock_init(lock, class, name, type, flags);
}

void rtems_bsd_mutex_lock_more(struct lock_object *lock, rtems_bsd_mutex *m,
    Thread_Control *owner, Thread_Control *executing,
    ISR_lock_Context *lock_context);

static inline void
rtems_bsd_mutex_lock(struct lock_object *lock, rtems_bsd_mutex *m)
{
	ISR_lock_Context lock_context;
	Thread_Control *executing;
	Thread_Control *owner;

	_ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);

	owner = m->owner;
	executing = _Thread_Executing;

	if (__predict_true(owner == NULL)) {
		m->owner = executing;
		++executing->resource_count;

		_ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
	} else {
		rtems_bsd_mutex_lock_more(lock, m, owner, executing,
		    &lock_context);
	}
}

static inline int
rtems_bsd_mutex_trylock(struct lock_object *lock, rtems_bsd_mutex *m)
{
	int success;
	ISR_lock_Context lock_context;
	Thread_Control *executing;
	Thread_Control *owner;

	_ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);

	owner = m->owner;
	executing = _Thread_Executing;

	if (owner == NULL) {
		m->owner = executing;
		++executing->resource_count;
		success = 1;
	} else if (owner == executing) {
		BSD_ASSERT(lock->lo_flags & LO_RECURSABLE);
		++m->nest_level;
		success = 1;
	} else {
		success = 0;
	}

	_ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);

	return (success);
}

void rtems_bsd_mutex_unlock_more(rtems_bsd_mutex *m, Thread_Control *owner,
    int keep_priority, RBTree_Node *first, ISR_lock_Context *lock_context);

static inline void
rtems_bsd_mutex_unlock(rtems_bsd_mutex *m)
{
	ISR_lock_Context lock_context;
	Thread_Control *owner;
	int nest_level;

	_ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);

	nest_level = m->nest_level;
	owner = m->owner;

	BSD_ASSERT(owner == _Thread_Executing);

	if (__predict_true(nest_level == 0)) {
		RBTree_Node *first = _RBTree_First(&m->rivals, RBT_LEFT);
		int keep_priority;

		--owner->resource_count;
		keep_priority = _Thread_Owns_resources(owner)
		    || owner->real_priority == owner->current_priority;

		m->owner = NULL;

		if (__predict_true(first == NULL && keep_priority)) {
			_ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
		} else {
			rtems_bsd_mutex_unlock_more(m, owner, keep_priority,
			    first, &lock_context);
		}

	} else {
		m->nest_level = nest_level - 1;

		_ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
	}
}

static inline int
rtems_bsd_mutex_owned(rtems_bsd_mutex *m)
{

	return (m->owner == _Thread_Get_executing());
}

static inline int
rtems_bsd_mutex_recursed(rtems_bsd_mutex *m)
{

	return (m->nest_level);
}

static inline void
rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m)
{
	BSD_ASSERT(_RBTree_Is_empty(&m->rivals));

	if (rtems_bsd_mutex_owned(m)) {
		m->nest_level = 0;
		rtems_bsd_mutex_unlock(m);
	}

	_ISR_lock_Destroy(&m->lock);
	lock_destroy(lock);
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ */