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

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 02d344f4 was 02d344f4, checked in by Sebastian Huber <sebastian.huber@…>, on 09/22/16 at 05:22: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, 2016 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 <sys/types.h>
48#include <rtems/bsd/sys/lock.h>
49
50#include <rtems/score/threadimpl.h>
51#include <rtems/score/threadqimpl.h>
52
53#ifdef __cplusplus
54extern "C" {
55#endif /* __cplusplus */
56
57#define RTEMS_BSD_MUTEX_TQ_OPERATIONS \
58    &_Thread_queue_Operations_priority_inherit
59
60static inline void
61rtems_bsd_mutex_init(struct lock_object *lock, rtems_bsd_mutex *m,
62    struct lock_class *class, const char *name, const char *type, int flags)
63{
64        _Thread_queue_Initialize(&m->queue);
65        m->nest_level = 0;
66
67        lock_init(lock, class, name, type, flags);
68}
69
70void rtems_bsd_mutex_lock_more(struct lock_object *lock, rtems_bsd_mutex *m,
71    Thread_Control *owner, Thread_Control *executing,
72    Thread_queue_Context *queue_context);
73
74static inline void
75rtems_bsd_mutex_lock(struct lock_object *lock, rtems_bsd_mutex *m)
76{
77        Thread_queue_Context queue_context;
78        Thread_Control *executing;
79        Thread_Control *owner;
80
81        _Thread_queue_Context_initialize(&queue_context);
82        _Thread_queue_Acquire(&m->queue, &queue_context);
83
84        owner = m->queue.Queue.owner;
85        executing = _Thread_Executing;
86
87        if (__predict_true(owner == NULL)) {
88                m->queue.Queue.owner = executing;
89                ++executing->resource_count;
90
91                _Thread_queue_Release(&m->queue, &queue_context);
92        } else {
93                rtems_bsd_mutex_lock_more(lock, m, owner, executing,
94                    &queue_context);
95        }
96}
97
98static inline int
99rtems_bsd_mutex_trylock(struct lock_object *lock, rtems_bsd_mutex *m)
100{
101        int success;
102        Thread_queue_Context queue_context;
103        Thread_Control *executing;
104        Thread_Control *owner;
105
106        _Thread_queue_Context_initialize(&queue_context);
107        _Thread_queue_Acquire(&m->queue, &queue_context);
108
109        owner = m->queue.Queue.owner;
110        executing = _Thread_Executing;
111
112        if (owner == NULL) {
113                m->queue.Queue.owner = executing;
114                ++executing->resource_count;
115                success = 1;
116        } else if (owner == executing) {
117                BSD_ASSERT(lock->lo_flags & LO_RECURSABLE);
118                ++m->nest_level;
119                success = 1;
120        } else {
121                success = 0;
122        }
123
124        _Thread_queue_Release(&m->queue, &queue_context);
125
126        return (success);
127}
128
129static inline void
130rtems_bsd_mutex_unlock(rtems_bsd_mutex *m)
131{
132        Thread_queue_Context queue_context;
133        Thread_Control *owner;
134        int nest_level;
135
136        _Thread_queue_Context_initialize(&queue_context);
137        _Thread_queue_Acquire(&m->queue, &queue_context);
138
139        nest_level = m->nest_level;
140        owner = m->queue.Queue.owner;
141
142        BSD_ASSERT(owner == _Thread_Executing);
143
144        if (__predict_true(nest_level == 0)) {
145                Thread_queue_Heads *heads;
146
147                heads = m->queue.Queue.heads;
148                m->queue.Queue.owner = NULL;
149                --owner->resource_count;
150
151                if (__predict_true(heads == NULL)) {
152                        _Thread_queue_Release(&m->queue, &queue_context);
153                } else {
154                        _Thread_queue_Surrender(&m->queue.Queue, heads, owner,
155                            &queue_context, RTEMS_BSD_MUTEX_TQ_OPERATIONS);
156                }
157        } else {
158                m->nest_level = nest_level - 1;
159
160                _Thread_queue_Release(&m->queue, &queue_context);
161        }
162}
163
164static inline int
165rtems_bsd_mutex_owned(rtems_bsd_mutex *m)
166{
167
168        return (m->queue.Queue.owner == _Thread_Get_executing());
169}
170
171static inline int
172rtems_bsd_mutex_recursed(rtems_bsd_mutex *m)
173{
174
175        return (m->nest_level);
176}
177
178static inline void
179rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m)
180{
181        BSD_ASSERT(m->queue.Queue.heads == NULL);
182
183        if (rtems_bsd_mutex_owned(m)) {
184                m->nest_level = 0;
185                rtems_bsd_mutex_unlock(m);
186        }
187
188        _Thread_queue_Destroy(&m->queue);
189        lock_destroy(lock);
190}
191
192#ifdef __cplusplus
193}
194#endif /* __cplusplus */
195
196#endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ */
Note: See TracBrowser for help on using the repository browser.