Overview   API Reference  

semaphore.hpp

00001 #if !defined(__SYSTEM_L4_SEMAPHORE_HPP__)
00002 #define __SYSTEM_L4_SEMAPHORE_HPP__
00003 
00004 //
00005 // L4 includes
00006 //
00007 #include <l4/semaphore/semaphore.h>
00008 
00009 //
00010 // local includes
00011 //
00012 #include "core/util/noncopyable.hpp"
00013 
00014 //
00015 // a l4semaphore wrapped into a C++ class.
00016 //
00017 struct semaphore : protected l4semaphore, private noncopyable
00018 {
00019     //
00020     // constructors
00021     //
00022     inline semaphore(void)
00023         : l4semaphore(L4SEMAPHORE_UNLOCKED)
00024     {}
00025 
00026     // do *NOT* copy another used l4semaphore (for L4SEMAPHORE_LOCKED/L4SEMAPHORE_UNLOCKED only).
00027     inline semaphore(const l4semaphore &initial_state)
00028         : l4semaphore(initial_state)
00029     {}
00030 
00031     //
00032     // typecast operators
00033     //
00034     inline int operator ()(void) const
00035     {
00036         return counter;
00037     }
00038 
00039     inline operator int(void) const
00040     {
00041         return (*this)();
00042     }
00043 
00044     //
00045     // map l4semaphore functions to methods ...
00046     //
00047     inline int down(void)
00048     {
00049         l4semaphore_down(this);
00050         return 0;
00051     }
00052 
00053     //
00054     // timeout in us. special values:
00055     //      0 -> timeout 0
00056     //     ~0 -> timeout NEVER
00057     //
00058     inline int down(unsigned timeout)
00059     {
00060         // l4semaphore_down_timed wants timeout in ms
00061         return (timeout == ~0u) ? down() : l4semaphore_down_timed(this, timeout / 1000);
00062     }
00063 
00064     inline int try_down(void)
00065     {
00066         return l4semaphore_try_down(this);
00067     }
00068 
00069     inline void up(void)
00070     {
00071         l4semaphore_up(this);
00072     }
00073 
00074     //
00075     // prefix increment operators
00076     //
00077     inline semaphore &operator ++(void)
00078     {
00079         up();
00080         return *this;
00081     }
00082 
00083     inline semaphore &operator --(void)
00084     {
00085         down();
00086         return *this;
00087     }
00088 
00089     //
00090     // postfix increment operators: these ones return void to avoid to copy the entire object,
00091     //     thus violating standard compliance. DO NOT return a reference here.
00092     //
00093     inline void operator ++(int)
00094     {
00095         ++*this;
00096     }
00097 
00098     inline void operator --(int)
00099     {
00100         --*this;
00101     }
00102 };
00103 
00104 #endif
00105 
00106 // ***** end of source ***** //
00107 

L4vmm Reference Manual, written by Mario Schwalbe  © 2006-2008