sem.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_SEM_H
8 #define _PICO_SEM_H
9 
10 #include "pico/lock_core.h"
11 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 typedef struct __packed_aligned semaphore {
32  struct lock_core core;
33  int16_t permits;
34  int16_t max_permits;
35 } semaphore_t;
36 
37 
45 void sem_init(semaphore_t *sem, int16_t initial_permits, int16_t max_permits);
46 
53 int sem_available(semaphore_t *sem);
54 
64 bool sem_release(semaphore_t *sem);
65 
74 void sem_reset(semaphore_t *sem, int16_t permits);
75 
83 void sem_acquire_blocking(semaphore_t *sem);
84 
96 bool sem_acquire_timeout_ms(semaphore_t *sem, uint32_t timeout_ms);
97 
109 bool sem_acquire_timeout_us(semaphore_t *sem, uint32_t timeout_us);
110 
123 bool sem_acquire_block_until(semaphore_t *sem, absolute_time_t until);
124 
134 bool sem_try_acquire(semaphore_t *sem);
135 
136 #ifdef __cplusplus
137 }
138 #endif
139 #endif
lock_core
Definition: lock_core.h:53
sem_acquire_timeout_ms
bool sem_acquire_timeout_ms(semaphore_t *sem, uint32_t timeout_ms)
Acquire a permit from a semaphore, with timeout.
Definition: sem.c:33
sem_init
void sem_init(semaphore_t *sem, int16_t initial_permits, int16_t max_permits)
Initialise a semaphore structure.
Definition: sem.c:10
absolute_time_t
Definition: types.h:33
semaphore
Definition: sem.h:31
__packed_aligned
recursive mutex instance
Definition: mutex.h:47
sem_acquire_timeout_us
bool sem_acquire_timeout_us(semaphore_t *sem, uint32_t timeout_us)
Acquire a permit from a semaphore, with timeout.
Definition: sem.c:37
sem_release
bool sem_release(semaphore_t *sem)
Release a permit on a semaphore.
Definition: sem.c:67
sem_try_acquire
bool sem_try_acquire(semaphore_t *sem)
Attempt to acquire a permit from a semaphore without blocking.
Definition: sem.c:55
sem_acquire_blocking
void sem_acquire_blocking(semaphore_t *sem)
Acquire a permit from the semaphore.
Definition: sem.c:21
sem_reset
void sem_reset(semaphore_t *sem, int16_t permits)
Reset semaphore to a specific number of available permits.
Definition: sem.c:80
sem_acquire_block_until
bool sem_acquire_block_until(semaphore_t *sem, absolute_time_t until)
Wait to acquire a permit from a semaphore until a specific time.
Definition: sem.c:41
lock_core.h
sem_available
int sem_available(semaphore_t *sem)
Return number of available permits on the semaphore.
Definition: sem.c:17