Skip to content

Commit f539414

Browse files
idle check callback
1 parent ca3821c commit f539414

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

include/taskswitch.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ typedef uint32_t gid_t;
3737
*/
3838
typedef uint8_t cpu_id_t;
3939

40+
struct process_t;
41+
42+
typedef bool (*activity_callback_t)(struct process_t*);
43+
4044
/**
4145
* @brief Represents a process in the system.
4246
*
@@ -61,6 +65,7 @@ typedef struct process_t {
6165
struct basic_ctx* code; /**< BASIC interpreter context */
6266
struct process_t* prev; /**< Previous process in doubly linked list */
6367
struct process_t* next; /**< Next process in doubly linked list */
68+
activity_callback_t check_idle; /**< If non-null, called to check if the process should remain idle */
6469
} process_t;
6570

6671
/**
@@ -216,3 +221,5 @@ const char* proc_set_csd(process_t* proc, const char* csd);
216221
* @brief Initialise the process subsystem.
217222
*/
218223
void init_process();
224+
225+
void proc_set_idle(process_t* proc, activity_callback_t callback);

src/taskswitch.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ process_t* proc_load(const char* fullpath, struct console* cons, pid_t parent_pi
9898
newproc->ppid = parent_pid;
9999
newproc->cons = cons;
100100
newproc->cpu = logical_cpu_id();
101+
newproc->check_idle = NULL;
101102
newproc->start_time = time(NULL);
102103
kfree_null(&programtext);
103104

@@ -148,18 +149,25 @@ process_t* proc_cur(uint8_t logical_cpu)
148149
return cur;
149150
}
150151

152+
bool check_wait_pid(process_t* proc) {
153+
if (proc_find(proc->waitpid) == NULL) {
154+
proc->waitpid = 0;
155+
return false;
156+
}
157+
return true;
158+
}
159+
151160
void proc_run(process_t* proc)
152161
{
153-
if (proc->waitpid == 0) {
162+
if (proc->check_idle && !proc->check_idle(proc)) {
163+
proc_set_idle(proc, NULL);
154164
basic_run(proc->code);
155165
return;
166+
} else if (proc->check_idle) {
167+
_mm_pause();
168+
return;
156169
}
157-
_mm_pause();
158-
if (proc_find(proc->waitpid) == NULL) {
159-
proc->waitpid = 0;
160-
proc->state = PROC_RUNNING;
161-
basic_run(proc->code);
162-
}
170+
basic_run(proc->code);
163171
}
164172

165173
process_t* proc_find(pid_t pid)
@@ -184,14 +192,24 @@ bool proc_kill_id(pid_t id)
184192
return true;
185193
}
186194

195+
void proc_set_idle(process_t* proc, activity_callback_t callback) {
196+
if (callback) {
197+
proc->check_idle = check_wait_pid;
198+
proc->state = PROC_SUSPENDED;
199+
} else {
200+
proc->check_idle = NULL;
201+
proc->state = PROC_RUNNING;
202+
}
203+
}
204+
187205
void proc_wait(process_t* proc, pid_t otherpid)
188206
{
189207
if (!proc_find(otherpid)) {
190208
/* Process would wait forever */
191209
return;
192210
}
193211
proc->waitpid = otherpid;
194-
proc->state = PROC_SUSPENDED;
212+
proc_set_idle(proc, check_wait_pid);
195213
}
196214

197215
const char* proc_set_csd(process_t* proc, const char* csd)

0 commit comments

Comments
 (0)