Xenomai API  2.6.5
cb_lock.h
1 #ifndef CB_LOCK_H
2 #define CB_LOCK_H
3 
4 #include <asm/xenomai/atomic.h>
5 #include <nucleus/compiler.h>
6 #include <nucleus/types.h>
7 
8 #if !defined(__KERNEL__) && !defined(__XENO_SIM__)
9 typedef void xnthread_t;
10 #endif /* __KERNEL__ */
11 
12 #define __CLAIMED_BIT XN_HANDLE_SPARE3
13 
14 #define test_claimed(owner) xnhandle_test_spare(owner, __CLAIMED_BIT)
15 #define clear_claimed(owner) xnhandle_mask_spare(owner)
16 #define set_claimed(owner, bit) ({ \
17  xnhandle_t __tmp = xnhandle_mask_spare(owner); \
18  if (bit) \
19  xnhandle_set_spare(__tmp, __CLAIMED_BIT); \
20  __tmp; \
21 })
22 
23 #ifdef CONFIG_XENO_FASTSYNCH
24 
25 static __inline__ int __cb_try_read_lock(xnarch_atomic_t *lock)
26 {
27  unsigned val = xnarch_atomic_get(lock);
28  while (likely(val != -1)) {
29  unsigned old = xnarch_atomic_cmpxchg(lock, val, val + 1);
30  if (likely(old == val))
31  return 0;
32  val = old;
33  }
34  return -EBUSY;
35 }
36 
37 static __inline__ void __cb_read_unlock(xnarch_atomic_t *lock)
38 {
39  unsigned old, val = xnarch_atomic_get(lock);
40  while (likely(val != -1)) {
41  old = xnarch_atomic_cmpxchg(lock, val, val - 1);
42  if (likely(old == val))
43  return;
44  val = old;
45  }
46 }
47 
48 static __inline__ int __cb_try_write_lock(xnarch_atomic_t *lock)
49 {
50  unsigned old = xnarch_atomic_cmpxchg(lock, 0, -1);
51  if (unlikely(old))
52  return -EBUSY;
53  return 0;
54 }
55 
56 static __inline__ void __cb_force_write_lock(xnarch_atomic_t *lock)
57 {
58  xnarch_atomic_set(lock, -1);
59 }
60 
61 static __inline__ void __cb_write_unlock(xnarch_atomic_t *lock)
62 {
63  xnarch_atomic_set(lock, 0);
64 }
65 #define DECLARE_CB_LOCK_FLAGS(name) struct { } name __attribute__((unused))
66 #define cb_try_read_lock(lock, flags) __cb_try_read_lock(lock)
67 #define cb_read_unlock(lock, flags) __cb_read_unlock(lock)
68 #define cb_try_write_lock(lock, flags) __cb_try_write_lock(lock)
69 #define cb_force_write_lock(lock, flags) __cb_force_write_lock(lock)
70 #define cb_write_unlock(lock, flags) __cb_write_unlock(lock)
71 #else /* !CONFIG_XENO_FASTSYNCH */
72 #if defined(__KERNEL__) || defined(__XENO_SIM__)
73 #define DECLARE_CB_LOCK_FLAGS(name) spl_t name
74 #define cb_try_read_lock(lock, flags) \
75  ({ xnlock_get_irqsave(&nklock, flags); 0; })
76 #define cb_read_unlock(lock, flags) xnlock_put_irqrestore(&nklock, flags)
77 #define cb_try_write_lock(lock, flags) \
78  ({ xnlock_get_irqsave(&nklock, flags); 0; })
79 #define cb_force_write_lock(lock, flags) \
80  ({ xnlock_get_irqsave(&nklock, flags); 0; })
81 #define cb_write_unlock(lock, flags) xnlock_put_irqrestore(&nklock, flags)
82 #else /* !__KERNEL__ */
83 #define DECLARE_CB_LOCK_FLAGS(name)
84 #define cb_try_read_lock(lock, flags) (0)
85 #define cb_read_unlock(lock, flags) do { } while (0)
86 #define cb_try_write_lock(lock, flags) (0)
87 #define cb_force_write_lock(lock, flags) do { } while (0)
88 #define cb_write_unlock(lock, flags) do { } while (0)
89 #endif /* !__KERNEL__ */
90 #endif /* !CONFIG_XENO_FASTSYNCH */
91 
92 #endif /* CB_LOCK_H */