Xenomai  3.0.5
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 
31 #define XNSYNCH_CLAIMED 0x10 /* Claimed by other thread(s) w/ PIP */
32 
33 /* Spare flags usable by upper interfaces */
34 #define XNSYNCH_SPARE0 0x01000000
35 #define XNSYNCH_SPARE1 0x02000000
36 #define XNSYNCH_SPARE2 0x04000000
37 #define XNSYNCH_SPARE3 0x08000000
38 #define XNSYNCH_SPARE4 0x10000000
39 #define XNSYNCH_SPARE5 0x20000000
40 #define XNSYNCH_SPARE6 0x40000000
41 #define XNSYNCH_SPARE7 0x80000000
42 
43 /* Statuses */
44 #define XNSYNCH_DONE 0 /* Resource available / operation complete */
45 #define XNSYNCH_WAIT 1 /* Calling thread blocked -- start rescheduling */
46 #define XNSYNCH_RESCHED 2 /* Force rescheduling */
47 
48 struct xnthread;
49 struct xnsynch;
50 
51 struct xnsynch {
52  struct list_head link;
53  int wprio;
54  unsigned long status;
55  struct list_head pendq;
56  struct xnthread *owner;
57  atomic_t *fastlock;
58  void (*cleanup)(struct xnsynch *synch); /* Cleanup handler */
59 };
60 
61 #define XNSYNCH_WAITQUEUE_INITIALIZER(__name) { \
62  .status = XNSYNCH_PRIO, \
63  .wprio = -1, \
64  .pendq = LIST_HEAD_INIT((__name).pendq), \
65  .owner = NULL, \
66  .cleanup = NULL, \
67  .fastlock = NULL, \
68  }
69 
70 #define DEFINE_XNWAITQ(__name) \
71  struct xnsynch __name = XNSYNCH_WAITQUEUE_INITIALIZER(__name)
72 
73 static inline void xnsynch_set_status(struct xnsynch *synch, int bits)
74 {
75  synch->status |= bits;
76 }
77 
78 static inline void xnsynch_clear_status(struct xnsynch *synch, int bits)
79 {
80  synch->status &= ~bits;
81 }
82 
83 #define xnsynch_for_each_sleeper(__pos, __synch) \
84  list_for_each_entry(__pos, &(__synch)->pendq, plink)
85 
86 #define xnsynch_for_each_sleeper_safe(__pos, __tmp, __synch) \
87  list_for_each_entry_safe(__pos, __tmp, &(__synch)->pendq, plink)
88 
89 static inline int xnsynch_pended_p(struct xnsynch *synch)
90 {
91  return !list_empty(&synch->pendq);
92 }
93 
94 static inline struct xnthread *xnsynch_owner(struct xnsynch *synch)
95 {
96  return synch->owner;
97 }
98 
99 #define xnsynch_fastlock(synch) ((synch)->fastlock)
100 #define xnsynch_fastlock_p(synch) ((synch)->fastlock != NULL)
101 #define xnsynch_owner_check(synch, thread) \
102  xnsynch_fast_owner_check((synch)->fastlock, thread->handle)
103 
104 #if XENO_DEBUG(MUTEX_RELAXED)
105 
106 void xnsynch_detect_relaxed_owner(struct xnsynch *synch,
107  struct xnthread *sleeper);
108 
109 void xnsynch_detect_claimed_relax(struct xnthread *owner);
110 
111 #else /* !XENO_DEBUG(MUTEX_RELAXED) */
112 
113 static inline void xnsynch_detect_relaxed_owner(struct xnsynch *synch,
114  struct xnthread *sleeper)
115 {
116 }
117 
118 static inline void xnsynch_detect_claimed_relax(struct xnthread *owner)
119 {
120 }
121 
122 #endif /* !XENO_DEBUG(MUTEX_RELAXED) */
123 
124 void xnsynch_init(struct xnsynch *synch, int flags,
125  atomic_t *fastlock);
126 
127 int xnsynch_destroy(struct xnsynch *synch);
128 
129 static inline void xnsynch_set_owner(struct xnsynch *synch,
130  struct xnthread *thread)
131 {
132  synch->owner = thread;
133 }
134 
135 static inline void xnsynch_register_cleanup(struct xnsynch *synch,
136  void (*handler)(struct xnsynch *))
137 {
138  synch->cleanup = handler;
139 }
140 
141 int __must_check xnsynch_sleep_on(struct xnsynch *synch,
142  xnticks_t timeout,
143  xntmode_t timeout_mode);
144 
145 struct xnthread *xnsynch_wakeup_one_sleeper(struct xnsynch *synch);
146 
147 int xnsynch_wakeup_many_sleepers(struct xnsynch *synch, int nr);
148 
149 void xnsynch_wakeup_this_sleeper(struct xnsynch *synch,
150  struct xnthread *sleeper);
151 
152 int __must_check xnsynch_acquire(struct xnsynch *synch,
153  xnticks_t timeout,
154  xntmode_t timeout_mode);
155 
156 int __must_check xnsynch_try_acquire(struct xnsynch *synch);
157 
158 struct xnthread *xnsynch_release(struct xnsynch *synch,
159  struct xnthread *thread);
160 
161 struct xnthread *xnsynch_peek_pendq(struct xnsynch *synch);
162 
163 int xnsynch_flush(struct xnsynch *synch, int reason);
164 
165 void xnsynch_release_all_ownerships(struct xnthread *thread);
166 
167 void xnsynch_requeue_sleeper(struct xnthread *thread);
168 
169 void xnsynch_forget_sleeper(struct xnthread *thread);
170 
173 #endif /* !_COBALT_KERNEL_SYNCH_H_ */
int xnsynch_flush(struct xnsynch *synch, int reason)
Unblock all waiters pending on a resource.
Definition: synch.c:810
struct xnthread * xnsynch_release(struct xnsynch *synch, struct xnthread *thread)
Give the resource ownership to the next waiting thread.
Definition: synch.c:662
int xnsynch_destroy(struct xnsynch *synch)
Destroy a synchronization object.
Definition: synch.c:117
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:424
void xnsynch_wakeup_this_sleeper(struct xnsynch *synch, struct xnthread *sleeper)
Unblock a particular thread from wait.
Definition: synch.c:292
struct xnthread * xnsynch_wakeup_one_sleeper(struct xnsynch *synch)
Unblock the heading thread from wait.
Definition: synch.c:216
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:162
void xnsynch_init(struct xnsynch *synch, int flags, atomic_t *fastlock)
Initialize a synchronization object.
Definition: synch.c:81
int __must_check xnsynch_try_acquire(struct xnsynch *synch)
Try acquiring the ownership of a synchronization object.
Definition: synch.c:360
Copyright © 2011 Gilles Chanteperdrix gilles.chanteperdrix@xenomai.org.
Definition: atomic.h:24
struct xnthread * xnsynch_peek_pendq(struct xnsynch *synch)
Access the thread leading a synch object wait queue.
Definition: synch.c:750