source: rtems-libbsd/rtemsbsd/include/machine/rtems-bsd-muteximpl.h @ c0b0b3e

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since c0b0b3e was c0b0b3e, checked in by Sebastian Huber <sebastian.huber@…>, on 04/23/15 at 12:55:42

rtems-bsd-mutex: Update due to API changes

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_machine
5 *
6 * @brief Implementation of a mutex with a simple priority inheritance
7 * protocol.
8 */
9
10/*
11 * Copyright (c) 2014, 2015 embedded brains GmbH.  All rights reserved.
12 *
13 *  embedded brains GmbH
14 *  Dornierstr. 4
15 *  82178 Puchheim
16 *  Germany
17 *  <rtems@embedded-brains.de>
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 *    notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 *    notice, this list of conditions and the following disclaimer in the
26 *    documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#ifndef _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_
42#define _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_
43
44#include <machine/rtems-bsd-mutex.h>
45#include <machine/rtems-bsd-support.h>
46
47#include <rtems/bsd/sys/types.h>
48#include <rtems/bsd/sys/lock.h>
49
50#include <rtems/score/threadimpl.h>
51
52#ifdef __cplusplus
53extern "C" {
54#endif /* __cplusplus */
55
56static inline void
57rtems_bsd_mutex_init(struct lock_object *lock, rtems_bsd_mutex *m,
58    struct lock_class *class, const char *name, const char *type, int flags)
59{
60        _ISR_lock_Initialize(&m->lock, name);
61        m->owner = NULL;
62        m->nest_level = 0;
63        _RBTree_Initialize_empty(&m->rivals);
64
65        lock_init(lock, class, name, type, flags);
66}
67
68void rtems_bsd_mutex_lock_more(struct lock_object *lock, rtems_bsd_mutex *m,
69    Thread_Control *owner, Thread_Control *executing,
70    ISR_lock_Context *lock_context);
71
72static inline void
73rtems_bsd_mutex_lock(struct lock_object *lock, rtems_bsd_mutex *m)
74{
75        ISR_lock_Context lock_context;
76        Thread_Control *executing;
77        Thread_Control *owner;
78
79        _ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);
80
81        owner = m->owner;
82        executing = _Thread_Executing;
83
84        if (__predict_true(owner == NULL)) {
85                m->owner = executing;
86                ++executing->resource_count;
87
88                _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
89        } else {
90                rtems_bsd_mutex_lock_more(lock, m, owner, executing,
91                    &lock_context);
92        }
93}
94
95static inline int
96rtems_bsd_mutex_trylock(struct lock_object *lock, rtems_bsd_mutex *m)
97{
98        int success;
99        ISR_lock_Context lock_context;
100        Thread_Control *executing;
101        Thread_Control *owner;
102
103        _ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);
104
105        owner = m->owner;
106        executing = _Thread_Executing;
107
108        if (owner == NULL) {
109                m->owner = executing;
110                ++executing->resource_count;
111                success = 1;
112        } else if (owner == executing) {
113                BSD_ASSERT(lock->lo_flags & LO_RECURSABLE);
114                ++m->nest_level;
115                success = 1;
116        } else {
117                success = 0;
118        }
119
120        _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
121
122        return (success);
123}
124
125void rtems_bsd_mutex_unlock_more(rtems_bsd_mutex *m, Thread_Control *owner,
126    int keep_priority, RBTree_Node *first, ISR_lock_Context *lock_context);
127
128static inline void
129rtems_bsd_mutex_unlock(rtems_bsd_mutex *m)
130{
131        ISR_lock_Context lock_context;
132        Thread_Control *owner;
133        int nest_level;
134
135        _ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);
136
137        nest_level = m->nest_level;
138        owner = m->owner;
139
140        BSD_ASSERT(owner == _Thread_Executing);
141
142        if (__predict_true(nest_level == 0)) {
143                RBTree_Node *first = _RBTree_First(&m->rivals, RBT_LEFT);
144                int keep_priority;
145
146                --owner->resource_count;
147                keep_priority = _Thread_Owns_resources(owner)
148                    || owner->real_priority == owner->current_priority;
149
150                m->owner = NULL;
151
152                if (__predict_true(first == NULL && keep_priority)) {
153                        _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
154                } else {
155                        rtems_bsd_mutex_unlock_more(m, owner, keep_priority,
156                            first, &lock_context);
157                }
158
159        } else {
160                m->nest_level = nest_level - 1;
161
162                _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
163        }
164}
165
166static inline int
167rtems_bsd_mutex_owned(rtems_bsd_mutex *m)
168{
169
170        return (m->owner == _Thread_Get_executing());
171}
172
173static inline int
174rtems_bsd_mutex_recursed(rtems_bsd_mutex *m)
175{
176
177        return (m->nest_level);
178}
179
180static inline void
181rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m)
182{
183        BSD_ASSERT(_RBTree_Is_empty(&m->rivals));
184
185        if (rtems_bsd_mutex_owned(m)) {
186                m->nest_level = 0;
187                rtems_bsd_mutex_unlock(m);
188        }
189
190        _ISR_lock_Destroy(&m->lock);
191        lock_destroy(lock);
192}
193
194#ifdef __cplusplus
195}
196#endif /* __cplusplus */
197
198#endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ */
Note: See TracBrowser for help on using the repository browser.