Lcd companion boards support (VKLCD50RTA & VKLCD70RT)

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 #include "Callback.h"
00028 #include "toolchain.h"
00029 #include "Semaphore.h"
00030 #include "Mutex.h"
00031 
00032 namespace rtos {
00033 
00034 /** The Thread class allow defining, creating, and controlling thread functions in the system. */
00035 class Thread {
00036 public:
00037     /** Allocate a new thread without starting execution
00038       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
00039       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
00040       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
00041     */
00042     Thread(osPriority priority=osPriorityNormal,
00043            uint32_t stack_size=DEFAULT_STACK_SIZE,
00044            unsigned char *stack_pointer=NULL) {
00045         constructor(priority, stack_size, stack_pointer);
00046     }
00047 
00048     /** Create a new thread, and start it executing the specified function.
00049       @param   task           function to be executed by this thread.
00050       @param   argument       pointer that is passed to the thread function as start argument. (default: NULL).
00051       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
00052       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
00053       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
00054       @deprecated
00055         Thread-spawning constructors hide errors and may lead to complex
00056         program state when a thread is declared.
00057 
00058         The explicit Thread::start member function should be used to spawn
00059         a thread.
00060     */
00061     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00062         "Thread-spawning constructors hide errors and may lead to complex "
00063         "program state when a thread is declared")
00064     Thread(mbed::Callback<void()> task,
00065            osPriority priority=osPriorityNormal,
00066            uint32_t stack_size=DEFAULT_STACK_SIZE,
00067            unsigned char *stack_pointer=NULL) {
00068         constructor(task, priority, stack_size, stack_pointer);
00069     }
00070 
00071     /** Create a new thread, and start it executing the specified function.
00072       @param   obj            argument to task.
00073       @param   method         function to be executed by this thread.
00074       @param   argument       pointer that is passed to the thread function as start argument. (default: NULL).
00075       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
00076       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
00077       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
00078       @deprecated
00079         Thread-spawning constructors hide errors and may lead to complex
00080         program state when a thread is declared.
00081 
00082         The explicit Thread::start member function should be used to spawn
00083         a thread.
00084     */
00085     template <typename T>
00086     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00087         "Thread-spawning constructors hide errors and may lead to complex "
00088         "program state when a thread is declared")
00089     Thread(T *obj, void (T::*method)(),
00090            osPriority priority=osPriorityNormal,
00091            uint32_t stack_size=DEFAULT_STACK_SIZE,
00092            unsigned char *stack_pointer=NULL) {
00093         constructor(mbed::Callback<void()>(obj, method),
00094                     priority, stack_size, stack_pointer);
00095     }
00096 
00097     /** Create a new thread, and start it executing the specified function.
00098       @param   obj            argument to task.
00099       @param   method         function to be executed by this thread.
00100       @param   argument       pointer that is passed to the thread function as start argument. (default: NULL).
00101       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
00102       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
00103       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
00104       @deprecated
00105         Thread-spawning constructors hide errors and may lead to complex
00106         program state when a thread is declared.
00107 
00108         The explicit Thread::start member function should be used to spawn
00109         a thread.
00110     */
00111     template <typename T>
00112     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00113         "Thread-spawning constructors hide errors and may lead to complex "
00114         "program state when a thread is declared")
00115     Thread(T *obj, void (*method)(T *),
00116            osPriority priority=osPriorityNormal,
00117            uint32_t stack_size=DEFAULT_STACK_SIZE,
00118            unsigned char *stack_pointer=NULL) {
00119         constructor(mbed::Callback<void()>(obj, method),
00120                     priority, stack_size, stack_pointer);
00121     }
00122 
00123     /** Create a new thread, and start it executing the specified function.
00124         Provided for backwards compatibility
00125       @param   task           function to be executed by this thread.
00126       @param   argument       pointer that is passed to the thread function as start argument. (default: NULL).
00127       @param   priority       initial priority of the thread function. (default: osPriorityNormal).
00128       @param   stack_size      stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
00129       @param   stack_pointer  pointer to the stack area to be used by this thread (default: NULL).
00130       @deprecated
00131         Thread-spawning constructors hide errors and may lead to complex
00132         program state when a thread is declared.
00133 
00134         The explicit Thread::start member function should be used to spawn
00135         a thread.
00136     */
00137     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00138         "Thread-spawning constructors hide errors and may lead to complex "
00139         "program state when a thread is declared")
00140     Thread(void (*task)(void const *argument), void *argument=NULL,
00141            osPriority priority=osPriorityNormal,
00142            uint32_t stack_size=DEFAULT_STACK_SIZE,
00143            unsigned char *stack_pointer=NULL) {
00144         constructor(mbed::Callback<void()>(argument, (void (*)(void *))task),
00145                     priority, stack_size, stack_pointer);
00146     }
00147 
00148     /** Starts a thread executing the specified function.
00149       @param   task           function to be executed by this thread.
00150       @return  status code that indicates the execution status of the function.
00151     */
00152     osStatus start(mbed::Callback<void()> task);
00153 
00154     /** Starts a thread executing the specified function.
00155       @param   obj            argument to task
00156       @param   method         function to be executed by this thread.
00157       @return  status code that indicates the execution status of the function.
00158     */
00159     template <typename T, typename M>
00160     osStatus start(T *obj, M method) {
00161         return start(mbed::Callback<void()>(obj, method));
00162     }
00163 
00164     /** Wait for thread to terminate
00165       @return  status code that indicates the execution status of the function.
00166       @note not callable from interrupt
00167     */
00168     osStatus join();
00169 
00170     /** Terminate execution of a thread and remove it from Active Threads
00171       @return  status code that indicates the execution status of the function.
00172     */
00173     osStatus terminate();
00174 
00175     /** Set priority of an active thread
00176       @param   priority  new priority value for the thread function.
00177       @return  status code that indicates the execution status of the function.
00178     */
00179     osStatus set_priority(osPriority priority);
00180 
00181     /** Get priority of an active thread
00182       @return  current priority value of the thread function.
00183     */
00184     osPriority get_priority();
00185 
00186     /** Set the specified Signal Flags of an active thread.
00187       @param   signals  specifies the signal flags of the thread that should be set.
00188       @return  previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
00189     */
00190     int32_t signal_set(int32_t signals);
00191 
00192     /** Clears the specified Signal Flags of an active thread.
00193       @param   signals  specifies the signal flags of the thread that should be cleared.
00194       @return  resultant signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
00195     */
00196     int32_t signal_clr(int32_t signals);
00197 
00198     /** State of the Thread */
00199     enum State {
00200         Inactive,           /**< Not created or terminated */
00201         Ready,              /**< Ready to run */
00202         Running,            /**< Running */
00203         WaitingDelay,       /**< Waiting for a delay to occur */
00204         WaitingInterval,    /**< Waiting for an interval to occur */
00205         WaitingOr,          /**< Waiting for one event in a set to occur */
00206         WaitingAnd,         /**< Waiting for multiple events in a set to occur */
00207         WaitingSemaphore,   /**< Waiting for a semaphore event to occur */
00208         WaitingMailbox,     /**< Waiting for a mailbox event to occur */
00209         WaitingMutex,       /**< Waiting for a mutex event to occur */
00210 
00211         /* Not in sync with RTX below here */
00212         Deleted,            /**< The task has been deleted */
00213     };
00214 
00215     /** State of this Thread
00216       @return  the State of this Thread
00217     */
00218     State get_state();
00219     
00220     /** Get the total stack memory size for this Thread
00221       @return  the total stack memory size in bytes
00222     */
00223     uint32_t stack_size();
00224     
00225     /** Get the currently unused stack memory for this Thread
00226       @return  the currently unused stack memory in bytes
00227     */
00228     uint32_t free_stack();
00229     
00230     /** Get the currently used stack memory for this Thread
00231       @return  the currently used stack memory in bytes
00232     */
00233     uint32_t used_stack();
00234     
00235     /** Get the maximum stack memory usage to date for this Thread
00236       @return  the maximum stack memory usage to date in bytes
00237     */
00238     uint32_t max_stack();
00239 
00240     /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
00241       @param   signals   wait until all specified signal flags set or 0 for any single signal flag.
00242       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00243       @return  event flag information or error code.
00244       @note not callable from interrupt
00245     */
00246     static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever);
00247 
00248     /** Wait for a specified time period in millisec:
00249       @param   millisec  time delay value
00250       @return  status code that indicates the execution status of the function.
00251       @note not callable from interrupt
00252     */
00253     static osStatus wait(uint32_t millisec);
00254 
00255     /** Pass control to next thread that is in state READY.
00256       @return  status code that indicates the execution status of the function.
00257       @note not callable from interrupt
00258     */
00259     static osStatus yield();
00260 
00261     /** Get the thread id of the current running thread.
00262       @return  thread ID for reference by other functions or NULL in case of error.
00263     */
00264     static osThreadId gettid();
00265     
00266     /** Attach a function to be called by the RTOS idle task
00267       @param   fptr  pointer to the function to be called
00268     */
00269     static void attach_idle_hook(void (*fptr)(void));
00270 
00271     virtual ~Thread();
00272 
00273 private:
00274     // Required to share definitions without
00275     // delegated constructors
00276     void constructor(osPriority priority=osPriorityNormal,
00277                      uint32_t stack_size=DEFAULT_STACK_SIZE,
00278                      unsigned char *stack_pointer=NULL);
00279     void constructor(mbed::Callback<void()> task,
00280                      osPriority priority=osPriorityNormal,
00281                      uint32_t stack_size=DEFAULT_STACK_SIZE,
00282                      unsigned char *stack_pointer=NULL);
00283     static void _thunk(const void * thread_ptr);
00284 
00285     mbed::Callback<void()> _task;
00286     osThreadId _tid;
00287     osThreadDef_t _thread_def;
00288     bool _dynamic_stack;
00289     Semaphore _join_sem;
00290     Mutex _mutex;
00291 };
00292 
00293 }
00294 #endif
00295