updated AUTHORS
[platform/core/graphics/tizenvg.git] / src / lib / tvgTask.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17 #ifndef _TVG_TASK_H_
18 #define _TVG_TASK_H_
19
20 #include <memory>
21 #include <future>
22
23 namespace tvg
24 {
25
26 /*
27   Task Interface.
28   Able to run a task in the thread pool. derive from the
29   task interface and implement run method.
30
31   To get the result call task->get() which will return immidiately if the
32   task is already finishd otherwise will wait till task completion.
33  */
34
35 class Task
36 {
37 public:
38     virtual ~Task() = default;
39     void get() { if (_receiver.valid()) _receiver.get(); }
40
41 protected:
42     virtual void run() = 0;
43 private:
44     void operator()()
45     {
46         run();
47         _sender.set_value();
48     }
49     void prepare()
50     {
51         _sender = std::promise<void>();
52         _receiver = _sender.get_future();
53     }
54     friend class Executor;
55
56     std::promise<void> _sender;
57     std::future<void>  _receiver;
58 };
59
60
61 using shared_task = std::shared_ptr<Task>;
62
63 /*
64   async() function takes a shared task and runs it in
65   a thread pool asyncronously. call get() on the shared_task
66   to get the ressult out of the shared_task.
67  */
68 void async(shared_task task);
69
70 }
71
72 #endif //_TVG_TASK_H_