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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since be2e60c was 8176af1, checked in by Sebastian Huber <sebastian.huber@…>, on 09/24/14 at 13:41:16

rtems_bsd_mutex: Optimize

  • Property mode set to 100644
File size: 4.7 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 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/isrlevel.h>
51#include <rtems/score/threadimpl.h>
52#include <rtems/score/threadqimpl.h>
53
54#ifdef __cplusplus
55extern "C" {
56#endif /* __cplusplus */
57
58static inline void
59rtems_bsd_mutex_init(struct lock_object *lock, rtems_bsd_mutex *m,
60    struct lock_class *class, const char *name, const char *type, int flags)
61{
62        m->owner = NULL;
63        m->nest_level = 0;
64        _RBTree_Initialize_empty(&m->rivals);
65
66        lock_init(lock, class, name, type, flags);
67}
68
69void rtems_bsd_mutex_lock_more(struct lock_object *lock, rtems_bsd_mutex *m,
70    Thread_Control *owner, Thread_Control *executing, ISR_Level level);
71
72static inline void
73rtems_bsd_mutex_lock(struct lock_object *lock, rtems_bsd_mutex *m)
74{
75        ISR_Level level;
76        Thread_Control *executing;
77        Thread_Control *owner;
78
79        _ISR_Disable(level);
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_Enable(level);
89        } else {
90                rtems_bsd_mutex_lock_more(lock, m, owner, executing, level);
91        }
92}
93
94static inline int
95rtems_bsd_mutex_trylock(struct lock_object *lock, rtems_bsd_mutex *m)
96{
97        int success;
98        ISR_Level level;
99        Thread_Control *executing;
100        Thread_Control *owner;
101
102        _ISR_Disable(level);
103
104        owner = m->owner;
105        executing = _Thread_Executing;
106
107        if (owner == NULL) {
108                m->owner = executing;
109                ++executing->resource_count;
110                success = 1;
111        } else if (owner == executing) {
112                BSD_ASSERT(lock->lo_flags & LO_RECURSABLE);
113                ++m->nest_level;
114                success = 1;
115        } else {
116                success = 0;
117        }
118
119        _ISR_Enable(level);
120
121        return (success);
122}
123
124void rtems_bsd_mutex_unlock_more(rtems_bsd_mutex *m, Thread_Control *owner,
125    int keep_priority, RBTree_Node *first, ISR_Level level);
126
127static inline void
128rtems_bsd_mutex_unlock(rtems_bsd_mutex *m)
129{
130        ISR_Level level;
131        int nest_level;
132
133        _ISR_Disable(level);
134
135        nest_level = m->nest_level;
136        if (__predict_true(nest_level == 0)) {
137                RBTree_Node *first = _RBTree_First(&m->rivals, RBT_LEFT);
138                Thread_Control *owner = m->owner;
139                int keep_priority;
140
141                --owner->resource_count;
142                keep_priority = _Thread_Owns_resources(owner)
143                    || owner->real_priority == owner->current_priority;
144
145                m->owner = NULL;
146
147                if (__predict_true(first == NULL && keep_priority
148                    && owner == _Thread_Executing)) {
149                        _ISR_Enable(level);
150                } else {
151                        rtems_bsd_mutex_unlock_more(m, owner, keep_priority,
152                            first, level);
153                }
154
155        } else {
156                m->nest_level = nest_level - 1;
157
158                _ISR_Enable(level);
159        }
160}
161
162static inline int
163rtems_bsd_mutex_owned(rtems_bsd_mutex *m)
164{
165
166        return (m->owner == _Thread_Get_executing());
167}
168
169static inline int
170rtems_bsd_mutex_recursed(rtems_bsd_mutex *m)
171{
172
173        return (m->nest_level);
174}
175
176static inline void
177rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m)
178{
179        BSD_ASSERT(_RBTree_Is_empty(&m->rivals));
180
181        if (rtems_bsd_mutex_owned(m)) {
182                m->nest_level = 0;
183                rtems_bsd_mutex_unlock(m);
184        }
185
186        lock_destroy(lock);
187}
188
189#ifdef __cplusplus
190}
191#endif /* __cplusplus */
192
193#endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ */
Note: See TracBrowser for help on using the repository browser.