Skip to content

Commit

Permalink
sched: no two tasks can share the same name
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ahmed <ahmeddan @ amazon com>
  • Loading branch information
82marbag authored and wipawel committed Aug 30, 2020
1 parent 1d878cb commit 487f836
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
31 changes: 27 additions & 4 deletions common/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <console.h>
#include <errno.h>
#include <ktf.h>
#include <lib.h>
#include <list.h>
Expand Down Expand Up @@ -56,6 +57,8 @@ static const char *task_state_names[] = {
[TASK_STATE_DONE] = "DONE",
};

#define PAGE_ORDER_TASK PAGE_ORDER_4K

static inline void set_task_state(task_t *task, task_state_t state) {
ASSERT(task);

Expand All @@ -77,7 +80,7 @@ static inline task_state_t get_task_state(task_t *task) {
}

static task_t *create_task(void) {
task_t *task = get_free_page(GFP_KERNEL);
task_t *task = get_free_pages(PAGE_ORDER_TASK, GFP_KERNEL);

if (!task)
return NULL;
Expand All @@ -94,16 +97,32 @@ static task_t *create_task(void) {
return task;
}

static void prepare_task(task_t *task, const char *name, task_func_t func, void *arg) {
/* The caller should never use the parameter again after calling this function */
static void destroy_task(task_t *task) {
if (!task)
return;

spin_lock(&lock);
list_unlink(&task->list);
spin_unlock(&lock);

put_pages(task, PAGE_ORDER_TASK);
}

static int prepare_task(task_t *task, const char *name, task_func_t func, void *arg) {
if (!task)
return -EINVAL;

if (get_task_by_name(name))
return -EEXIST;

BUG_ON(get_task_state(task) > TASK_STATE_READY);

task->name = name;
task->func = func;
task->arg = arg;
set_task_state(task, TASK_STATE_READY);
return ESUCCESS;
}

static void wait_for_task_state(task_t *task, task_state_t state) {
Expand All @@ -120,7 +139,11 @@ task_t *new_task(const char *name, task_func_t func, void *arg) {
if (!task)
return NULL;

prepare_task(task, name, func, arg);
if (unlikely(prepare_task(task, name, func, arg) != ESUCCESS)) {
destroy_task(task);
return NULL;
}

return task;
}

Expand All @@ -139,7 +162,7 @@ task_t *get_task_by_name(const char *name) {
task_t *task;

list_for_each_entry (task, &tasks, list) {
if (!strcmp(task->name, name))
if (string_equal(task->name, name))
return task;
}

Expand Down
6 changes: 6 additions & 0 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ static inline const char *string_trim_whitspace(const char *s) {
return s;
}

static inline int string_empty(const char *s) { return !s || *s == '\0'; }

static inline int string_equal(const char *s1, const char *s2) {
return (!s1 || !s2) ? s1 == s2 : !strcmp(s1, s2);
}

/* External declarations */

extern unsigned long strtoul(const char *nptr, char **endptr, int base);
Expand Down

0 comments on commit 487f836

Please sign in to comment.