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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since b3ff71e was b3ff71e, checked in by Sebastian Huber <sebastian.huber@…>, on 04/30/15 at 08:41:34

rtems-bsd-mutex: Use standard thread queues

  • Property mode set to 100644
File size: 5.3 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        _Thread_queue_Initialize(&m->queue, THREAD_QUEUE_DISCIPLINE_PRIORITY);
61        m->owner = NULL;
62        m->nest_level = 0;
63
64        lock_init(lock, class, name, type, flags);
65}
66
67void rtems_bsd_mutex_lock_more(struct lock_object *lock, rtems_bsd_mutex *m,
68    Thread_Control *owner, Thread_Control *executing,
69    ISR_lock_Context *lock_context);
70
71static inline void
72rtems_bsd_mutex_lock(struct lock_object *lock, rtems_bsd_mutex *m)
73{
74        ISR_lock_Context lock_context;
75        Thread_Control *executing;
76        Thread_Control *owner;
77
78        _Thread_queue_Acquire(&m->queue, &lock_context);
79
80        owner = m->owner;
81        executing = _Thread_Executing;
82        ++executing->resource_count;
83
84        if (__predict_true(owner == NULL)) {
85                m->owner = executing;
86
87                _Thread_queue_Release(&m->queue, &lock_context);
88        } else {
89                rtems_bsd_mutex_lock_more(lock, m, owner, executing,
90                    &lock_context);
91        }
92}
93
94static inline int
95rtems_bsd_mutex_trylock(struct lock_object *lock, rtems_bsd_mutex *m)
96{
97        int success;
98        ISR_lock_Context lock_context;
99        Thread_Control *executing;
100        Thread_Control *owner;
101
102        _Thread_queue_Acquire(&m->queue, &lock_context);
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        _Thread_queue_Release(&m->queue, &lock_context);
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_lock_Context *lock_context);
126
127static inline void
128rtems_bsd_mutex_unlock(rtems_bsd_mutex *m)
129{
130        ISR_lock_Context lock_context;
131        Thread_Control *owner;
132        int nest_level;
133
134        _Thread_queue_Acquire(&m->queue, &lock_context);
135
136        nest_level = m->nest_level;
137        owner = m->owner;
138
139        BSD_ASSERT(owner == _Thread_Executing);
140
141        if (__predict_true(nest_level == 0)) {
142                RBTree_Node *first;
143                int keep_priority;
144
145                --owner->resource_count;
146
147                /*
148                 * Ensure that the owner resource count is visible to all other
149                 * processors and that we read the latest priority restore
150                 * hint.
151                 */
152                _Atomic_Fence( ATOMIC_ORDER_ACQ_REL );
153
154                first = _RBTree_First(&m->queue.Queues.Priority, RBT_LEFT);
155                keep_priority = _Thread_Owns_resources(owner)
156                    || !owner->priority_restore_hint;
157
158                m->owner = NULL;
159
160                if (__predict_true(first == NULL && keep_priority)) {
161                        _Thread_queue_Release(&m->queue, &lock_context);
162                } else {
163                        rtems_bsd_mutex_unlock_more(m, owner, keep_priority,
164                            first, &lock_context);
165                }
166
167        } else {
168                m->nest_level = nest_level - 1;
169
170                _Thread_queue_Release(&m->queue, &lock_context);
171        }
172}
173
174static inline int
175rtems_bsd_mutex_owned(rtems_bsd_mutex *m)
176{
177
178        return (m->owner == _Thread_Get_executing());
179}
180
181static inline int
182rtems_bsd_mutex_recursed(rtems_bsd_mutex *m)
183{
184
185        return (m->nest_level);
186}
187
188static inline void
189rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m)
190{
191        BSD_ASSERT(_RBTree_Is_empty(&m->queue.Queues.Priority));
192
193        if (rtems_bsd_mutex_owned(m)) {
194                m->nest_level = 0;
195                rtems_bsd_mutex_unlock(m);
196        }
197
198        _Thread_queue_Dequeue(&m->queue);
199        lock_destroy(lock);
200}
201
202#ifdef __cplusplus
203}
204#endif /* __cplusplus */
205
206#endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ */
Note: See TracBrowser for help on using the repository browser.