v2.0.0
async_context_threadsafe_background.h
1/*
2 * Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _PICO_ASYNC_CONTEXT_THREADSAFE_BACKGROUND_H
8#define _PICO_ASYNC_CONTEXT_THREADSAFE_BACKGROUND_H
9
20#include "pico/async_context.h"
21#include "pico/sem.h"
22#include "pico/mutex.h"
23#include "hardware/irq.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#ifndef ASYNC_CONTEXT_THREADSAFE_BACKGROUND_MULTI_CORE
30#define ASYNC_CONTEXT_THREADSAFE_BACKGROUND_MULTI_CORE LIB_PICO_MULTICORE
31#endif
32
33typedef struct async_context_threadsafe_background async_context_threadsafe_background_t;
34
38 typedef struct async_context_threadsafe_background_config {
42 uint8_t low_priority_irq_handler_priority;
53 alarm_pool_t *custom_alarm_pool;
54} async_context_threadsafe_background_config_t;
55
56 struct async_context_threadsafe_background {
57 async_context_t core;
58 alarm_pool_t *alarm_pool; // this must be on the same core as core_num
59 absolute_time_t last_set_alarm_time;
60 recursive_mutex_t lock_mutex;
61 semaphore_t work_needed_sem;
62 volatile alarm_id_t alarm_id;
63#if ASYNC_CONTEXT_THREADSAFE_BACKGROUND_MULTI_CORE
64 volatile alarm_id_t force_alarm_id;
65 bool alarm_pool_owned;
66#endif
67 uint8_t low_priority_irq_num;
68 volatile bool alarm_pending;
69};
70
82bool async_context_threadsafe_background_init(async_context_threadsafe_background_t *self, async_context_threadsafe_background_config_t *config);
83
91 async_context_threadsafe_background_config_t async_context_threadsafe_background_default_config(void);
92
103 static inline bool async_context_threadsafe_background_init_with_defaults(async_context_threadsafe_background_t *self) {
104 async_context_threadsafe_background_config_t config = async_context_threadsafe_background_default_config();
105 return async_context_threadsafe_background_init(self, &config);
106}
107
108#ifdef __cplusplus
109}
110#endif
111
112#endif
async_context.h
alarm_id_t
int32_t alarm_id_t
The identifier for an alarm.
Definition: time.h:365
async_context_threadsafe_background_init_with_defaults
static bool async_context_threadsafe_background_init_with_defaults(async_context_threadsafe_background_t *self)
Initialize an async_context_threadsafe_background instance with default values.
Definition: async_context_threadsafe_background.h:103
async_context_threadsafe_background_init
bool async_context_threadsafe_background_init(async_context_threadsafe_background_t *self, async_context_threadsafe_background_config_t *config)
Initialize an async_context_threadsafe_background instance using the specified configuration.
Definition: async_context_threadsafe_background.c:195
async_context_threadsafe_background_default_config
async_context_threadsafe_background_config_t async_context_threadsafe_background_default_config(void)
Return a copy of the default configuration object used by async_context_threadsafe_background_init_wi...
Definition: async_context_threadsafe_background.c:28
absolute_time_t
uint64_t absolute_time_t
An opaque 64 bit timestamp in microseconds.
Definition: types.h:43
irq.h
mutex.h
sem.h
alarm_pool
Definition: time.c:28
async_context_threadsafe_background_config
Configuration object for async_context_threadsafe_background instances.
Definition: async_context_threadsafe_background.h:38
async_context_threadsafe_background_config::custom_alarm_pool
alarm_pool_t * custom_alarm_pool
a specific alarm pool to use (or NULL to use ta default)
Definition: async_context_threadsafe_background.h:53
async_context_threadsafe_background_config::low_priority_irq_handler_priority
uint8_t low_priority_irq_handler_priority
the priority of the low priority IRQ
Definition: async_context_threadsafe_background.h:42
async_context_threadsafe_background
Definition: async_context_threadsafe_background.h:56
async_context
Base structure type of all async_contexts. For details about its use, see pico_async_context.
Definition: async_context.h:179
recursive_mutex_t
recursive mutex instance
Definition: mutex.h:47
semaphore
Definition: sem.h:31