FreeRTOS Addons
Loading...
Searching...
No Matches
workqueue.hpp
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#include "freertos-config.h"
41
42#ifndef WORK_QUEUE_HPP_
43#define WORK_QUEUE_HPP_
44
45#include "thread.hpp"
46#include "queue.hpp"
47#include "semaphore.hpp"
48
49
50namespace cpp_freertos {
51
52
53#define DEFAULT_MAX_WORK_ITEMS 10
54#define DEFAULT_WORK_QUEUE_STACK_SIZE (configMINIMAL_STACK_SIZE * 2)
55#define DEFAULT_WORK_QUEUE_PRIORITY (tskIDLE_PRIORITY + 1)
56
57
69class WorkItem {
70
72 //
73 // Public API
74 //
76 public:
77
89 WorkItem(bool freeAfterComplete = false);
90
94 virtual ~WorkItem();
95
100 bool FreeAfterRun();
101
106 virtual void Run() = 0;
107
109 //
110 // Private API
111 // The internals of this wrapper class.
112 //
114 private:
119 const bool FreeItemAfterCompleted;
120};
121
122
129
131 //
132 // Public API
133 //
135 public:
147 WorkQueue( const char * const Name,
148 uint16_t StackDepth = DEFAULT_WORK_QUEUE_STACK_SIZE,
149 UBaseType_t Priority = DEFAULT_WORK_QUEUE_PRIORITY,
150 UBaseType_t MaxWorkItems = DEFAULT_MAX_WORK_ITEMS);
151
161 WorkQueue( uint16_t StackDepth = DEFAULT_WORK_QUEUE_STACK_SIZE,
162 UBaseType_t Priority = DEFAULT_WORK_QUEUE_PRIORITY,
163 UBaseType_t MaxWorkItems = DEFAULT_MAX_WORK_ITEMS);
164
165#if (INCLUDE_vTaskDelete == 1)
173 ~WorkQueue();
174#else
175//
176// If we are using C++11 or later, take advantage of the
177// newer features to find bugs.
178//
179#if __cplusplus >= 201103L
184 ~WorkQueue() = delete;
185#endif
186#endif
187
195 bool QueueWork(WorkItem *work);
196
198 //
199 // Private API
200 // The internals of this class.
201 //
203 private:
204
208 class CWorkerThread : public Thread {
209
210 public:
211 CWorkerThread( const char * const Name,
212 uint16_t StackDepth,
213 UBaseType_t Priority,
214 WorkQueue *Parent);
215
216 CWorkerThread( uint16_t StackDepth,
217 UBaseType_t Priority,
218 WorkQueue *Parent);
219
220 virtual ~CWorkerThread();
221
222 protected:
223 virtual void Run();
224
225 private:
226 const WorkQueue *ParentWorkQueue;
227 };
228
232 CWorkerThread *WorkerThread;
233
237 Queue *WorkItemQueue;
238
242 BinarySemaphore *ThreadComplete;
243};
244
245
246}
247#endif
248
249
Definition: semaphore.hpp:196
Definition: queue.hpp:117
Definition: thread.hpp:79
Definition: workqueue.hpp:69
virtual void Run()=0
virtual ~WorkItem()
Definition: cworkqueue.cpp:52
bool FreeAfterRun()
Definition: cworkqueue.cpp:57
Definition: workqueue.hpp:128
~WorkQueue()
Definition: cworkqueue.cpp:102
bool QueueWork(WorkItem *work)
Definition: cworkqueue.cpp:137
Definition: condition_variable.hpp:57