Xenomai  3.1
synch.h
1 /*
2  * Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * Xenomai is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * Xenomai is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Xenomai; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
19 #ifndef _COBALT_KERNEL_SYNCH_H
20 #define _COBALT_KERNEL_SYNCH_H
21 
22 #include <cobalt/kernel/list.h>
23 #include <cobalt/kernel/assert.h>
24 #include <cobalt/kernel/timer.h>
25 #include <cobalt/uapi/kernel/synch.h>
26 #include <cobalt/uapi/kernel/thread.h>
27 
32 #define XNSYNCH_CLAIMED 0x100 /* Claimed by other thread(s) (PI) */
33 #define XNSYNCH_CEILING 0x200 /* Actively boosting (PP) */
34 
35 /* Spare flags usable by upper interfaces */
36 #define XNSYNCH_SPARE0 0x01000000
37 #define XNSYNCH_SPARE1 0x02000000
38 #define XNSYNCH_SPARE2 0x04000000
39 #define XNSYNCH_SPARE3 0x08000000
40 #define XNSYNCH_SPARE4 0x10000000
41 #define XNSYNCH_SPARE5 0x20000000
42 #define XNSYNCH_SPARE6 0x40000000
43 #define XNSYNCH_SPARE7 0x80000000
44 
45 /* Statuses */
46 #define XNSYNCH_DONE 0 /* Resource available / operation complete */
47 #define XNSYNCH_WAIT 1 /* Calling thread blocked -- start rescheduling */
48 #define XNSYNCH_RESCHED 2 /* Force rescheduling */
49 
50 struct xnthread;
51 struct xnsynch;
52 
53 struct xnsynch {
55  int wprio;
57  struct list_head next;
62  u32 *ceiling_ref;
64  unsigned long status;
66  struct list_head pendq;
68  struct xnthread *owner;
70  atomic_t *fastlock;
71  /* Cleanup handler */
72  void (*cleanup)(struct xnsynch *synch);
73 };
74 
75 #define XNSYNCH_WAITQUEUE_INITIALIZER(__name) { \
76  .status = XNSYNCH_PRIO, \
77  .wprio = -1, \
78  .pendq = LIST_HEAD_INIT((__name).pendq), \
79  .owner = NULL, \
80  .cleanup = NULL, \
81  .fastlock = NULL, \
82  }
83 
84 #define DEFINE_XNWAITQ(__name) \
85  struct xnsynch __name = XNSYNCH_WAITQUEUE_INITIALIZER(__name)
86 
87 static inline void xnsynch_set_status(struct xnsynch *synch, int bits)
88 {
89  synch->status |= bits;
90 }
91 
92 static inline void xnsynch_clear_status(struct xnsynch *synch, int bits)
93 {
94  synch->status &= ~bits;
95 }
96 
97 #define xnsynch_for_each_sleeper(__pos, __synch) \
98  list_for_each_entry(__pos, &(__synch)->pendq, plink)
99 
100 #define xnsynch_for_each_sleeper_safe(__pos, __tmp, __synch) \
101  list_for_each_entry_safe(__pos, __tmp, &(__synch)->pendq, plink)
102 
103 static inline int xnsynch_pended_p(struct xnsynch *synch)
104 {
105  return !list_empty(&synch->pendq);
106 }
107 
108 static inline struct xnthread *xnsynch_owner(struct xnsynch *synch)
109 {
110  return synch->owner;
111 }
112 
113 #define xnsynch_fastlock(synch) ((synch)->fastlock)
114 #define xnsynch_fastlock_p(synch) ((synch)->fastlock != NULL)
115 #define xnsynch_owner_check(synch, thread) \
116  xnsynch_fast_owner_check((synch)->fastlock, thread->handle)
117 
118 #ifdef CONFIG_XENO_OPT_DEBUG_MUTEX_RELAXED
119 
120 void xnsynch_detect_relaxed_owner(struct xnsynch *synch,
121  struct xnthread *sleeper);
122 
123 void xnsynch_detect_boosted_relax(struct xnthread *owner);
124 
125 #else /* !CONFIG_XENO_OPT_DEBUG_MUTEX_RELAXED */
126 
127 static inline void xnsynch_detect_relaxed_owner(struct xnsynch *synch,
128  struct xnthread *sleeper) { }
129 
130 static inline void xnsynch_detect_boosted_relax(struct xnthread *owner) { }
131 
132 #endif /* !CONFIG_XENO_OPT_DEBUG_MUTEX_RELAXED */
133 
134 void xnsynch_init(struct xnsynch *synch, int flags,
135  atomic_t *fastlock);
136 
137 void xnsynch_init_protect(struct xnsynch *synch, int flags,
138  atomic_t *fastlock, u32 *ceiling_ref);
139 
140 int xnsynch_destroy(struct xnsynch *synch);
141 
142 void xnsynch_commit_ceiling(struct xnthread *curr);
143 
144 static inline void xnsynch_register_cleanup(struct xnsynch *synch,
145  void (*handler)(struct xnsynch *))
146 {
147  synch->cleanup = handler;
148 }
149 
150 int __must_check xnsynch_sleep_on(struct xnsynch *synch,
151  xnticks_t timeout,
152  xntmode_t timeout_mode);
153 
154 struct xnthread *xnsynch_wakeup_one_sleeper(struct xnsynch *synch);
155 
156 int xnsynch_wakeup_many_sleepers(struct xnsynch *synch, int nr);
157 
158 void xnsynch_wakeup_this_sleeper(struct xnsynch *synch,
159  struct xnthread *sleeper);
160 
161 int __must_check xnsynch_acquire(struct xnsynch *synch,
162  xnticks_t timeout,
163  xntmode_t timeout_mode);
164 
165 int __must_check xnsynch_try_acquire(struct xnsynch *synch);
166 
167 bool xnsynch_release(struct xnsynch *synch, struct xnthread *thread);
168 
169 struct xnthread *xnsynch_peek_pendq(struct xnsynch *synch);
170 
171 int xnsynch_flush(struct xnsynch *synch, int reason);
172 
173 void xnsynch_requeue_sleeper(struct xnthread *thread);
174 
175 void xnsynch_forget_sleeper(struct xnthread *thread);
176 
179 #endif /* !_COBALT_KERNEL_SYNCH_H_ */
int xnsynch_flush(struct xnsynch *synch, int reason)
Unblock all waiters pending on a resource.
Definition: synch.c:1076
int xnsynch_destroy(struct xnsynch *synch)
Destroy a synchronization object.
Definition: synch.c:160
int __must_check xnsynch_acquire(struct xnsynch *synch, xnticks_t timeout, xntmode_t timeout_mode)
Acquire the ownership of a synchronization object.
Definition: synch.c:668
void xnsynch_wakeup_this_sleeper(struct xnsynch *synch, struct xnthread *sleeper)
Unblock a particular thread from wait.
Definition: synch.c:338
struct xnthread * xnsynch_wakeup_one_sleeper(struct xnsynch *synch)
Unblock the heading thread from wait.
Definition: synch.c:261
void xnsynch_init_protect(struct xnsynch *synch, int flags, atomic_t *fastlock, u32 *ceiling_ref)
Initialize a synchronization object enforcing PP.
Definition: synch.c:135
int __must_check xnsynch_sleep_on(struct xnsynch *synch, xnticks_t timeout, xntmode_t timeout_mode)
Sleep on an ownerless synchronization object.
Definition: synch.c:206
void xnsynch_init(struct xnsynch *synch, int flags, atomic_t *fastlock)
Initialize a synchronization object.
Definition: synch.c:91
int __must_check xnsynch_try_acquire(struct xnsynch *synch)
Try acquiring the ownership of a synchronization object.
Definition: synch.c:602
Copyright © 2011 Gilles Chanteperdrix gilles.chanteperdrix@xenomai.org.
Definition: atomic.h:24
bool xnsynch_release(struct xnsynch *synch, struct xnthread *thread)
Release a resource and pass it to the next waiting thread.
Definition: synch.c:927
struct xnthread * xnsynch_peek_pendq(struct xnsynch *synch)
Access the thread leading a synch object wait queue.
Definition: synch.c:1021