Skip to content

Commit b285ce5

Browse files
committed
pbio/os: Add way to request cancellation.
This will eventually replace task->cancel as well as PROCESS_EXITHANDLER.
1 parent 7ee0908 commit b285ce5

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/pbio/include/pbio/os.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ typedef pbio_error_t (*pbio_os_process_func_t)(pbio_os_state_t *state, void *con
8383

8484
typedef struct _pbio_os_process_t pbio_os_process_t;
8585

86+
/**
87+
* Requests that can be made to a process.
88+
*
89+
* The event loop will not enforce any particular response. It is up to the
90+
* thread to handle (or ignore) the request and exit with some return code when
91+
* it sees fit, not necessarily immediately.
92+
*/
93+
typedef enum {
94+
/**
95+
* No requests.
96+
*/
97+
PBIO_OS_PROCESS_REQUEST_TYPE_NONE = 0,
98+
/**
99+
* Ask the process to cancel/exit.
100+
*/
101+
PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL = 1 << 1,
102+
} pbio_os_process_request_type_t;
103+
86104
/**
87105
* A process.
88106
*/
@@ -107,6 +125,11 @@ struct _pbio_os_process_t {
107125
* Most recent result of running one iteration of the protothread.
108126
*/
109127
pbio_error_t err;
128+
/**
129+
* Request made to the process such as cancellation. It is up to the
130+
* thread function to implement how to respond, if at all.
131+
*/
132+
pbio_os_process_request_type_t request;
110133
};
111134

112135
/**

lib/pbio/src/os.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void pbio_os_request_poll(void) {
4949
static pbio_os_process_t *process_list = NULL;
5050

5151
/**
52-
* Placeholder thread that does nothing.
52+
* Placeholder thread that does nothing and never completes.
5353
*
5454
* @param [in] state The protothread state.
5555
* @param [in] context The context.
@@ -98,6 +98,7 @@ void pbio_os_process_start(pbio_os_process_t *process, pbio_os_process_func_t fu
9898
void pbio_os_process_init(pbio_os_process_t *process, pbio_os_process_func_t func) {
9999
process->err = PBIO_ERROR_AGAIN;
100100
process->state = 0;
101+
process->request = PBIO_OS_PROCESS_REQUEST_TYPE_NONE;
101102
if (func) {
102103
process->func = func;
103104
}
@@ -106,6 +107,20 @@ void pbio_os_process_init(pbio_os_process_t *process, pbio_os_process_func_t fun
106107
pbio_os_request_poll();
107108
}
108109

110+
/**
111+
* Makes a request to a process.
112+
*
113+
* It is like sending a signal to the process. It is up to the process to
114+
* respond or ignore it.
115+
*
116+
* @param process The process.
117+
* @param request The request to make.
118+
*/
119+
void pbio_os_process_make_request(pbio_os_process_t *process, pbio_os_process_request_type_t request) {
120+
process->request = request;
121+
pbio_os_request_poll();
122+
}
123+
109124
/**
110125
* Drives the event loop once: Runs one iteration of all processes.
111126
*

0 commit comments

Comments
 (0)