updated licenses info.
[platform/core/uifw/lottie-player.git] / src / vector / vtaskqueue.h
1 /* 
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #ifndef VTASKQUEUE_H
20 #define VTASKQUEUE_H
21
22 #include <deque>
23
24 template <typename Task>
25 class TaskQueue {
26     using lock_t = std::unique_lock<std::mutex>;
27     std::deque<Task>      _q;
28     bool                    _done{false};
29     std::mutex              _mutex;
30     std::condition_variable _ready;
31
32 public:
33     bool try_pop(Task &task)
34     {
35         lock_t lock{_mutex, std::try_to_lock};
36         if (!lock || _q.empty()) return false;
37         task = std::move(_q.front());
38         _q.pop_front();
39         return true;
40     }
41
42     bool try_push(Task &&task)
43     {
44         {
45             lock_t lock{_mutex, std::try_to_lock};
46             if (!lock) return false;
47             _q.push_back(std::move(task));
48         }
49         _ready.notify_one();
50         return true;
51     }
52
53     void done()
54     {
55         {
56             lock_t lock{_mutex};
57             _done = true;
58         }
59         _ready.notify_all();
60     }
61
62     bool pop(Task &task)
63     {
64         lock_t lock{_mutex};
65         while (_q.empty() && !_done) _ready.wait(lock);
66         if (_q.empty()) return false;
67         task = std::move(_q.front());
68         _q.pop_front();
69         return true;
70     }
71
72     void push(Task &&task)
73     {
74         {
75             lock_t lock{_mutex};
76             _q.push_back(std::move(task));
77         }
78         _ready.notify_one();
79     }
80
81 };
82
83 #endif  // VTASKQUEUE_H