lock_core.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_LOCK_CORE_H
8 #define _PICO_LOCK_CORE_H
9 
10 #include "pico.h"
11 #include "pico/time.h"
12 #include "hardware/sync.h"
13 
41 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_LOCK_CORE, Enable/disable assertions in the lock core, type=bool, default=0, group=pico_sync
42 #ifndef PARAM_ASSERTIONS_ENABLED_LOCK_CORE
43 #define PARAM_ASSERTIONS_ENABLED_LOCK_CORE 0
44 #endif
45 
53 struct lock_core {
54  // spin lock protecting this lock's state
55  spin_lock_t *spin_lock;
56 
57  // note any lock members in containing structures need not be volatile;
58  // they are protected by memory/compiler barriers when gaining and release spin locks
59 };
60 
61 typedef struct lock_core lock_core_t;
62 
72 void lock_init(lock_core_t *core, uint lock_num);
73 
74 #ifndef lock_owner_id_t
75 
80 #define lock_owner_id_t int8_t
81 #endif
82 
83 #ifndef LOCK_INVALID_OWNER_ID
84 
87 #define LOCK_INVALID_OWNER_ID ((lock_owner_id_t)-1)
88 #endif
89 
90 #ifndef lock_get_caller_owner_id
91 
95 #define lock_get_caller_owner_id() ((lock_owner_id_t)get_core_num())
96 #ifndef lock_is_owner_id_valid
97 #define lock_is_owner_id_valid(id) ((id)>=0)
98 #endif
99 #endif
100 
101 #ifndef lock_is_owner_id_valid
102 #define lock_is_owner_id_valid(id) ((id) != LOCK_INVALID_OWNER_ID)
103 #endif
104 
105 #ifndef lock_internal_spin_unlock_with_wait
106 
126 #define lock_internal_spin_unlock_with_wait(lock, save) spin_unlock((lock)->spin_lock, save), __wfe()
127 #endif
128 
129 #ifndef lock_internal_spin_unlock_with_notify
130 
149 #define lock_internal_spin_unlock_with_notify(lock, save) spin_unlock((lock)->spin_lock, save), __sev()
150 #endif
151 
152 #ifndef lock_internal_spin_unlock_with_best_effort_wait_or_timeout
153 
175 #define lock_internal_spin_unlock_with_best_effort_wait_or_timeout(lock, save, until) ({ \
176  spin_unlock((lock)->spin_lock, save); \
177  best_effort_wfe_or_timeout(until); \
178 })
179 #endif
180 
181 #ifndef sync_internal_yield_until_before
182 
194 #define sync_internal_yield_until_before(until) ((void)0)
195 #endif
196 
197 #endif
lock_core
Definition: lock_core.h:53
time.h
sync.h
pico.h
lock_init
void lock_init(lock_core_t *core, uint lock_num)
Initialise a lock structure.
Definition: lock_core.c:9
spin_lock_t
volatile uint32_t spin_lock_t
A spin lock identifier.
Definition: sync.h:56