async_context.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
63 #ifndef _PICO_ASYNC_CONTEXT_H
64 #define _PICO_ASYNC_CONTEXT_H
65 
66 #include "pico.h"
67 #include "pico/time.h"
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 enum {
74  ASYNC_CONTEXT_POLL = 1,
75  ASYNC_CONTEXT_THREADSAFE_BACKGROUND = 2,
76  ASYNC_CONTEXT_FREERTOS = 3,
77 };
78 
79 typedef struct async_context async_context_t;
80 
90 typedef struct async_work_on_timeout {
103  void (*do_work)(async_context_t *context, struct async_work_on_timeout *timeout);
112  void *user_data;
114 
136  void (*do_work)(async_context_t *context, struct async_when_pending_worker *worker);
142 
143 #define ASYNC_CONTEXT_FLAG_CALLBACK_FROM_NON_IRQ 0x1
144 #define ASYNC_CONTEXT_FLAG_CALLBACK_FROM_IRQ 0x2
145 #define ASYNC_CONTEXT_FLAG_POLLED 0x4
146 
151 typedef struct async_context_type {
152  uint16_t type;
153  // see wrapper functions for documentation
154  void (*acquire_lock_blocking)(async_context_t *self);
155  void (*release_lock)(async_context_t *self);
156  void (*lock_check)(async_context_t *self);
157  uint32_t (*execute_sync)(async_context_t *context, uint32_t (*func)(void *param), void *param);
158  bool (*add_at_time_worker)(async_context_t *self, async_at_time_worker_t *worker);
159  bool (*remove_at_time_worker)(async_context_t *self, async_at_time_worker_t *worker);
160  bool (*add_when_pending_worker)(async_context_t *self, async_when_pending_worker_t *worker);
161  bool (*remove_when_pending_worker)(async_context_t *self, async_when_pending_worker_t *worker);
162  void (*set_work_pending)(async_context_t *self, async_when_pending_worker_t *worker);
163  void (*poll)(async_context_t *self); // may be NULL
164  void (*wait_until)(async_context_t *self, absolute_time_t until);
165  void (*wait_for_work_until)(async_context_t *self, absolute_time_t until);
166  void (*deinit)(async_context_t *self);
168 
176  const async_context_type_t *type;
177  async_when_pending_worker_t *when_pending_list;
178  async_at_time_worker_t *at_time_list;
179  absolute_time_t next_time;
180  uint16_t flags;
181  uint8_t core_num;
182 };
183 
203  context->type->acquire_lock_blocking(context);
204 }
205 
221 static inline void async_context_release_lock(async_context_t *context) {
222  context->type->release_lock(context);
223 }
224 
232 static inline void async_context_lock_check(async_context_t *context) {
233  context->type->lock_check(context);
234 }
235 
251 static inline uint32_t async_context_execute_sync(async_context_t *context, uint32_t (*func)(void *param), void *param) {
252  return context->type->execute_sync(context, func, param);
253 }
254 
271  return context->type->add_at_time_worker(context, worker);
272 }
273 
291  worker->next_time = at;
292  return context->type->add_at_time_worker(context, worker);
293 }
294 
311 static inline bool async_context_add_at_time_worker_in_ms(async_context_t *context, async_at_time_worker_t *worker, uint32_t ms) {
312  worker->next_time = make_timeout_time_ms(ms);
313  return context->type->add_at_time_worker(context, worker);
314 }
315 
328  return context->type->remove_at_time_worker(context, worker);
329 }
330 
348  return context->type->add_when_pending_worker(context, worker);
349 }
350 
363  return context->type->remove_when_pending_worker(context, worker);
364 }
365 
378  context->type->set_work_pending(context, worker);
379 }
380 
392 static inline void async_context_poll(async_context_t *context) {
393  if (context->type->poll) context->type->poll(context);
394 }
395 
406 static inline void async_context_wait_until(async_context_t *context, absolute_time_t until) {
407  context->type->wait_until(context, until);
408 }
409 
420  context->type->wait_for_work_until(context, until);
421 }
422 
432 static inline void async_context_wait_for_work_ms(async_context_t *context, uint32_t ms) {
434 }
435 
443 static inline uint async_context_core_num(const async_context_t *context) {
444  return context->core_num;
445 }
446 
460 static inline void async_context_deinit(async_context_t *context) {
461  context->type->deinit(context);
462 }
463 
464 #ifdef __cplusplus
465 }
466 #endif
467 
468 #endif
async_work_on_timeout::user_data
void * user_data
Definition: async_context.h:112
async_work_on_timeout
A "timeout" instance used by an async_context.
Definition: async_context.h:90
async_work_on_timeout::next_time
absolute_time_t next_time
Definition: async_context.h:108
async_context_deinit
static void async_context_deinit(async_context_t *context)
End async_context processing, and free any resources.
Definition: async_context.h:460
async_context_add_at_time_worker_at
static bool async_context_add_at_time_worker_at(async_context_t *context, async_at_time_worker_t *worker, absolute_time_t at)
Add an "at time" worker to a context.
Definition: async_context.h:290
absolute_time_t
Definition: types.h:33
async_context_remove_at_time_worker
static bool async_context_remove_at_time_worker(async_context_t *context, async_at_time_worker_t *worker)
Remove an "at time" worker from a context.
Definition: async_context.h:327
async_context_wait_for_work_until
static void async_context_wait_for_work_until(async_context_t *context, absolute_time_t until)
Block until work needs to be done or the specified time has been reached.
Definition: async_context.h:419
async_context_core_num
static uint async_context_core_num(const async_context_t *context)
Return the processor core this async_context belongs to.
Definition: async_context.h:443
async_context_release_lock
static void async_context_release_lock(async_context_t *context)
Release the async_context lock.
Definition: async_context.h:221
async_when_pending_worker::next
struct async_when_pending_worker * next
Definition: async_context.h:129
async_when_pending_worker::do_work
void(* do_work)(async_context_t *context, struct async_when_pending_worker *worker)
Definition: async_context.h:136
async_context_remove_when_pending_worker
static bool async_context_remove_when_pending_worker(async_context_t *context, async_when_pending_worker_t *worker)
Remove a "when pending" worker from a context.
Definition: async_context.h:362
async_context_set_work_pending
static void async_context_set_work_pending(async_context_t *context, async_when_pending_worker_t *worker)
Mark a "when pending" worker as having work pending.
Definition: async_context.h:377
make_timeout_time_ms
static absolute_time_t make_timeout_time_ms(uint32_t ms)
Convenience method to get the timestamp a number of milliseconds from the current time.
Definition: time.h:141
async_when_pending_worker
A "worker" instance used by an async_context.
Definition: async_context.h:125
async_context_poll
static void async_context_poll(async_context_t *context)
Perform any pending work for polling style async_context.
Definition: async_context.h:392
async_context_execute_sync
static uint32_t async_context_execute_sync(async_context_t *context, uint32_t(*func)(void *param), void *param)
Execute work synchronously on the core the async_context belongs to.
Definition: async_context.h:251
async_context_add_when_pending_worker
static bool async_context_add_when_pending_worker(async_context_t *context, async_when_pending_worker_t *worker)
Add a "when pending" worker to a context.
Definition: async_context.h:347
async_context_add_at_time_worker_in_ms
static bool async_context_add_at_time_worker_in_ms(async_context_t *context, async_at_time_worker_t *worker, uint32_t ms)
Add an "at time" worker to a context.
Definition: async_context.h:311
time.h
async_work_on_timeout::do_work
void(* do_work)(async_context_t *context, struct async_work_on_timeout *timeout)
Definition: async_context.h:103
async_context_lock_check
static void async_context_lock_check(async_context_t *context)
Assert if the caller does not own the lock for the async_context.
Definition: async_context.h:232
pico.h
async_context
Base structure type of all async_contexts. For details about its use, see pico_async_context.
Definition: async_context.h:175
async_work_on_timeout::next
struct async_work_on_timeout * next
Definition: async_context.h:94
async_context_type_t
struct async_context_type async_context_type_t
Implementation of an async_context type, providing methods common to that type.
async_context_acquire_lock_blocking
static void async_context_acquire_lock_blocking(async_context_t *context)
Acquire the async_context lock.
Definition: async_context.h:202
async_when_pending_worker::work_pending
bool work_pending
Definition: async_context.h:140
async_context_wait_for_work_ms
static void async_context_wait_for_work_ms(async_context_t *context, uint32_t ms)
Block until work needs to be done or the specified number of milliseconds have passed.
Definition: async_context.h:432
async_context_add_at_time_worker
static bool async_context_add_at_time_worker(async_context_t *context, async_at_time_worker_t *worker)
Add an "at time" worker to a context.
Definition: async_context.h:270
async_context_type
Implementation of an async_context type, providing methods common to that type.
Definition: async_context.h:151
async_when_pending_worker_t
struct async_when_pending_worker async_when_pending_worker_t
A "worker" instance used by an async_context.
async_context_wait_until
static void async_context_wait_until(async_context_t *context, absolute_time_t until)
sleep until the specified time in an async_context callback safe way
Definition: async_context.h:406
async_at_time_worker_t
struct async_work_on_timeout async_at_time_worker_t
A "timeout" instance used by an async_context.