joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

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