Toyomasa Watarai / mbed-rtos

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     /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
00093       @param   signals   wait until all specified signal flags set or 0 for any single signal flag.
00094       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00095       @return  event flag information or error code.
00096     */
00097     static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever);
00098 
00099     /** Wait for a specified time period in millisec:
00100       @param   millisec  time delay value
00101       @return  status code that indicates the execution status of the function.
00102     */
00103     static osStatus wait(uint32_t millisec);
00104 
00105     /** Pass control to next thread that is in state READY.
00106       @return  status code that indicates the execution status of the function.
00107     */
00108     static osStatus yield();
00109 
00110     /** Get the thread id of the current running thread.
00111       @return  thread ID for reference by other functions or NULL in case of error.
00112     */
00113     static osThreadId gettid();
00114 
00115     virtual ~Thread();
00116 
00117 private:
00118     osThreadId _tid;
00119     osThreadDef_t _thread_def;
00120     bool _dynamic_stack;
00121 };
00122 
00123 }
00124 #endif