Skip to content

Commit 8f8b98e

Browse files
committed
pbio/os: Add context to process.
When using the same thread for multiple processes it can be useful to have a reference to the state that belongs to that instance. This avoids having to use several cases of CONTAINER_OF to get to the parent of a state, which is not always available. This also brings it one step closer to pbio tasks, which we might model in this way too.
1 parent bc4a682 commit 8f8b98e

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

lib/pbio/drv/charger/charger_mp2639a.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static bool read_chg(void) {
178178

179179
static pbio_os_process_t pbdrv_charger_mp2639a_process;
180180

181-
pbio_error_t pbdrv_charger_mp2639a_process_thread(pbio_os_state_t *state) {
181+
pbio_error_t pbdrv_charger_mp2639a_process_thread(pbio_os_state_t *state, void *context) {
182182

183183
static pbio_os_timer_t timer;
184184

@@ -282,7 +282,7 @@ pbio_error_t pbdrv_charger_mp2639a_process_thread(pbio_os_state_t *state) {
282282

283283
void pbdrv_charger_init(void) {
284284
pbdrv_init_busy_up();
285-
pbio_os_start_process(&pbdrv_charger_mp2639a_process, pbdrv_charger_mp2639a_process_thread);
285+
pbio_os_start_process(&pbdrv_charger_mp2639a_process, pbdrv_charger_mp2639a_process_thread, NULL);
286286
}
287287

288288
#endif // PBDRV_CONFIG_CHARGER_MP2639A

lib/pbio/include/pbio/os.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool pbio_os_timer_is_expired(pbio_os_timer_t *timer);
8787
*/
8888
typedef uint32_t pbio_os_state_t;
8989

90-
typedef pbio_error_t (*pbio_os_process_func_t)(pbio_os_state_t *state);
90+
typedef pbio_error_t (*pbio_os_process_func_t)(pbio_os_state_t *state, void *context);
9191

9292
typedef struct _pbio_os_process_t pbio_os_process_t;
9393

@@ -107,6 +107,10 @@ struct _pbio_os_process_t {
107107
* The protothread.
108108
*/
109109
pbio_os_process_func_t func;
110+
/**
111+
* Context passed on each call to the protothread.
112+
*/
113+
void *context;
110114
/**
111115
* Most recent result of running one iteration of the protothread.
112116
*/
@@ -187,7 +191,7 @@ void pbio_os_run_while_idle(void);
187191

188192
void pbio_os_request_poll(void);
189193

190-
void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func);
194+
void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func, void *context);
191195

192196
/**
193197
* Disables interrupts and returns the previous interrupt state.

lib/pbio/src/os.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ static pbio_os_process_t *process_list = NULL;
5353
*
5454
* @param process The process to start.
5555
* @param func The process thread function.
56+
* @param context The context to pass to the process.
5657
*/
57-
void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func) {
58+
void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func, void *context) {
5859

5960
// Add the new process to the end of the list.
6061
pbio_os_process_t *last = process_list;
@@ -69,6 +70,7 @@ void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t fu
6970

7071
// Initialize the process.
7172
process->func = func;
73+
process->context = context;
7274
process->next = NULL;
7375
process->err = PBIO_ERROR_AGAIN;
7476
process->state = 0;
@@ -103,7 +105,7 @@ bool pbio_os_run_processes_once(void) {
103105
while (process) {
104106
// Run one iteration of the process if not yet completed or errored.
105107
if (process->err == PBIO_ERROR_AGAIN) {
106-
process->err = process->func(&process->state);
108+
process->err = process->func(&process->state, process->context);
107109
}
108110
process = process->next;
109111
}

0 commit comments

Comments
 (0)