Daiki Kato / mbed-os-lychee

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

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