3 #include "lock_guard.h"
4 #include "switch_lock.h"
9 #define NO_INSTRUMENT __attribute__((no_instrument_function))
11 /** A wrapper for Switch_lock that works even when the threading system
12 has not been intialized yet.
13 This wrapper is necessary because most lock-protected objects are
14 initialized before the threading system has been fired up.
16 class Helping_lock : private Switch_lock
20 using Switch_lock::Status;
21 using Switch_lock::Not_locked;
22 using Switch_lock::Locked;
23 using Switch_lock::Invalid;
25 using Switch_lock::invalidate;
26 using Switch_lock::valid;
27 using Switch_lock::wait_free;
29 static bool threading_system_active;
39 #include "std_macros.h"
42 /** Threading system activated. */
43 bool Helping_lock::threading_system_active = false;
47 Helping_lock::Helping_lock ()
49 Switch_lock::initialize();
52 /** Acquire the lock with priority inheritance.
53 @return true if we owned the lock already. false otherwise.
56 Helping_lock::Status NO_INSTRUMENT
57 Helping_lock::test_and_set ()
59 if (! threading_system_active) // still initializing?
62 return Switch_lock::test_and_set();
65 /** Acquire the lock with priority inheritance.
66 If the lock is occupied, enqueue in list of helpers and lend CPU
67 to current lock owner until we are the lock owner.
69 PUBLIC inline NEEDS ["panic.h"]
70 Helping_lock::Status NO_INSTRUMENT
73 return test_and_set();
77 @return true if lock is set.
79 PUBLIC inline NEEDS["std_macros.h"]
80 Helping_lock::Status NO_INSTRUMENT
83 if (EXPECT_FALSE( ! threading_system_active) ) // still initializing?
86 return Switch_lock::test();
90 Return the CPU to helper or next lock owner, whoever has the higher
91 priority, given that thread's priority is higher that our's.
93 PUBLIC inline NEEDS["std_macros.h"]
97 if (EXPECT_FALSE( ! threading_system_active) ) // still initializing?
100 Switch_lock::clear();
103 PUBLIC inline NEEDS[Helping_lock::clear]
105 Helping_lock::set(Status s)
112 @return current owner of the lock. 0 if there is no owner.
114 PUBLIC inline NEEDS["std_macros.h", "globals.h"]
115 Context* NO_INSTRUMENT
116 Helping_lock::lock_owner () const
118 if (EXPECT_FALSE( ! threading_system_active) ) // still initializing?
121 return Switch_lock::lock_owner();