FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
workqueue.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4  *
5  * This file is part of the FreeRTOS Add-ons project.
6  *
7  * Source Code:
8  * https://github.com/michaelbecker/freertos-addons
9  *
10  * Project Page:
11  * http://michaelbecker.github.io/freertos-addons/
12  *
13  * On-line Documentation:
14  * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files
18  * (the "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so,subject to the
22  * following conditions:
23  *
24  * + The above copyright notice and this permission notice shall be included
25  * in all copies or substantial portions of the Software.
26  * + Credit is appreciated, but not required, if you find this project
27  * useful enough to include in your application, product, device, etc.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  *
37  ***************************************************************************/
38 
39 
40 #ifndef WORK_QUEUE_HPP_
41 #define WORK_QUEUE_HPP_
42 
43 #include "thread.hpp"
44 #include "queue.hpp"
45 #include "semaphore.hpp"
46 
47 
48 namespace cpp_freertos {
49 
50 
51 #define DEFAULT_MAX_WORK_ITEMS 10
52 #define DEFAULT_WORK_QUEUE_STACK_SIZE (configMINIMAL_STACK_SIZE * 2)
53 #define DEFAULT_WORK_QUEUE_PRIORITY (tskIDLE_PRIORITY + 1)
54 
55 
67 class WorkItem {
68 
70  //
71  // Public API
72  //
74  public:
75 
87  WorkItem(bool freeAfterComplete = false);
88 
92  virtual ~WorkItem();
93 
98  bool FreeAfterRun();
99 
104  virtual void Run() = 0;
105 
107  //
108  // Private API
109  // The internals of this wrapper class.
110  //
112  private:
118 };
119 
120 
126 class WorkQueue {
127 
129  //
130  // Public API
131  //
133  public:
145  WorkQueue( const char * const Name,
146  uint16_t StackDepth = DEFAULT_WORK_QUEUE_STACK_SIZE,
147  UBaseType_t Priority = DEFAULT_WORK_QUEUE_PRIORITY,
148  UBaseType_t MaxWorkItems = DEFAULT_MAX_WORK_ITEMS);
149 
159  WorkQueue( uint16_t StackDepth = DEFAULT_WORK_QUEUE_STACK_SIZE,
160  UBaseType_t Priority = DEFAULT_WORK_QUEUE_PRIORITY,
161  UBaseType_t MaxWorkItems = DEFAULT_MAX_WORK_ITEMS);
162 
163 #if (INCLUDE_vTaskDelete == 1)
164 
171  ~WorkQueue();
172 #else
173 //
174 // If we are using C++11 or later, take advantage of the
175 // newer features to find bugs.
176 //
177 #if __cplusplus >= 201103L
178 
182  ~WorkQueue() = delete;
183 #endif
184 #endif
185 
193  bool QueueWork(WorkItem *work);
194 
196  //
197  // Private API
198  // The internals of this class.
199  //
201  private:
202 
206  class CWorkerThread : public Thread {
207 
208  public:
209  CWorkerThread( const char * const Name,
210  uint16_t StackDepth,
211  UBaseType_t Priority,
212  WorkQueue *Parent);
213 
214  CWorkerThread( uint16_t StackDepth,
215  UBaseType_t Priority,
216  WorkQueue *Parent);
217 
218  virtual ~CWorkerThread();
219 
220  protected:
221  virtual void Run();
222 
223  private:
225  };
226 
231 
236 
241 };
242 
243 
244 }
245 #endif
246 
247 
const bool FreeItemAfterCompleted
Definition: workqueue.hpp:117
virtual void Run()=0
WorkItem(bool freeAfterComplete=false)
Definition: cworkqueue.cpp:46
CWorkerThread * WorkerThread
Definition: workqueue.hpp:230
#define DEFAULT_WORK_QUEUE_STACK_SIZE
Definition: workqueue.hpp:52
#define DEFAULT_WORK_QUEUE_PRIORITY
Definition: workqueue.hpp:53
BinarySemaphore * ThreadComplete
Definition: workqueue.hpp:240
#define DEFAULT_MAX_WORK_ITEMS
Definition: workqueue.hpp:51