FreeTensor
Loading...
Searching...
No Matches
sync_func.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_SYNC_FUNC_H
2#define FREE_TENSOR_SYNC_FUNC_H
3
4#include <functional>
5#include <mutex>
6
7namespace freetensor {
8
9namespace detail {
10
11template <class T> struct TaggedSyncFunc { T f_; };
12template <class T> struct TaggedUnsyncFunc { T f_; };
13
14} // namespace detail
15
16template <typename R, typename... Params> class SyncFunc;
17
21template <typename R, typename... Params> class SyncFunc<R(Params...)> {
22 std::function<R(Params...)> f_;
23 bool synchronized_ = true;
24 mutable std::mutex
25 mutex_; // mutable to allow const callback object to lock against
26
27 public:
29 SyncFunc(std::nullptr_t) {}
30
31 explicit SyncFunc(const std::function<R(Params...)> &f, bool sync)
32 : f_(f), synchronized_(sync) {}
33 explicit SyncFunc(std::function<R(Params...)> &&f, bool sync)
34 : f_(std::move(f)), synchronized_(sync) {}
35
36 template <class T>
37 SyncFunc(const detail::TaggedSyncFunc<T> &f) : SyncFunc(f.f_, true) {}
38 template <class T>
39 SyncFunc(detail::TaggedSyncFunc<T> &&f) : SyncFunc(std::move(f.f_), true) {}
40
41 template <class T>
42 SyncFunc(const detail::TaggedUnsyncFunc<T> &f) : SyncFunc(f.f_, false) {}
43 template <class T>
45 : SyncFunc(std::move(f.f_), false) {}
46
47 SyncFunc(const SyncFunc &other)
48 : f_(other.f_), synchronized_(other.synchronized_), mutex_() {}
49 SyncFunc &operator=(const SyncFunc &other) {
50 f_ = other.f_;
51 synchronized_ = other.synchronized_;
52 return *this;
53 }
54
55 friend bool operator==(const SyncFunc &self, std::nullptr_t) {
56 return self.f_ == nullptr;
57 }
58 friend bool operator==(std::nullptr_t, const SyncFunc &self) {
59 return self.f_ == nullptr;
60 }
61
62 template <typename... Args> R operator()(Args &&...args) const {
63 if (synchronized_) {
64 std::lock_guard lg(mutex_);
65 return f_(std::forward<Args>(args)...);
66 } else {
67 return f_(std::forward<Args>(args)...);
68 }
69 }
70};
71
75template <class T>
77 return {std::forward<T>(f)};
78}
79
83template <class T>
85 return {std::forward<T>(f)};
86}
87
88} // namespace freetensor
89
90#endif // FREE_TENSOR_SYNC_FUNC_H
SyncFunc(detail::TaggedSyncFunc< T > &&f)
Definition: sync_func.h:39
SyncFunc & operator=(const SyncFunc &other)
Definition: sync_func.h:49
SyncFunc(const std::function< R(Params...)> &f, bool sync)
Definition: sync_func.h:31
friend bool operator==(std::nullptr_t, const SyncFunc &self)
Definition: sync_func.h:58
SyncFunc(const detail::TaggedUnsyncFunc< T > &f)
Definition: sync_func.h:42
SyncFunc()
Definition: sync_func.h:28
SyncFunc(std::function< R(Params...)> &&f, bool sync)
Definition: sync_func.h:33
friend bool operator==(const SyncFunc &self, std::nullptr_t)
Definition: sync_func.h:55
SyncFunc(std::nullptr_t)
Definition: sync_func.h:29
SyncFunc(const detail::TaggedSyncFunc< T > &f)
Definition: sync_func.h:37
SyncFunc(detail::TaggedUnsyncFunc< T > &&f)
Definition: sync_func.h:44
SyncFunc(const SyncFunc &other)
Definition: sync_func.h:47
R operator()(Args &&...args) const
Definition: sync_func.h:62
Definition: sync_func.h:16
Definition: allocator.h:9
detail::TaggedSyncFunc< std::remove_reference_t< T > > syncFunc(T &&f)
Definition: sync_func.h:76
detail::TaggedUnsyncFunc< std::remove_reference_t< T > > unsyncFunc(T &&f)
Definition: sync_func.h:84
STL namespace.
Definition: sync_func.h:11
T f_
Definition: sync_func.h:11
Definition: sync_func.h:12
T f_
Definition: sync_func.h:12