Interactive CLI 2.3
A cross-platform C++14 library for interactive command-line interfaces
Loading...
Searching...
No Matches
loopscheduler.h
Go to the documentation of this file.
1/*******************************************************************************
2 * CLI - A simple command line interface.
3 * Copyright (C) 2016-2021 Daniele Pallastrelli
4 *
5 * Boost Software License - Version 1.0 - August 17th, 2003
6 *
7 * Permission is hereby granted, free of charge, to any person or organization
8 * obtaining a copy of the software and accompanying documentation covered by
9 * this license (the "Software") to use, reproduce, display, distribute,
10 * execute, and transmit the Software, and to prepare derivative works of the
11 * Software, and to permit third-parties to whom the Software is furnished to
12 * do so, all subject to the following:
13 *
14 * The copyright notices in the Software and this entire statement, including
15 * the above license grant, this restriction and the following disclaimer,
16 * must be included in all copies of the Software, in whole or in part, and
17 * all derivative works of the Software, unless such copies or derivative
18 * works are solely in the form of machine-executable object code generated by
19 * a source language processor.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ******************************************************************************/
29
30#ifndef CLI_LOOPSCHEDULER_H_
31#define CLI_LOOPSCHEDULER_H_
32
33#include <queue>
34#include <thread>
35#include <mutex>
36#include <condition_variable>
37#include "scheduler.h"
38
39namespace cli
40{
41
47{
48public:
49 LoopScheduler() = default;
50 ~LoopScheduler() override
51 {
52 Stop();
53 }
54
55 // non copyable
56 LoopScheduler(const LoopScheduler&) = delete;
58
59 void Stop()
60 {
61 std::lock_guard<std::mutex> lck (mtx);
62 running = false;
63 cv.notify_all();
64 }
65
66 void Run()
67 {
68 while( ExecOne() ) {};
69 }
70
71 bool Stopped() const
72 {
73 std::lock_guard<std::mutex> lck (mtx);
74 return !running;
75 }
76
77 void Post(const std::function<void()>& f) override
78 {
79 std::lock_guard<std::mutex> lck (mtx);
80 tasks.push(f);
81 cv.notify_all();
82 }
83
84 bool ExecOne()
85 {
86 std::function<void()> task;
87 {
88 std::unique_lock<std::mutex> lck(mtx);
89 cv.wait(lck, [this](){ return !running || !tasks.empty(); });
90 if (!running)
91 return false;
92 task = tasks.front();
93 tasks.pop();
94 }
95
96 if (task)
97 task();
98
99 return true;
100 }
101
102 bool PollOne()
103 {
104 std::function<void()> task;
105 {
106 std::lock_guard<std::mutex> lck(mtx);
107 if (!running || tasks.empty())
108 return false;
109 task = tasks.front();
110 tasks.pop();
111 }
112
113 if (task)
114 task();
115
116 return true;
117 }
118
119private:
120 std::queue<std::function<void()>> tasks;
121 bool running{ true };
122 mutable std::mutex mtx;
123 std::condition_variable cv;
124};
125
126} // namespace cli
127
128#endif // CLI_LOOPSCHEDULER_H_
bool Stopped() const
LoopScheduler(const LoopScheduler &)=delete
void Post(const std::function< void()> &f) override
Submits a completion token or function object for execution.
LoopScheduler()=default
LoopScheduler & operator=(const LoopScheduler &)=delete
~LoopScheduler() override