]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtos/Thread.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rtos / rtos / Thread.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2012 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef THREAD_H
23 #define THREAD_H
24
25 #include <stdint.h>
26 #include "cmsis_os.h"
27
28 namespace rtos {
29
30 /** The Thread class allow defining, creating, and controlling thread functions in the system. */
31 class Thread {
32 public:
33     /** Create a new thread, and start it executing the specified function.
34       @param   task           function to be executed by this thread.
35       @param   argument       pointer that is passed to the thread function as start argument. (default: NULL).
36       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
37       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
38       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
39     */
40     Thread(void (*task)(void const *argument), void *argument=NULL,
41            osPriority priority=osPriorityNormal,
42            uint32_t stack_size=DEFAULT_STACK_SIZE,
43            unsigned char *stack_pointer=NULL);
44
45     /** Terminate execution of a thread and remove it from Active Threads
46       @return  status code that indicates the execution status of the function.
47     */
48     osStatus terminate();
49
50     /** Set priority of an active thread
51       @param   priority  new priority value for the thread function.
52       @return  status code that indicates the execution status of the function.
53     */
54     osStatus set_priority(osPriority priority);
55
56     /** Get priority of an active thread
57       @return  current priority value of the thread function.
58     */
59     osPriority get_priority();
60
61     /** Set the specified Signal Flags of an active thread.
62       @param   signals  specifies the signal flags of the thread that should be set.
63       @return  previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
64     */
65     int32_t signal_set(int32_t signals);
66
67     /** State of the Thread */
68     enum State {
69         Inactive,           /**< Not created or terminated */
70         Ready,              /**< Ready to run */
71         Running,            /**< Running */
72         WaitingDelay,       /**< Waiting for a delay to occur */
73         WaitingInterval,    /**< Waiting for an interval to occur */
74         WaitingOr,          /**< Waiting for one event in a set to occur */
75         WaitingAnd,         /**< Waiting for multiple events in a set to occur */
76         WaitingSemaphore,   /**< Waiting for a semaphore event to occur */
77         WaitingMailbox,     /**< Waiting for a mailbox event to occur */
78         WaitingMutex,       /**< Waiting for a mutex event to occur */
79     };
80
81     /** State of this Thread
82       @return  the State of this Thread
83     */
84     State get_state();
85
86     /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
87       @param   signals   wait until all specified signal flags set or 0 for any single signal flag.
88       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
89       @return  event flag information or error code.
90     */
91     static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever);
92
93     /** Wait for a specified time period in millisec:
94       @param   millisec  time delay value
95       @return  status code that indicates the execution status of the function.
96     */
97     static osStatus wait(uint32_t millisec);
98
99     /** Pass control to next thread that is in state READY.
100       @return  status code that indicates the execution status of the function.
101     */
102     static osStatus yield();
103
104     /** Get the thread id of the current running thread.
105       @return  thread ID for reference by other functions or NULL in case of error.
106     */
107     static osThreadId gettid();
108
109     virtual ~Thread();
110
111 private:
112     osThreadId _tid;
113     osThreadDef_t _thread_def;
114     bool _dynamic_stack;
115 };
116
117 }
118 #endif