diff options
author | sterling <sterling@micosa.net> | 2015-07-13 18:43:37 -0700 |
---|---|---|
committer | sterling <sterling@micosa.net> | 2015-07-13 18:43:37 -0700 |
commit | 99724a9af650efcee7de43fd8262e4f444251bb7 (patch) | |
tree | e06d07b6bcd9050cf0f5dd08e492b7077ce818f2 /libs/os/src/os_task.c | |
parent | 4481bf7ddb5a740e6e8d3f54e7a0c4bf1237819b (diff) | |
parent | afec057da8f9379cd581295f43414030666aff42 (diff) |
Merge commit 'afec057da8f9379cd581295f43414030666aff42' as 'libs/os'
Diffstat (limited to 'libs/os/src/os_task.c')
-rw-r--r-- | libs/os/src/os_task.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/libs/os/src/os_task.c b/libs/os/src/os_task.c new file mode 100644 index 00000000..31e77f53 --- /dev/null +++ b/libs/os/src/os_task.c @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2015 Stack Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "os/os.h" + +#include <string.h> + +uint8_t g_task_id; + +static void +_clear_stack(os_stack_t *stack_bottom, int size) +{ + int i; + + for (i = 0; i < size; i++) { + stack_bottom[i] = OS_STACK_PATTERN; + } +} + +static inline uint8_t +os_task_next_id(void) +{ + uint8_t rc; + os_sr_t sr; + + OS_ENTER_CRITICAL(sr); + rc = g_task_id; + g_task_id++; + OS_EXIT_CRITICAL(sr); + + return (rc); +} + +int +os_task_init(struct os_task *t, char *name, os_task_func_t func, void *arg, + uint8_t prio, os_stack_t *stack_bottom, uint16_t stack_size) +{ + int rc; + + memset(t, 0, sizeof(*t)); + + t->t_func = func; + t->t_arg = arg; + + t->t_taskid = os_task_next_id(); + t->t_prio = prio; + + t->t_state = OS_TASK_READY; + t->t_name = name; + t->t_next_wakeup = 0; + + _clear_stack(stack_bottom, stack_size); + t->t_stackptr = os_arch_task_stack_init(t, &stack_bottom[stack_size], + stack_size); + + /* insert this task into the scheduler list */ + rc = os_sched_insert(t, 0); + if (rc != OS_OK) { + goto err; + } + + return (0); +err: + return (rc); +} + |