forked from Cambricon/mlu-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.hpp
153 lines (130 loc) · 4.62 KB
/
subscriber.hpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*************************************************************************
* Copyright (C) [2024] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#pragma once
#include <stdint.h>
#include <functional>
#include <list>
#include <map>
#include <utility>
#include <tuple>
#include <unordered_map>
#include <memory>
#include <mutex>
#include <pthread.h>
#include "cnrt.h"
#include "core/macros.h"
// c++17 shared_mutex/unique_mutex alternative, may need to move into separate
// file
template <class LockType, bool exclusive>
inline void Lock(LockType *lck_);
template <class LockType, bool exclusive>
inline void UnLock(LockType *lck_);
template <>
inline void Lock<pthread_rwlock_t, true>(pthread_rwlock_t *lck_) {
pthread_rwlock_wrlock(lck_);
}
template <>
inline void Lock<pthread_rwlock_t, false>(pthread_rwlock_t *lck_) {
pthread_rwlock_rdlock(lck_);
}
template <>
inline void UnLock<pthread_rwlock_t, true>(pthread_rwlock_t *lck_) {
pthread_rwlock_unlock(lck_);
}
template <>
inline void UnLock<pthread_rwlock_t, false>(pthread_rwlock_t *lck_) {
pthread_rwlock_unlock(lck_);
}
template <class LockType, bool exclusive>
class LockGuard {
public:
explicit LockGuard(LockType &lck) : lck_(&lck) {
Lock<LockType, exclusive>(lck_);
}
~LockGuard() { UnLock<LockType, exclusive>(lck_); }
private:
LockType *lck_;
};
using ReadLock = LockGuard<pthread_rwlock_t, false>;
using WriteLock = LockGuard<pthread_rwlock_t, true>;
namespace mluop {
namespace pubsub {
enum EventType : uint32_t {
UNINITIALIZED = 0,
BANG_REGISTER_FUNCTION = 0x1,
CNRT_INVOKE_KERNEL = 0x2,
MLUOP_API = 0x1000, // for all mluOp api
// MLUOP_API + offset is for specific mluOp api
ALL = INT32_MAX,
};
struct ParamBangRegisterFunction {
void **module;
const char *hostFunc;
const char *deviceName;
int *wSize;
};
class Publisher {
public:
using EventHandler =
std::pair<std::function<void(const void *, void *)>, void *>;
static Publisher &instance() {
static Publisher publisher;
return publisher;
}
static void publish(EventType event, const void *params) {
if (MLUOP_PREDICT_FALSE(delete_flag)) return;
// TODO handle event type ALL
const auto &handlers = instance().subscriber_manager_[event];
ReadLock lock(instance().mtx_pubsub_);
for (const auto &handler : handlers) {
handler.second.first(params, handler.second.second);
// handler.first(params, handler.second);
}
}
static size_t subscribe(EventType event,
std::function<void(const void *, void *)> handler,
void *usr);
static void unsubscribe(EventType event, size_t idx);
static void save_internal_subscriber(EventType event, size_t idx);
~Publisher();
private:
explicit Publisher() = default;
Publisher(const Publisher &) = delete;
Publisher &operator=(const Publisher &) = delete;
Publisher(Publisher &&) = delete;
std::unordered_map<EventType, std::map<std::shared_ptr<char>, EventHandler>>
subscriber_manager_{
{EventType::BANG_REGISTER_FUNCTION, {}},
{EventType::CNRT_INVOKE_KERNEL, {}},
{EventType::MLUOP_API, {}},
};
std::map<size_t, std::shared_ptr<char>> ugly_key_store_;
// TODO consider different lock for different event type
pthread_rwlock_t mtx_pubsub_ = PTHREAD_RWLOCK_INITIALIZER;
std::list<std::tuple<EventType, size_t>> internal_subscribers_;
// XXX ugly workaround to avoid ASan's 'Use After Free' when mluOp is called
// by `dlopen`
static bool delete_flag;
};
} // namespace pubsub
} // namespace mluop