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
bool sem_acquire_timeout_us(semaphore_t *sem, uint32_t timeout_us)
Acquire a permit from a semaphore, with timeout.
Definition: sem.c:42
bool sem_try_acquire(semaphore_t *sem)
Attempt to acquire a permit from a semaphore without blocking.
Definition: sem.c:60
bool sem_acquire_timeout_ms(semaphore_t *sem, uint32_t timeout_ms)
Acquire a permit from a semaphore, with timeout.
Definition: sem.c:38
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:46
void sem_init(semaphore_t *sem, int16_t initial_permits, int16_t max_permits)
Initialise a semaphore structure.
Definition: sem.c:10
void sem_reset(semaphore_t *sem, int16_t permits)
Reset semaphore to a specific number of available permits.
Definition: sem.c:85
int sem_available(semaphore_t *sem)
Return number of available permits on the semaphore.
Definition: sem.c:17
bool sem_release(semaphore_t *sem)
Release a permit on a semaphore.
Definition: sem.c:72
void sem_acquire_blocking(semaphore_t *sem)
Acquire a permit from the semaphore.
Definition: sem.c:26
recursive mutex instance
Definition: mutex.h:47
Definition: types.h:33
Definition: lock_core.h:53
Definition: sem.h:31