]> git.sesse.net Git - nageru/blob - futatabi/queue_spot_holder.h
Log a warning when we kill a client that is not keeping up.
[nageru] / futatabi / queue_spot_holder.h
1 #ifndef _QUEUE_SPOT_HOLDER
2 #define _QUEUE_SPOT_HOLDER 1
3
4 // A RAII class to hold a shared resource, in our case an (unordered!) spot in a queue,
5 // for as long as a frame is under computation.
6
7 class QueueInterface {
8 public:
9         virtual ~QueueInterface() {}
10         virtual void take_queue_spot() = 0;
11         virtual void release_queue_spot() = 0;
12 };
13
14 class QueueSpotHolder {
15 public:
16         QueueSpotHolder()
17                 : queue(nullptr) {}
18
19         explicit QueueSpotHolder(QueueInterface *queue)
20                 : queue(queue)
21         {
22                 queue->take_queue_spot();
23         }
24
25         QueueSpotHolder(QueueSpotHolder &&other)
26                 : queue(other.queue)
27         {
28                 other.queue = nullptr;
29         }
30
31         QueueSpotHolder &operator=(QueueSpotHolder &&other)
32         {
33                 queue = other.queue;
34                 other.queue = nullptr;
35                 return *this;
36         }
37
38         ~QueueSpotHolder()
39         {
40                 if (queue != nullptr) {
41                         queue->release_queue_spot();
42                 }
43         }
44
45         // Movable only.
46         QueueSpotHolder(QueueSpotHolder &) = delete;
47         QueueSpotHolder &operator=(QueueSpotHolder &) = delete;
48
49 private:
50         QueueInterface *queue;
51 };
52
53 #endif  // !defined(_QUEUE_SPOT_HOLDER)