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