Skip to content

Commit d06724e

Browse files
committed
pbio/os: Implement thread reset and race.
1 parent 8f8b98e commit d06724e

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

lib/pbio/drv/charger/charger_mp2639a.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pbio_error_t pbdrv_charger_mp2639a_process_thread(pbio_os_state_t *state, void *
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, NULL);
285+
pbio_os_process_start(&pbdrv_charger_mp2639a_process, pbdrv_charger_mp2639a_process_thread, NULL);
286286
}
287287

288288
#endif // PBDRV_CONFIG_CHARGER_MP2639A

lib/pbio/include/pbio/os.h

+28-1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ struct _pbio_os_process_t {
166166
} \
167167
} while (0)
168168

169+
/**
170+
* Yields the protothread until the specified condition is true.
171+
*
172+
* @param [in] state Protothread state.
173+
* @param [in] condition The condition.
174+
*/
175+
#define AWAIT_UNTIL(state, condition) \
176+
do { \
177+
PB_LC_SET(state); \
178+
if (!(condition)) { \
179+
return PBIO_ERROR_AGAIN; \
180+
} \
181+
} while (0)
169182

170183
#define AWAIT(state, child, statement) \
171184
do { \
@@ -176,6 +189,16 @@ struct _pbio_os_process_t {
176189
} \
177190
} while (0)
178191

192+
#define AWAIT_RACE(state, child1, child2, statement1, statement2) \
193+
do { \
194+
ASYNC_INIT((child1)); \
195+
ASYNC_INIT((child2)); \
196+
PB_LC_SET(state); \
197+
if ((statement1) == PBIO_ERROR_AGAIN && (statement2) == PBIO_ERROR_AGAIN) { \
198+
return PBIO_ERROR_AGAIN; \
199+
} \
200+
} while (0)
201+
179202
#define AWAIT_MS(state, timer, duration) \
180203
do { \
181204
pbio_os_timer_set(timer, duration); \
@@ -191,7 +214,11 @@ void pbio_os_run_while_idle(void);
191214

192215
void pbio_os_request_poll(void);
193216

194-
void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func, void *context);
217+
pbio_error_t pbio_port_process_none_thread(pbio_os_state_t *state, void *context);
218+
219+
void pbio_os_process_start(pbio_os_process_t *process, pbio_os_process_func_t func, void *context);
220+
221+
void pbio_os_process_reset(pbio_os_process_t *process, pbio_os_process_func_t func);
195222

196223
/**
197224
* Disables interrupts and returns the previous interrupt state.

lib/pbio/src/os.c

+29-5
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,23 @@ void pbio_os_request_poll(void) {
4747
static pbio_os_process_t *process_list = NULL;
4848

4949
/**
50-
* Starts a process.
50+
* Placeholder thread that does nothing.
5151
*
52-
* NB: Processes can be started only once. They cannot be restarted.
52+
* @param [in] state The protothread state.
53+
* @param [in] context The context.
54+
*/
55+
pbio_error_t pbio_port_process_none_thread(pbio_os_state_t *state, void *context) {
56+
return PBIO_ERROR_AGAIN;
57+
}
58+
59+
/**
60+
* Adds a process to the list of processes to run and starts it soon.
5361
*
5462
* @param process The process to start.
5563
* @param func The process thread function.
5664
* @param context The context to pass to the process.
5765
*/
58-
void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func, void *context) {
66+
void pbio_os_process_start(pbio_os_process_t *process, pbio_os_process_func_t func, void *context) {
5967

6068
// Add the new process to the end of the list.
6169
pbio_os_process_t *last = process_list;
@@ -69,11 +77,27 @@ void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t fu
6977
}
7078

7179
// Initialize the process.
72-
process->func = func;
73-
process->context = context;
7480
process->next = NULL;
81+
process->context = context;
82+
83+
pbio_os_process_reset(process, func);
84+
}
85+
86+
/**
87+
* Resets an existing process to the initial state with a new function and context.
88+
*
89+
* @param process The process to start.
90+
* @param func The process thread function. Choose NULL if it does not need changing.
91+
*/
92+
void pbio_os_process_reset(pbio_os_process_t *process, pbio_os_process_func_t func) {
7593
process->err = PBIO_ERROR_AGAIN;
7694
process->state = 0;
95+
if (func) {
96+
process->func = func;
97+
}
98+
99+
// Request a poll to start the process soon, running to its first yield.
100+
pbio_os_request_poll();
77101
}
78102

79103
/**

0 commit comments

Comments
 (0)