Atomic Boolean

Atomic operations class. Currently only supports AtomicBool

Only test-and-set/test-and-clear operations fully tested.

Committer:
Tomo2k
Date:
Fri May 02 13:40:57 2014 +0000
Revision:
0:25fedaa4b401
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tomo2k 0:25fedaa4b401 1 /* (C) 2014 Richard Thompson (Tomo2k)
Tomo2k 0:25fedaa4b401 2 This software is Apache 2.0 licenced.
Tomo2k 0:25fedaa4b401 3 */
Tomo2k 0:25fedaa4b401 4 #pragma once
Tomo2k 0:25fedaa4b401 5
Tomo2k 0:25fedaa4b401 6 //! Atomic Boolean.
Tomo2k 0:25fedaa4b401 7 //! Also provides test-and-set and test-and-clear methods.
Tomo2k 0:25fedaa4b401 8 //! Interchangable with bool.\n
Tomo2k 0:25fedaa4b401 9 //! Works by disabling then re-enabling interrupts.\n
Tomo2k 0:25fedaa4b401 10 //! Do not use during ISR or when interrupts should remain disabled.
Tomo2k 0:25fedaa4b401 11 class AtomicBool
Tomo2k 0:25fedaa4b401 12 {
Tomo2k 0:25fedaa4b401 13 public:
Tomo2k 0:25fedaa4b401 14 //! Create the boolean and set initial value from bool
Tomo2k 0:25fedaa4b401 15 //! @warning Construction is not guaranteed atomic! Interrupts may occur before construction completes.
Tomo2k 0:25fedaa4b401 16 AtomicBool(const bool initialValue) :
Tomo2k 0:25fedaa4b401 17 m_value(initialValue) {}
Tomo2k 0:25fedaa4b401 18
Tomo2k 0:25fedaa4b401 19 //! Create and set initial value from another AtomicBool.
Tomo2k 0:25fedaa4b401 20 //! This method guaranteed atomic.
Tomo2k 0:25fedaa4b401 21 AtomicBool(const AtomicBool &rhs) {
Tomo2k 0:25fedaa4b401 22 __disable_irq();
Tomo2k 0:25fedaa4b401 23 m_value = rhs.m_value;
Tomo2k 0:25fedaa4b401 24 __enable_irq();
Tomo2k 0:25fedaa4b401 25 }
Tomo2k 0:25fedaa4b401 26
Tomo2k 0:25fedaa4b401 27 // Assign to bool
Tomo2k 0:25fedaa4b401 28 void operator=(const bool &rhs) {
Tomo2k 0:25fedaa4b401 29 __disable_irq();
Tomo2k 0:25fedaa4b401 30 m_value = rhs;
Tomo2k 0:25fedaa4b401 31 __enable_irq();
Tomo2k 0:25fedaa4b401 32 }
Tomo2k 0:25fedaa4b401 33
Tomo2k 0:25fedaa4b401 34 // Assign to own type
Tomo2k 0:25fedaa4b401 35 void operator=(const AtomicBool &rhs) {
Tomo2k 0:25fedaa4b401 36 __disable_irq();
Tomo2k 0:25fedaa4b401 37 m_value = rhs.m_value;
Tomo2k 0:25fedaa4b401 38 __enable_irq();
Tomo2k 0:25fedaa4b401 39 }
Tomo2k 0:25fedaa4b401 40
Tomo2k 0:25fedaa4b401 41 inline bool operator==(const bool &rhs) const {
Tomo2k 0:25fedaa4b401 42 __disable_irq();
Tomo2k 0:25fedaa4b401 43 bool result(m_value == rhs);
Tomo2k 0:25fedaa4b401 44 __enable_irq();
Tomo2k 0:25fedaa4b401 45 return result;
Tomo2k 0:25fedaa4b401 46 }
Tomo2k 0:25fedaa4b401 47
Tomo2k 0:25fedaa4b401 48 inline bool operator!=(const bool &rhs) const {
Tomo2k 0:25fedaa4b401 49 __disable_irq();
Tomo2k 0:25fedaa4b401 50 bool result(m_value != rhs);
Tomo2k 0:25fedaa4b401 51 __enable_irq();
Tomo2k 0:25fedaa4b401 52 return result;
Tomo2k 0:25fedaa4b401 53 }
Tomo2k 0:25fedaa4b401 54
Tomo2k 0:25fedaa4b401 55 //! Test and set the bool
Tomo2k 0:25fedaa4b401 56 //! @returns true if bool was false before calling
Tomo2k 0:25fedaa4b401 57 inline bool testAndSet() {
Tomo2k 0:25fedaa4b401 58 __disable_irq();
Tomo2k 0:25fedaa4b401 59 bool result(!m_value);
Tomo2k 0:25fedaa4b401 60 m_value = true;
Tomo2k 0:25fedaa4b401 61 __enable_irq();
Tomo2k 0:25fedaa4b401 62 return result;
Tomo2k 0:25fedaa4b401 63 }
Tomo2k 0:25fedaa4b401 64
Tomo2k 0:25fedaa4b401 65 //! Test and clear the bool
Tomo2k 0:25fedaa4b401 66 //! @returns true if bool was true before calling
Tomo2k 0:25fedaa4b401 67 inline bool testAndClear() {
Tomo2k 0:25fedaa4b401 68 __disable_irq();
Tomo2k 0:25fedaa4b401 69 bool result(m_value);
Tomo2k 0:25fedaa4b401 70 m_value = false;
Tomo2k 0:25fedaa4b401 71 __enable_irq();
Tomo2k 0:25fedaa4b401 72 return result;
Tomo2k 0:25fedaa4b401 73 }
Tomo2k 0:25fedaa4b401 74
Tomo2k 0:25fedaa4b401 75 inline operator bool() const {
Tomo2k 0:25fedaa4b401 76 __disable_irq();
Tomo2k 0:25fedaa4b401 77 bool result(m_value);
Tomo2k 0:25fedaa4b401 78 __enable_irq();
Tomo2k 0:25fedaa4b401 79 return result;
Tomo2k 0:25fedaa4b401 80 }
Tomo2k 0:25fedaa4b401 81
Tomo2k 0:25fedaa4b401 82 bool operator==(const AtomicBool &rhs) const {
Tomo2k 0:25fedaa4b401 83 __disable_irq();
Tomo2k 0:25fedaa4b401 84 bool result(m_value == rhs.m_value);
Tomo2k 0:25fedaa4b401 85 __enable_irq();
Tomo2k 0:25fedaa4b401 86 return result;
Tomo2k 0:25fedaa4b401 87 }
Tomo2k 0:25fedaa4b401 88
Tomo2k 0:25fedaa4b401 89 bool operator!=(const AtomicBool &rhs) const {
Tomo2k 0:25fedaa4b401 90 __disable_irq();
Tomo2k 0:25fedaa4b401 91 bool result(m_value != rhs.m_value);
Tomo2k 0:25fedaa4b401 92 __enable_irq();
Tomo2k 0:25fedaa4b401 93 return result;
Tomo2k 0:25fedaa4b401 94 }
Tomo2k 0:25fedaa4b401 95
Tomo2k 0:25fedaa4b401 96 private:
Tomo2k 0:25fedaa4b401 97 bool m_value;
Tomo2k 0:25fedaa4b401 98 };