source: xplra/src/plugin/src/xplra/RequestQueue.h@ 92:e3a0abb22ef4

Last change on this file since 92:e3a0abb22ef4 was 92:e3a0abb22ef4, checked in by István Váradi <ivaradi@…>, 7 years ago

C++11 compatibility

File size: 5.3 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of XPLRA, a remote-access plugin for X-Plane
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29#ifndef XPLRA_REQUESTQUEUE_H
30#define XPLRA_REQUESTQUEUE_H
31//------------------------------------------------------------------------------
32
33#include <hu/varadiistvan/scpl/Mutex.h>
34#include <hu/varadiistvan/scpl/CondVar.h>
35
36#include <XPLMProcessing.h>
37
38#include <vector>
39
40//------------------------------------------------------------------------------
41
42// Define to 1, if the new XPLMScheduleFlightLoop function should be
43// used to schedule the flight loop only on-demand
44#define USE_SCHEDULE_FLIGHTLOOP 0
45
46//------------------------------------------------------------------------------
47
48namespace xplra {
49
50//------------------------------------------------------------------------------
51
52class Request;
53
54//------------------------------------------------------------------------------
55
56/**
57 * The queue of the requests. Client serving threads can add requests
58 * to the queue. The queue provides a flight loop callback, which it
59 * automatically schedules if a request is added to it, unless we are
60 * already disabled.
61 *
62 * An instance of the queue should be created in the plugin enable
63 * callback. The disable callback should disable the queue, so that it
64 * will not accept any further requests.
65 */
66class RequestQueue
67{
68private:
69 /**
70 * Type for the collection of the queued requests.
71 */
72 typedef std::vector<Request*> requests_t;
73
74#if !USE_SCHEDULE_FLIGHTLOOP
75 /**
76 * The interval of the flight loop.
77 */
78 static constexpr float flightLoopInterval = 0.1;
79#endif
80
81 /**
82 * The flight loop function.
83 */
84 static float flightLoop(float inElapsedSinceLastCall,
85 float inElapsedTimeSinceLastFlightLoop,
86 int inCounter, void* inRefCon);
87
88#if USE_SCHEDULE_FLIGHTLOOP
89 /**
90 * The ID of the flight loop running the queue.
91 */
92 XPLMFlightLoopID flightLoopID;
93#endif
94
95 /**
96 * The mutex protecting the queue.
97 */
98 hu::varadiistvan::scpl::Mutex mutex;
99
100 /**
101 * A conditional variable to notify the threads waiting for their
102 * request to be executed.
103 */
104 hu::varadiistvan::scpl::CondVar requestsDone;
105
106 /**
107 * Indicate if the queue is enabled or not.
108 */
109 volatile bool enabled;
110
111 /**
112 * The vector of the queued requests.
113 */
114 requests_t requests;
115
116public:
117 /**
118 * Construct the queue. It creates the flight loop, so it should
119 * be called from a plugin callback.
120 */
121 RequestQueue();
122
123 /**
124 * Execute the given request. It will be enqueued, and then this
125 * function will wait until the request is executed or the request
126 * queue is disabled.
127 *
128 * If the queue is already disabled when calling this function, it
129 * returns immediately with false.
130 *
131 * @return if the request was executed
132 */
133 bool execute(Request* request);
134
135 /**
136 * Disable the queue. It sets the flag indicating that the queue
137 * is disabled, and wakes up all waiters waiting on the
138 * conditional variable. It should be called from a plugin
139 * callback, as it also destroys the flight loop.
140 */
141 void disable();
142
143private:
144 /**
145 * Run the queue.
146 *
147 * It locks the mutex, swaps the vector of requests with an empty
148 * one, unlocks the mutex and runs the requests one by one. When
149 * all requests are executed, it wakes up all waiters waiting on
150 * the conditional variable.
151 *
152 * It is called from the flight loop function.
153 */
154 void run();
155};
156
157//------------------------------------------------------------------------------
158
159} /* namespace xplra */
160
161//------------------------------------------------------------------------------
162#endif // XPLRA_REQUESTQUEUE_H
163
164// Local Variables:
165// mode: C++
166// c-basic-offset: 4
167// indent-tabs-mode: nil
168// End:
Note: See TracBrowser for help on using the repository browser.