Publishing 2019 R3 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / include / event_impl.h
1 /*
2 // Copyright (c) 2016 Intel Corporation
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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #pragma once
19 #include "api/event.hpp"
20 #include "refcounted_obj.h"
21
22 #include <list>
23 #include <mutex>
24 #include <utility>
25
26 namespace cldnn {
27 struct user_event;
28
29 struct event_impl : public refcounted_obj<event_impl> {
30 public:
31     event_impl() = default;
32
33     void wait();
34     bool is_set();
35     virtual bool is_valid() const { return _attached; }
36     virtual void reset() {
37         _attached = false;
38         _set = false;
39         _profiling_captured = false;
40         _profiling_info.clear();
41     }
42     // returns true if handler has been successfully added
43     bool add_event_handler(event_handler handler, void* data);
44
45     const std::list<instrumentation::profiling_interval>& get_profiling_info();
46
47 private:
48     std::mutex _handlers_mutex;
49     std::list<std::pair<event_handler, void*>> _handlers;
50
51     bool _profiling_captured = false;
52     std::list<instrumentation::profiling_interval> _profiling_info;
53
54 protected:
55     bool _set = false;
56     bool _attached =
57         false;  // because ocl event can be attached later, we need mechanism to check if such event was attached
58     void call_handlers();
59
60     virtual void wait_impl() = 0;
61     virtual bool is_set_impl() = 0;
62     virtual bool add_event_handler_impl(event_handler, void*) { return true; }
63
64     // returns whether profiling info has been captures successfully and there's no need to call this impl a second time
65     // when user requests to get profling info
66     virtual bool get_profiling_info_impl(std::list<instrumentation::profiling_interval>&) { return true; }
67 };
68
69 struct user_event : virtual public event_impl {
70 public:
71     explicit user_event(bool set = false) { _set = set; }
72
73     void set() {
74         if (_set)
75             return;
76         _set = true;
77         set_impl();
78         call_handlers();
79     }
80
81 private:
82     virtual void set_impl() = 0;
83 };
84
85 }  // namespace cldnn