summaryrefslogtreecommitdiff
path: root/cortex_m/critical_section.h
blob: 6b611a832e652986afeea8391cc7f38b87d23def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once

#include <cstdint>

struct critical_section {
    uint32_t primask;

    critical_section() {
        asm volatile("mrs %0, primask" : "=r" (primask));

        asm volatile("cpsid i");

        asm volatile("dmb");
    }

    ~critical_section() {
        asm volatile("dmb");

        asm volatile("msr primask, %0" :: "r" (primask));
    }
};