Vhex-kernel/src/kernel/scheduler/process/alloc.c

62 lines
1.3 KiB
C
Raw Normal View History

2019-12-29 16:39:30 +01:00
#include <kernel/process.h>
#include <kernel/devices/earlyterm.h>
#include <kernel/util/atomic.h>
#include <kernel/memory.h>
2019-12-29 16:39:30 +01:00
// TODO: return EAGAIN !!
// TODO: return ENOMEM !!
struct process *process_alloc(void)
2019-12-29 16:39:30 +01:00
{
extern struct process *proc_table;
extern uint32_t proc_table_max;
uint32_t proc_table_idx;
struct process **proc;
2019-12-29 16:39:30 +01:00
// Start atomic operation
atomic_start();
// Try to find free slot
proc = &proc_table;
proc_table_idx = -1;
while (++proc_table_idx < proc_table_max && *proc != NULL)
2019-12-29 16:39:30 +01:00
{
// Check free slot
if ((*proc)->status != PROC_DEAD) {
proc = &(*proc)->next;
continue;
2019-12-29 16:39:30 +01:00
}
// Indicate init phase, stop atomic
// operations and return the process
(*proc)->status = PROC_INIT;
(*proc)->pgid = 0;
atomic_stop();
return (*proc);
}
// Check error
if (proc_table_idx >= proc_table_max) {
earlyterm_write("proc_alloc: EAGAIN !\n");
atomic_stop();
return (NULL);
2019-12-29 16:39:30 +01:00
}
// Alloc new process manually
*proc = (struct process *)pm_alloc(sizeof(struct process));
if (*proc == NULL) {
earlyterm_write("proc_alloc: ENOMEM !\n");
atomic_stop();
return (NULL);
}
// Init and return the now process
(*proc)->status = PROC_INIT;
(*proc)->pid = proc_table_idx + 1;
(*proc)->pgid = 0;
(*proc)->next = NULL;
// Stop atomic operations
atomic_stop();
return (*proc);
2019-12-29 16:39:30 +01:00
}