forked from NVIDIA/gpu-rest-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
82 lines (69 loc) · 1.7 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef COMMON_H
#define COMMON_H
#include <memory>
#include <condition_variable>
#include <mutex>
#include <queue>
/* A simple threadsafe queue using a mutex and a condition variable. */
template <typename T>
class Queue
{
public:
Queue() = default;
Queue(Queue&& other)
{
std::unique_lock<std::mutex> lock(other.mutex_);
queue_ = std::move(other.queue_);
}
void Push(T value)
{
std::unique_lock<std::mutex> lock(mutex_);
queue_.push(std::move(value));
lock.unlock();
cond_.notify_one();
}
T Pop()
{
std::unique_lock<std::mutex> lock(mutex_);
cond_.wait(lock, [this]{return !queue_.empty();});
T value = std::move(queue_.front());
queue_.pop();
return value;
}
size_t Size()
{
std::unique_lock<std::mutex> lock(mutex_);
return queue_.size();
}
private:
mutable std::mutex mutex_;
std::queue<T> queue_;
std::condition_variable cond_;
};
/* A pool of available contexts is simply implemented as a queue for our example. */
template <typename Context>
using ContextPool = Queue<std::unique_ptr<Context>>;
/* A RAII class for acquiring an execution context from a context pool. */
template <typename Context>
class ScopedContext
{
public:
explicit ScopedContext(ContextPool<Context>& pool)
: pool_(pool), context_(pool_.Pop())
{
context_->Activate();
}
~ScopedContext()
{
context_->Deactivate();
pool_.Push(std::move(context_));
}
Context* operator->() const
{
return context_.get();
}
private:
ContextPool<Context>& pool_;
std::unique_ptr<Context> context_;
};
#endif // COMMON_H