COSA
An Object-Oriented Platform for Arduino Programming
Resource.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_RESOURCE_HH
22 #define COSA_RESOURCE_HH
23 
24 #include "Cosa/Types.h"
25 
29 class Resource {
30 public:
35  m_busy(false)
36  {}
37 
42  void acquire()
43  {
44  uint8_t key = lock(m_busy);
45  on_acquire();
46  unlock(key);
47  }
48 
54  bool try_acquire()
55  {
56  synchronized {
57  if (m_busy) return (false);
58  m_busy = true;
59  on_acquire();
60  }
61  }
62 
67  void release()
68  {
69  synchronized {
70  on_release();
71  m_busy = false;
72  }
73  }
74 
75 protected:
80  virtual void on_acquire() = 0;
81 
86  virtual void on_release() = 0;
87 
88 private:
89  volatile bool m_busy;
90 };
91 #endif
92 
Resource()
Definition: Resource.hh:34
void release()
Definition: Resource.hh:67
void acquire()
Definition: Resource.hh:42
virtual void on_release()=0
uint8_t lock()
Definition: Types.h:319
bool try_acquire()
Definition: Resource.hh:54
void unlock(uint8_t key)
Definition: Types.h:331
virtual void on_acquire()=0