critical_section.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_CRITICAL_SECTION_H
8 #define _PICO_CRITICAL_SECTION_H
9 
10 #include "pico/lock_core.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
30  spin_lock_t *spin_lock;
31  uint32_t save;
32 } critical_section_t;
33 
44 void critical_section_init(critical_section_t *crit_sec);
45 
51 void critical_section_init_with_lock_num(critical_section_t *crit_sec, uint lock_num);
52 
61 static inline void critical_section_enter_blocking(critical_section_t *crit_sec) {
62  crit_sec->save = spin_lock_blocking(crit_sec->spin_lock);
63 }
64 
70 static inline void critical_section_exit(critical_section_t *crit_sec) {
71  spin_unlock(crit_sec->spin_lock, crit_sec->save);
72 }
73 
83 void critical_section_deinit(critical_section_t *crit_sec);
84 
91 static inline bool critical_section_is_initialized(critical_section_t *crit_sec) {
92  return crit_sec->spin_lock != 0;
93 }
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 #endif
critical_section_is_initialized
static bool critical_section_is_initialized(critical_section_t *crit_sec)
Test whether a critical_section has been initialized.
Definition: critical_section.h:91
critical_section_deinit
void critical_section_deinit(critical_section_t *crit_sec)
De-Initialise a critical_section created by the critical_section_init method.
Definition: critical_section.c:22
__packed_aligned
recursive mutex instance
Definition: mutex.h:47
spin_lock_blocking
static __force_inline uint32_t spin_lock_blocking(spin_lock_t *lock)
Acquire a spin lock safely.
Definition: sync.h:279
critical_section_init
void critical_section_init(critical_section_t *crit_sec)
Initialise a critical_section structure allowing the system to assign a spin lock number.
Definition: critical_section.c:13
spin_unlock
static __force_inline void spin_unlock(spin_lock_t *lock, uint32_t saved_irq)
Release a spin lock safely.
Definition: sync.h:307
critical_section_enter_blocking
static void critical_section_enter_blocking(critical_section_t *crit_sec)
Enter a critical_section.
Definition: critical_section.h:61
critical_section_exit
static void critical_section_exit(critical_section_t *crit_sec)
Release a critical_section.
Definition: critical_section.h:70
critical_section
Definition: critical_section.h:29
lock_core.h
spin_lock_t
volatile uint32_t spin_lock_t
A spin lock identifier.
Definition: sync.h:56
critical_section_init_with_lock_num
void critical_section_init_with_lock_num(critical_section_t *crit_sec, uint lock_num)
Initialise a critical_section structure assigning a specific spin lock number.
Definition: critical_section.c:17