atMETEO
An ATmega based weather station
atomic.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
53 #include <inttypes.h> // AVR toolchain doesn't offer cinttypes header
54 
55 #include <avr/io.h>
56 
57 namespace Avr
58 {
59 
73 template<class TAtomicMode>
74 class AtomicGuard : private TAtomicMode
75 {
76 };
77 
87 {
88 protected:
89  AtomicForceOn() { cli(); }
90  ~AtomicForceOn() { sei(); }
91 };
92 
98 {
99 protected:
100  AtomicRestoreState() : m_SREG(SREG) { cli(); }
101  ~AtomicRestoreState() { SREG = m_SREG; }
102 
103 private:
104  const uint8_t m_SREG;
105 };
106 
118 template<class TNonAtomicMode>
119 class NonAtomicGuard : private TNonAtomicMode
120 {
121 };
122 
132 {
133 protected:
134  NonAtomicForceOff() { sei(); }
135  ~NonAtomicForceOff() { cli(); }
136 };
137 
143 {
144 protected:
145  NonAtomicRestoreState() : m_SREG(SREG) { sei(); }
146  ~NonAtomicRestoreState() { SREG = m_SREG; }
147 
148 private:
149  const uint8_t m_SREG;
150 };
151  // \addtogroup libtarget_atomic
153 
154 } // namespace Avr
Configuration parameter for NonAtomicGuard that unconditionally disables interrupts on exit...
Definition: atomic.h:131
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
Configuration parameter for AtomicGuard that saves the global interrupt state and restores it on exit...
Definition: atomic.h:97
Configuration parameter for AtomicGuard that unconditionally enables interrupts on exit...
Definition: atomic.h:86
RAII-style wrapper to disable interrupts for the duration of a scoped block.
Definition: atomic.h:74
Configuration parameter for NonAtomicGuard that saves the global interrupt state and restores it on e...
Definition: atomic.h:142
RAII-style wrapper to enable interrupts for the duration of a scoped block.
Definition: atomic.h:119