Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   DISCO-F746NG_rtos_test ecte433

Fork of mbed-rtos by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Thread.h Source File

Thread.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef THREAD_H
00023 #define THREAD_H
00024 
00025 #include <stdint.h>
00026 #include "cmsis_os.h"
00027 
00028 namespace rtos {
00029 
00030 /** The Thread class allow defining, creating, and controlling thread functions in the system. */
00031 class Thread {
00032 public:
00033     /** Create a new thread, and start it executing the specified function.
00034       @param   task           function to be executed by this thread.
00035       @param   argument       pointer that is passed to the thread function as start argument. (default: NULL).
00036       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
00037       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
00038       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
00039     */
00040     Thread(void (*task)(void const *argument), void *argument=NULL,
00041            osPriority priority=osPriorityNormal,
00042            uint32_t stack_size=DEFAULT_STACK_SIZE,
00043            unsigned char *stack_pointer=NULL);
00044 
00045     /** Terminate execution of a thread and remove it from Active Threads
00046       @return  status code that indicates the execution status of the function.
00047     */
00048     osStatus terminate();
00049 
00050     /** Set priority of an active thread
00051       @param   priority  new priority value for the thread function.
00052       @return  status code that indicates the execution status of the function.
00053     */
00054     osStatus set_priority(osPriority priority);
00055 
00056     /** Get priority of an active thread
00057       @return  current priority value of the thread function.
00058     */
00059     osPriority get_priority();
00060 
00061     /** Set the specified Signal Flags of an active thread.
00062       @param   signals  specifies the signal flags of the thread that should be set.
00063       @return  previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
00064     */
00065     int32_t signal_set(int32_t signals);
00066 
00067     /** Clears the specified Signal Flags of an active thread.
00068       @param   signals  specifies the signal flags of the thread that should be cleared.
00069       @return  resultant signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
00070     */
00071     int32_t signal_clr(int32_t signals);
00072 
00073     /** State of the Thread */
00074     enum State {
00075         Inactive,           /**< Not created or terminated */
00076         Ready,              /**< Ready to run */
00077         Running,            /**< Running */
00078         WaitingDelay,       /**< Waiting for a delay to occur */
00079         WaitingInterval,    /**< Waiting for an interval to occur */
00080         WaitingOr,          /**< Waiting for one event in a set to occur */
00081         WaitingAnd,         /**< Waiting for multiple events in a set to occur */
00082         WaitingSemaphore,   /**< Waiting for a semaphore event to occur */
00083         WaitingMailbox,     /**< Waiting for a mailbox event to occur */
00084         WaitingMutex,       /**< Waiting for a mutex event to occur */
00085     };
00086 
00087     /** State of this Thread
00088       @return  the State of this Thread
00089     */
00090     State get_state();
00091     
00092     /** Get the total stack memory size for this Thread
00093       @return  the total stack memory size in bytes
00094     */
00095     uint32_t stack_size();
00096     
00097     /** Get the currently unused stack memory for this Thread
00098       @return  the currently unused stack memory in bytes
00099     */
00100     uint32_t free_stack();
00101     
00102     /** Get the currently used stack memory for this Thread
00103       @return  the currently used stack memory in bytes
00104     */
00105     uint32_t used_stack();
00106     
00107     /** Get the maximum stack memory usage to date for this Thread
00108       @return  the maximum stack memory usage to date in bytes
00109     */
00110     uint32_t max_stack();
00111 
00112     /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
00113       @param   signals   wait until all specified signal flags set or 0 for any single signal flag.
00114       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00115       @return  event flag information or error code.
00116     */
00117     static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever);
00118 
00119     /** Wait for a specified time period in millisec:
00120       @param   millisec  time delay value
00121       @return  status code that indicates the execution status of the function.
00122     */
00123     static osStatus wait(uint32_t millisec);
00124 
00125     /** Pass control to next thread that is in state READY.
00126       @return  status code that indicates the execution status of the function.
00127     */
00128     static osStatus yield();
00129 
00130     /** Get the thread id of the current running thread.
00131       @return  thread ID for reference by other functions or NULL in case of error.
00132     */
00133     static osThreadId gettid();
00134 
00135     virtual ~Thread();
00136 
00137 private:
00138     osThreadId _tid;
00139     osThreadDef_t _thread_def;
00140     bool _dynamic_stack;
00141 };
00142 
00143 }
00144 #endif