summaryrefslogtreecommitdiff
path: root/inference-engine/thirdparty/ade/common/include/util/callonce.hpp
blob: 6ec574267ab5559011bcf8453e5c606640ee8201 (plain)
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
// Copyright (C) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

#ifndef UTIL_CALL_ONCE_HPP
#define UTIL_CALL_ONCE_HPP

#include <type_traits>
#include <mutex>

namespace util
{
// Special helper for thread-safe call once per graph execution
class CallOnce
{
   using Storage = typename std::aligned_storage<sizeof(std::once_flag), alignof(std::once_flag)>::type;

   Storage  m_flagStorage;

   CallOnce(const CallOnce&)             = delete;
   CallOnce& operator=(const CallOnce&)  = delete;
   CallOnce(const CallOnce&&)            = delete;
   CallOnce& operator=(const CallOnce&&) = delete;

public:

   CallOnce() { reset(); }

   template<typename F> void operator()(F&& f)
   {
      std::call_once(*reinterpret_cast<std::once_flag*>(&m_flagStorage), std::forward<F>(f));
   }

   void reset()
   {
      new (&m_flagStorage) std::once_flag;
   }
};

} // namespace DMIP

#endif // UTIL_CALL_ONCE_HPP