|
7 | 7 | #include <process.h> |
8 | 8 | #include <cm/syscall.h> |
9 | 9 | #include <cm/err.h> |
| 10 | +#include <cm/cm.h> |
| 11 | +#include <stdbool.h> |
| 12 | + |
| 13 | +/*************************************************************************************************** |
| 14 | + * Handling of process events |
| 15 | + * Some events are handled automaticaly by the library while others are for the applications to |
| 16 | + * handle. The applications can register a hander function for each event and those will be called |
| 17 | + * when the associated event is read. |
| 18 | + * |
| 19 | + * Not that though every thread receives events, the application event handler function is common |
| 20 | + * and is shared by every thread & the parent process. Per thread handling of events can be |
| 21 | + * done by quering process id. |
| 22 | + ***************************************************************************************************/ |
| 23 | +static cm_event_handler app_event_handlers[OSIF_PROCESS_EVENTS_COUNT] = { 0 }; |
| 24 | + |
| 25 | +bool cm_process_register_event_handler (OSIF_ProcessEvents e, cm_event_handler h) |
| 26 | +{ |
| 27 | + if (app_event_handlers[e] != NULL) { |
| 28 | + CM_RETURN_ERROR__ (CM_ERR_EVENT_HANDLER_ALREADY_REGISTERED, false); |
| 29 | + } |
| 30 | + |
| 31 | + app_event_handlers[e] = h; |
| 32 | + return true; |
| 33 | +} |
| 34 | + |
| 35 | +/*************************************************************************************************** |
| 36 | +* Default handler for process events |
| 37 | +* |
| 38 | +* Note: |
| 39 | +* Since its upto each process to read their events queue and handle them, |
| 40 | +* it is important that cm_process_handle_events() gets called regularly. |
| 41 | +**************************************************************************************************/ |
| 42 | +bool cm_process_handle_events() |
| 43 | +{ |
| 44 | + volatile OSIF_ProcessEvent e = { 0 }; |
| 45 | + if (!cm_process_pop_event ((OSIF_ProcessEvent*)&e)) { |
| 46 | + CM_RETURN_ERROR__ (cm_get_os_error(), false); |
| 47 | + } |
| 48 | + |
| 49 | + switch (e.event) { |
| 50 | + case OSIF_PROCESS_EVENT_PROCCESS_YIELD_REQ: |
| 51 | + if (app_event_handlers[e.event]) { |
| 52 | + app_event_handlers[e.event]((const OSIF_ProcessEvent* const)&e); |
| 53 | + } |
| 54 | + cm_process_yield(); |
| 55 | + break; |
| 56 | + case OSIF_PROCESS_EVENT_PROCCESS_CHILD_KILLED: |
| 57 | + if (app_event_handlers[e.event]) { |
| 58 | + app_event_handlers[e.event]((const OSIF_ProcessEvent* const)&e); |
| 59 | + } |
| 60 | + break; |
| 61 | + case OSIF_PROCESS_EVENT_NONE: |
| 62 | + break; |
| 63 | + default: |
| 64 | + // TODO: Should panic! and kill the process |
| 65 | + break; |
| 66 | + } |
| 67 | + return true; |
| 68 | +} |
| 69 | +/**************************************************************************************************/ |
10 | 70 |
|
11 | 71 | INT cm_process_create (void* startLocation, SIZE binaryLengthBytes, bool isKernelMode) |
12 | 72 | { |
@@ -44,10 +104,3 @@ INT cm_thread_create (void (*startLocation)(), bool isKernelMode) |
44 | 104 | } |
45 | 105 | return pid; |
46 | 106 | } |
47 | | - |
48 | | -bool cm_process_is_yield_requested() |
49 | | -{ |
50 | | - volatile OSIF_ProcessEvent e = { 0 }; |
51 | | - cm_process_pop_event ((OSIF_ProcessEvent*)&e); |
52 | | - return (e.event == OSIF_PROCESS_EVENT_PROCCESS_YIELD_REQ); |
53 | | -} |
|
0 commit comments