xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/linux/src/os_task.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "os/os.h"
21 #include "nimble/nimble_npl.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /**
28  * Initialize a task.
29  *
30  * This function initializes the task structure pointed to by t,
31  * clearing and setting it's stack pointer, provides sane defaults
32  * and sets the task as ready to run, and inserts it into the operating
33  * system scheduler.
34  *
35  * @param t The task to initialize
36  * @param name The name of the task to initialize
37  * @param func The task function to call
38  * @param arg The argument to pass to this task function
39  * @param prio The priority at which to run this task
40  * @param sanity_itvl The time at which this task should check in with the
41  *                    sanity task.  OS_WAIT_FOREVER means never check in
42  *                    here.
43  * @param stack_bottom A pointer to the bottom of a task's stack
44  * @param stack_size The overall size of the task's stack.
45  *
46  * @return 0 on success, non-zero on failure.
47  */
48 int
ble_npl_task_init(struct ble_npl_task * t,const char * name,ble_npl_task_func_t func,void * arg,uint8_t prio,ble_npl_time_t sanity_itvl,ble_npl_stack_t * stack_bottom,uint16_t stack_size)49 ble_npl_task_init(struct ble_npl_task *t, const char *name, ble_npl_task_func_t func,
50         void *arg, uint8_t prio, ble_npl_time_t sanity_itvl,
51         ble_npl_stack_t *stack_bottom, uint16_t stack_size)
52 {
53     int err;
54     if ((t == NULL) || (func == NULL)) {
55         return OS_INVALID_PARM;
56     }
57 
58     pthread_attr_t attr;
59     struct sched_param param;
60     err = pthread_attr_init(&attr);
61     if (err) return err;
62     err = pthread_attr_getschedparam (&attr, &param);
63     if (err) return err;
64     err = pthread_attr_setschedpolicy(&attr, SCHED_RR);
65     if (err) return err;
66     param.sched_priority = prio;
67     err = pthread_attr_setschedparam (&attr, &param);
68     if (err) return err;
69 
70     t->name = name;
71     err = pthread_create(&t->handle, &attr, func, arg);
72 
73     return err;
74 }
75 
76 /*
77  * Removes specified task
78  * XXX
79  * NOTE: This interface is currently experimental and not ready for common use
80  */
81 int
ble_npl_task_remove(struct ble_npl_task * t)82 ble_npl_task_remove(struct ble_npl_task *t)
83 {
84     return pthread_cancel(t->handle);
85 }
86 
87 /**
88  * Return the number of tasks initialized.
89  *
90  * @return number of tasks initialized
91  */
92 uint8_t
ble_npl_task_count(void)93 ble_npl_task_count(void)
94 {
95     return 0;
96 }
97 
98 void *
ble_npl_get_current_task_id(void)99 ble_npl_get_current_task_id(void)
100 {
101     return (void *)pthread_self();
102 }
103 
ble_npl_os_started(void)104 bool ble_npl_os_started(void)
105 {
106     return true;
107 }
108 
ble_npl_task_yield(void)109 void ble_npl_task_yield(void)
110 {
111     pthread_yield();
112 }
113 
114 #ifdef __cplusplus
115 }
116 #endif
117