1 /*****************************************************************************/
2 /**
3  * \file
4  * \brief   Simple lock implementation.
5  *          Does only work if all thread have the same priority!
6  *
7  * \date    02/1997
8  * \author  Michael Hohmuth <hohmuth@os.inf.tu-dresden.de> */
9 /*
10  * (c) 2000-2009 Author(s)
11  *     economic rights: Technische Universität Dresden (Germany)
12  * This file is part of TUD:OS and distributed under the terms of the
13  * GNU Lesser General Public License 2.1.
14  * Please see the COPYING-LGPL-2.1 file for details.
15  */
16 
17 /*****************************************************************************/
18 #ifndef __L4UTIL_LOCK_H__
19 #define __L4UTIL_LOCK_H__
20 
21 #include <l4/sys/thread.h>
22 #include <l4/sys/compiler.h>
23 #include <l4/util/atomic.h>
24 
25 EXTERN_C_BEGIN
26 
27 typedef l4_uint32_t l4util_simple_lock_t;
28 
29 L4_INLINE int  l4_simple_try_lock(l4util_simple_lock_t *lock);
30 L4_INLINE void l4_simple_unlock(l4util_simple_lock_t *lock);
31 L4_INLINE int  l4_simple_lock_locked(l4util_simple_lock_t *lock);
32 L4_INLINE void l4_simple_lock_solid(register l4util_simple_lock_t *p);
33 L4_INLINE void l4_simple_lock(l4util_simple_lock_t * lock);
34 
35 L4_INLINE int
l4_simple_try_lock(l4util_simple_lock_t * lock)36 l4_simple_try_lock(l4util_simple_lock_t *lock)
37 {
38   return l4util_xchg32(lock, 1) == 0;
39 }
40 
41 L4_INLINE void
l4_simple_unlock(l4util_simple_lock_t * lock)42 l4_simple_unlock(l4util_simple_lock_t *lock)
43 {
44   *lock = 0;
45 }
46 
47 L4_INLINE int
l4_simple_lock_locked(l4util_simple_lock_t * lock)48 l4_simple_lock_locked(l4util_simple_lock_t *lock)
49 {
50   return (*lock == 0) ? 0 : 1;
51 }
52 
53 L4_INLINE void
l4_simple_lock_solid(register l4util_simple_lock_t * p)54 l4_simple_lock_solid(register l4util_simple_lock_t *p)
55 {
56   while (l4_simple_lock_locked(p) || !l4_simple_try_lock(p))
57     l4_thread_switch(L4_INVALID_CAP);
58 }
59 
60 L4_INLINE void
l4_simple_lock(l4util_simple_lock_t * lock)61 l4_simple_lock(l4util_simple_lock_t * lock)
62 {
63   if (!l4_simple_try_lock(lock))
64     l4_simple_lock_solid(lock);
65 }
66 
67 EXTERN_C_END
68 
69 #endif
70