parent
d7f8743486
commit
56ece6fa58
@ -0,0 +1,16 @@
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/)
|
||||
|
||||
file(GLOB MINDRT_SRC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/actor/*.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/async/*.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/evloop/*.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/timer/*.cc
|
||||
)
|
||||
|
||||
|
||||
add_library(mindrt_mid OBJECT ${MINDRT_SRC})
|
||||
|
||||
|
@ -0,0 +1,214 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_ACTOR_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_ACTOR_H
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "actor/msg.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
class ActorBase;
|
||||
class ActorMgr;
|
||||
class ActorPolicy;
|
||||
|
||||
using ActorReference = std::shared_ptr<ActorBase>;
|
||||
|
||||
// should be at least greater than 1
|
||||
constexpr uint32_t MAX_ACTOR_RECORD_SIZE = 3;
|
||||
|
||||
class ActorBase {
|
||||
public:
|
||||
inline const AID &GetAID() const { return id; }
|
||||
|
||||
inline void AddMsgRecord(const std::string &msgName) {
|
||||
recordNextPoint++;
|
||||
uint32_t startPoint = recordNextPoint % MAX_ACTOR_RECORD_SIZE;
|
||||
msgRecords[startPoint] = msgName;
|
||||
}
|
||||
|
||||
inline void PrintMsgRecord() {
|
||||
uint32_t startPoint = recordNextPoint % MAX_ACTOR_RECORD_SIZE;
|
||||
for (uint32_t i = 0; i < MAX_ACTOR_RECORD_SIZE; i++) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_INFO, PID_LITEBUS_LOG, "Actor message dumps:%s",
|
||||
"actor:%s,msg:%s", id.Name().c_str(), msgRecords[startPoint].c_str());
|
||||
startPoint = (startPoint + MAX_ACTOR_RECORD_SIZE - 1) % MAX_ACTOR_RECORD_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
explicit ActorBase(const std::string &name);
|
||||
virtual ~ActorBase();
|
||||
|
||||
// send MessageBase message to the actor.
|
||||
int Send(const AID &to, std::unique_ptr<MessageBase> msg);
|
||||
|
||||
// send string message to the actor
|
||||
int Send(const AID &to, std::string &&name, std::string &&msg, bool remoteLink = false,
|
||||
bool isExactNotRemote = false);
|
||||
|
||||
// get output buffer size for flow control
|
||||
uint64_t GetOutBufSize(const AID &to);
|
||||
|
||||
// get input buffer size for flow control
|
||||
uint64_t GetInBufSize(const AID &to);
|
||||
|
||||
// set record send/receive message package size
|
||||
int AddRuleUdp(const std::string &peer, int recordNum);
|
||||
|
||||
// delete the send/receive message package size
|
||||
void DelRuleUdp(const std::string &peer, bool outputLog);
|
||||
|
||||
protected:
|
||||
using ActorFunction = std::function<void(std::unique_ptr<MessageBase> &msg)>;
|
||||
|
||||
// install KMSG handler . This method will be called before the actor start to run.
|
||||
virtual void Init() {}
|
||||
|
||||
// This method will be called before the actor start to terminate.
|
||||
virtual void Finalize() {}
|
||||
|
||||
// KHTTPMsg handler
|
||||
virtual void HandleHttp(std::unique_ptr<MessageBase> msg) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG,
|
||||
"ACTOR (%s) HandleHttp() is not implemented", "a=%s", id.Name().c_str());
|
||||
}
|
||||
|
||||
// KLOCALMsg handler
|
||||
virtual void HandleLocalMsg(std::unique_ptr<MessageBase> msg) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG,
|
||||
"ACTOR (%s) HandleLocalMsg() is not implemented.", "a=%s", id.Name().c_str());
|
||||
}
|
||||
|
||||
// The link is closed.
|
||||
virtual void Exited(const AID &actor) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG,
|
||||
"ACTOR (%s) Exited() is not implemented. ", "a=%s", id.Name().c_str());
|
||||
}
|
||||
|
||||
// Filter the KMSG
|
||||
virtual bool Filter(const std::unique_ptr<MessageBase> &msg) { return false; }
|
||||
|
||||
// register the message handle
|
||||
void Receive(const std::string &msgName, ActorFunction &&func);
|
||||
|
||||
// register the message handle. It will be discarded.
|
||||
template <typename T>
|
||||
void Receive(const std::string &msgName, void (T::*method)(mindspore::AID, std::string &&, std::string &&)) {
|
||||
ActorFunction func = std::bind(&BehaviorBase1<T>, static_cast<T *>(this), method, std::placeholders::_1);
|
||||
Receive(msgName, std::move(func));
|
||||
}
|
||||
|
||||
// register the message handle
|
||||
template <typename T>
|
||||
void Receive(const std::string &msgName, void (T::*method)(const mindspore::AID &, std::string &&, std::string &&)) {
|
||||
ActorFunction func = std::bind(&BehaviorBase<T>, static_cast<T *>(this), method, std::placeholders::_1);
|
||||
Receive(msgName, std::move(func));
|
||||
return;
|
||||
}
|
||||
|
||||
// register the message handle, for kmsg-udp message
|
||||
template <typename T>
|
||||
void ReceiveUdp(const std::string &msgName,
|
||||
void (T::*method)(const mindspore::AID &, std::string &&, std::string &&)) {
|
||||
ActorFunction func = std::bind(&BehaviorBaseForUdp<T>, static_cast<T *>(this), method, std::placeholders::_1);
|
||||
Receive(msgName, std::move(func));
|
||||
return;
|
||||
}
|
||||
|
||||
// Link the remote actor
|
||||
int Link(const AID &to);
|
||||
|
||||
// Unlink the remote actor
|
||||
int UnLink(const AID &to);
|
||||
|
||||
// Reconnect to the remote actor
|
||||
int Reconnect(const AID &to);
|
||||
|
||||
void Terminate();
|
||||
void Await();
|
||||
|
||||
private:
|
||||
friend class ActorMgr;
|
||||
friend class ActorThread;
|
||||
|
||||
// KMSG Msg Handler
|
||||
virtual void HandlekMsg(std::unique_ptr<MessageBase> &msg);
|
||||
|
||||
template <typename T>
|
||||
static void BehaviorBase(T *t, void (T::*method)(const mindspore::AID &, std::string &&, std::string &&),
|
||||
std::unique_ptr<MessageBase> &msg) {
|
||||
if (msg->type != MessageBase::Type::KMSG) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG, "Drop non-tcp message: %s",
|
||||
"from:%s,to:%s,name:%s", std::string(msg->from).c_str(), std::string(msg->to).c_str(),
|
||||
msg->name.c_str());
|
||||
return;
|
||||
}
|
||||
(t->*method)(msg->from, std::move(msg->name), std::move(msg->body));
|
||||
}
|
||||
|
||||
// register the message handle. It will be discarded.
|
||||
template <typename T>
|
||||
static void BehaviorBase1(T *t, void (T::*method)(mindspore::AID, std::string &&, std::string &&),
|
||||
std::unique_ptr<MessageBase> &msg) {
|
||||
if (msg->type != MessageBase::Type::KMSG) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG, "Drop non-tcp message: %s",
|
||||
"from:%s,to:%s,name:%s", std::string(msg->from).c_str(), std::string(msg->to).c_str(),
|
||||
msg->name.c_str());
|
||||
return;
|
||||
}
|
||||
(t->*method)(msg->from, std::move(msg->name), std::move(msg->body));
|
||||
}
|
||||
|
||||
// register the udp message handle. Use this closure function to drop non-udp messages
|
||||
template <typename T>
|
||||
static void BehaviorBaseForUdp(T *t, void (T::*method)(const mindspore::AID &, std::string &&, std::string &&),
|
||||
std::unique_ptr<MessageBase> &msg) {
|
||||
if (msg->type != MessageBase::Type::KUDP) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG, "Drop non-udp message: %s",
|
||||
"from:%s,to:%s,name:%s", std::string(msg->from).c_str(), std::string(msg->to).c_str(),
|
||||
msg->name.c_str());
|
||||
return;
|
||||
}
|
||||
(t->*method)(msg->from, std::move(msg->name), std::move(msg->body));
|
||||
}
|
||||
|
||||
void Run();
|
||||
void Quit();
|
||||
int EnqueMessage(std::unique_ptr<MessageBase> msg);
|
||||
|
||||
void Spawn(std::shared_ptr<ActorBase> &actor, std::unique_ptr<ActorPolicy> actorThread);
|
||||
void SetRunningStatus(bool start);
|
||||
|
||||
std::unique_ptr<ActorPolicy> actorThread;
|
||||
|
||||
AID id;
|
||||
std::map<std::string, ActorFunction> actionFunctions;
|
||||
std::mutex waiterLock;
|
||||
|
||||
std::string msgRecords[MAX_ACTOR_RECORD_SIZE];
|
||||
uint32_t recordNextPoint = 0;
|
||||
};
|
||||
|
||||
}; // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_ACTORAPP_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_ACTORAPP_H
|
||||
|
||||
#include "actor/actor.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
class MessageLocal : public MessageBase {
|
||||
public:
|
||||
MessageLocal(const AID &from, const AID &to, const std::string &name, void *aPtr)
|
||||
: MessageBase(from, to, name, "LocalMsg", Type::KLOCAL), ptr(aPtr) {}
|
||||
~MessageLocal() {}
|
||||
void *ptr;
|
||||
};
|
||||
|
||||
class AppActor : public ActorBase {
|
||||
public:
|
||||
typedef std::function<void(std::unique_ptr<MessageBase>)> APPBehavior;
|
||||
|
||||
AppActor(const std::string &name) : ActorBase(name) {}
|
||||
~AppActor() {}
|
||||
|
||||
inline int Send(const AID &to, std::unique_ptr<MessageBase> msg) { return ActorBase::Send(to, std::move(msg)); }
|
||||
// send T message to the actor
|
||||
template <typename M>
|
||||
int Send(const std::string &to, const std::string &msgName, std::unique_ptr<M> msg) {
|
||||
std::unique_ptr<MessageLocal> localMsg(new (std::nothrow) MessageLocal(GetAID(), to, msgName, msg.release()));
|
||||
BUS_OOM_EXIT(localMsg);
|
||||
return Send(to, std::move(localMsg));
|
||||
}
|
||||
|
||||
// register the message handle
|
||||
template <typename T, typename M>
|
||||
void Receive(const std::string &msgName, void (T::*method)(const AID &, std::unique_ptr<M>)) {
|
||||
APPBehavior behavior = std::bind(&BehaviorBase<T, M>, static_cast<T *>(this), method, std::placeholders::_1);
|
||||
|
||||
if (appBehaviors.find(msgName) != appBehaviors.end()) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG, "ACTOR msgName conflict:%s",
|
||||
"a=%s,msg=%s", GetAID().Name().c_str(), msgName.c_str());
|
||||
BUS_EXIT("msgName conflicts.");
|
||||
return;
|
||||
}
|
||||
|
||||
appBehaviors.emplace(msgName, behavior);
|
||||
return;
|
||||
}
|
||||
|
||||
template <typename T, typename M>
|
||||
static void BehaviorBase(T *t, void (T::*method)(const AID &, std::unique_ptr<M>), std::unique_ptr<MessageBase> msg) {
|
||||
(t->*method)(msg->From(), std::move(std::unique_ptr<M>((M *)static_cast<MessageLocal *>(msg.get())->ptr)));
|
||||
return;
|
||||
}
|
||||
|
||||
protected:
|
||||
// KLOCALMsg handler
|
||||
virtual void HandleLocalMsg(std::unique_ptr<MessageBase> msg) {
|
||||
auto it = appBehaviors.find(msg->Name());
|
||||
if (it != appBehaviors.end()) {
|
||||
it->second(std::move(msg));
|
||||
} else {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG, "ACTOR can not finds handler:%s",
|
||||
"a=%s,msg=%s,hdlno=%zd", GetAID().Name().c_str(), msg->Name().c_str(), appBehaviors.size());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, APPBehavior> appBehaviors;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_AID_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_AID_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "actor/buslog.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
constexpr auto BUS_TCP = "tcp";
|
||||
constexpr auto BUS_UDP = "udp";
|
||||
|
||||
class AID {
|
||||
public:
|
||||
AID() : name(), url() {}
|
||||
|
||||
~AID() {}
|
||||
|
||||
AID(const char *name);
|
||||
AID(const std::string &name);
|
||||
|
||||
AID(const std::string &tmpName, const std::string &sUrl) : name(tmpName), url(sUrl) { SetUnfixUrl(); }
|
||||
|
||||
AID(const AID &id) : name(id.name), url(id.url) { SetUnfixUrl(); }
|
||||
|
||||
// Overloading of Assignment Operator
|
||||
AID &operator=(const AID &id);
|
||||
|
||||
inline void SetUrl(const std::string &tmpUrl) {
|
||||
url = tmpUrl;
|
||||
SetUnfixUrl();
|
||||
}
|
||||
|
||||
inline void SetName(const std::string &tmpName) { name = tmpName; }
|
||||
|
||||
inline const std::string &Name() const { return name; }
|
||||
|
||||
inline const std::string &Url() const { return url; }
|
||||
|
||||
void SetProtocol(const std::string &protocol);
|
||||
bool OK() const;
|
||||
|
||||
std::string GetProtocol() const;
|
||||
std::string GetIp() const;
|
||||
uint16_t GetPort() const;
|
||||
inline std::string UnfixUrl() const { return GetIp() + ":" + std::to_string(GetPort()); }
|
||||
inline operator std::string() const { return name + "@" + url; }
|
||||
|
||||
inline std::string HashString() const { return name + "@" + UnfixUrl(); }
|
||||
|
||||
private:
|
||||
void SetUnfixUrl();
|
||||
|
||||
friend class Actor;
|
||||
|
||||
// actor's name
|
||||
std::string name;
|
||||
|
||||
/**
|
||||
tcp://ip:port
|
||||
udp://ip:port
|
||||
ip:port (tcp)
|
||||
**/
|
||||
std::string url;
|
||||
};
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &os, const AID &aid) {
|
||||
os << aid.Name() << "@" << aid.Url();
|
||||
return os;
|
||||
}
|
||||
|
||||
inline bool operator==(const AID &aid1, const AID &aid2) {
|
||||
if (aid1.GetProtocol() == BUS_TCP && aid2.GetProtocol() == BUS_TCP) {
|
||||
// NOTE : By default, http has no protocol filed, so we use 'UnfixUrl' to compare aids here
|
||||
return ((aid1.Name() == aid2.Name()) && (aid1.UnfixUrl() == aid2.UnfixUrl()));
|
||||
} else {
|
||||
return ((aid1.Name() == aid2.Name()) && (aid1.Url() == aid2.Url()));
|
||||
}
|
||||
}
|
||||
inline bool operator!=(const AID &aid1, const AID &aid2) { return !(aid1 == aid2); }
|
||||
|
||||
inline bool operator>(const AID &aid1, const AID &aid2) { return aid1.HashString() > aid2.HashString(); }
|
||||
inline bool operator<(const AID &aid1, const AID &aid2) { return aid1.HashString() < aid2.HashString(); }
|
||||
|
||||
}; // namespace mindspore
|
||||
|
||||
// custom specialization of std::hash can be injected in namespace std
|
||||
namespace std {
|
||||
template <>
|
||||
struct hash<mindspore::AID> {
|
||||
typedef mindspore::AID argument_type;
|
||||
typedef std::size_t result_type;
|
||||
result_type operator()(argument_type const &s) const noexcept { return (std::hash<std::string>{}(s.HashString())); }
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
#endif
|
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_BUSERRCODE_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_BUSERRCODE_H
|
||||
|
||||
// common err code -1 ~ -100
|
||||
constexpr int BUS_ERROR = -1;
|
||||
constexpr int BUS_OK = 0;
|
||||
constexpr int COMM_NULL_PTR = -1;
|
||||
constexpr int ERRORCODE_SUCCESS = 1;
|
||||
|
||||
// actor module err code -101 ~ -200
|
||||
constexpr int ACTOR_PARAMER_ERR = -101;
|
||||
constexpr int ACTOR_NOT_FIND = -102;
|
||||
constexpr int IO_NOT_FIND = -103;
|
||||
|
||||
// TCP module err code -301 ~ -400
|
||||
// Null
|
||||
// UDP IO err code -401 ~ -500
|
||||
constexpr int UDP_MSG_TOO_BIG = -401;
|
||||
constexpr int UDP_MSG_WRITE_ERR = -402;
|
||||
constexpr int UDP_MSG_SEND_ERR = -403;
|
||||
constexpr int UDP_MSG_ADDR_ERR = -404;
|
||||
constexpr int UDP_MSG_SEND_SUCCESS = 1;
|
||||
|
||||
// Protocol module err code -501 ~ -600
|
||||
constexpr int PB_MSG_NO_NAME = -501;
|
||||
|
||||
#endif
|
@ -0,0 +1,149 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_BUSLOG_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_BUSLOG_H
|
||||
|
||||
#include <signal.h>
|
||||
#include <iostream>
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
|
||||
#include "actor/buserrcode.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
#define BUS_LOG(severity) // LOG(severity)
|
||||
#define BUS_DLOG(verboselevel) // VLOG(verboselevel)
|
||||
|
||||
#define HARES_LOG_PID int // GetLogPID();
|
||||
#define PID_LITEBUS_LOG
|
||||
|
||||
#define ICTSBASE_LOG_COMMON_CODE
|
||||
#define HLOG_LEVEL_INFO
|
||||
#define PID_LITEBUS_LOG
|
||||
//#define BUS_OOM_EXIT
|
||||
#define HLOG_LEVEL_DEBUG 1
|
||||
#define ICTSBASE_LOG0(logig, level, pid, format)
|
||||
#define ICTSBASE_LOG1(logig, level, pid, format, para)
|
||||
#define ICTSBASE_LOG2(logig, level, pid, format, para1, para2)
|
||||
#define ICTSBASE_LOG3(logig, level, pid, format, para1, para2, para3)
|
||||
#define ICTSBASE_LOG4(logig, level, pid, format, para1, para2, para3, para4)
|
||||
#define ICTSBASE_LOG_STRING(logig, level, pid, preformat, format...)
|
||||
#define FlushHLogCache()
|
||||
// Kill the process for safe exiting.
|
||||
inline void KillProcess(const std::string &ret) {
|
||||
ICTSBASE_LOG_STRING(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_INFO, PID_LITEBUS_LOG, "BUS Exit Tip: %s", "%s",
|
||||
ret.c_str());
|
||||
// flush the log in cache to disk before exiting.
|
||||
FlushHLogCache();
|
||||
}
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
constexpr int DLEVEL4 = 1000;
|
||||
constexpr int DLEVEL3 = 3;
|
||||
constexpr int DLEVEL2 = 2;
|
||||
constexpr int DLEVEL1 = 1;
|
||||
constexpr int DLEVEL0 = 0;
|
||||
|
||||
#define BUS_ASSERT(expression) \
|
||||
do { \
|
||||
if (!(expression)) { \
|
||||
std::stringstream ss; \
|
||||
ss << "Assertion failed: " << #expression << ", file: " << __FILE__ << ", line: " << __LINE__; \
|
||||
mindspore::KillProcess(ss.str()); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define BUS_EXIT(ret) \
|
||||
do { \
|
||||
std::stringstream ss; \
|
||||
ss << (ret) << " ( file: " << __FILE__ << ", line: " << __LINE__ << " )."; \
|
||||
mindspore::KillProcess(ss.str()); \
|
||||
} while (0)
|
||||
|
||||
#define BUS_OOM_EXIT(ptr) \
|
||||
{ \
|
||||
if (ptr == nullptr) { \
|
||||
ICTSBASE_LOG0(ICTSBASE_LOG_COMMON_CODE, HLOG_LEVEL_ERROR, PID_LITEBUS_LOG, "new failed, will exit"); \
|
||||
BUS_EXIT("Exit for OOM."); \
|
||||
} \
|
||||
}
|
||||
|
||||
constexpr int LOG_CHECK_EVERY_FIRSTNUM = 10;
|
||||
constexpr int LOG_CHECK_EVERY_NUM1 = 10;
|
||||
constexpr int LOG_CHECK_EVERY_NUM2 = 100;
|
||||
constexpr int LOG_CHECK_EVERY_NUM3 = 1000;
|
||||
constexpr int LOG_CHECK_EVERY_NUM4 = 10000;
|
||||
|
||||
#define LOG_CHECK_ID_CONCAT(word1, word2) word1##word2
|
||||
|
||||
#define LOG_CHECK_ID LOG_CHECK_ID_CONCAT(__FUNCTION__, __LINE__)
|
||||
|
||||
#define LOG_CHECK_FIRST_N \
|
||||
[](uint32_t firstNum) { \
|
||||
static uint32_t LOG_CHECK_ID = 0; \
|
||||
++LOG_CHECK_ID; \
|
||||
return (LOG_CHECK_ID <= firstNum); \
|
||||
}
|
||||
|
||||
#define LOG_CHECK_EVERY_N1 \
|
||||
[](uint32_t firstNum, uint32_t num) { \
|
||||
static uint32_t LOG_CHECK_ID = 0; \
|
||||
++LOG_CHECK_ID; \
|
||||
return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID % num == 0)); \
|
||||
}
|
||||
|
||||
#define LOG_CHECK_EVERY_N2 \
|
||||
[](uint32_t firstNum, uint32_t num1, uint32_t num2) { \
|
||||
static uint32_t LOG_CHECK_ID = 0; \
|
||||
++LOG_CHECK_ID; \
|
||||
return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
|
||||
(LOG_CHECK_ID % num2 == 0)); \
|
||||
}
|
||||
|
||||
#define LOG_CHECK_EVERY_N3 \
|
||||
[](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3) { \
|
||||
static uint32_t LOG_CHECK_ID = 0; \
|
||||
++LOG_CHECK_ID; \
|
||||
return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
|
||||
(LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID % num3 == 0)); \
|
||||
}
|
||||
|
||||
#define LOG_CHECK_EVERY_N4 \
|
||||
[](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4) { \
|
||||
static uint32_t LOG_CHECK_ID = 0; \
|
||||
++LOG_CHECK_ID; \
|
||||
return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
|
||||
(LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID < num4 && LOG_CHECK_ID % num3 == 0) || \
|
||||
(LOG_CHECK_ID % num4 == 0)); \
|
||||
}
|
||||
|
||||
#define LOG_CHECK_EVERY_N \
|
||||
[]() { \
|
||||
static uint32_t LOG_CHECK_ID = 0; \
|
||||
++LOG_CHECK_ID; \
|
||||
return ((LOG_CHECK_ID <= LOG_CHECK_EVERY_FIRSTNUM) || \
|
||||
(LOG_CHECK_ID < LOG_CHECK_EVERY_NUM2 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM1 == 0) || \
|
||||
(LOG_CHECK_ID < LOG_CHECK_EVERY_NUM3 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM2 == 0) || \
|
||||
(LOG_CHECK_ID < LOG_CHECK_EVERY_NUM4 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM3 == 0) || \
|
||||
(LOG_CHECK_ID % LOG_CHECK_EVERY_NUM4 == 0)); \
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_MSG_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_MSG_H
|
||||
|
||||
#include "actor/aid.h"
|
||||
|
||||
namespace mindspore {
|
||||
class ActorBase;
|
||||
class MessageBase {
|
||||
public:
|
||||
enum class Type : char {
|
||||
KMSG = 1,
|
||||
KUDP,
|
||||
KHTTP,
|
||||
KASYNC,
|
||||
KLOCAL,
|
||||
KEXIT,
|
||||
KTERMINATE,
|
||||
};
|
||||
|
||||
MessageBase(Type eType = Type::KMSG) : from(), name(), type(eType) {}
|
||||
|
||||
explicit MessageBase(const std::string &sName, Type eType = Type::KMSG) : from(), name(sName), type(eType) {}
|
||||
|
||||
explicit MessageBase(const AID &aFrom, const AID &aTo, Type eType = Type::KMSG)
|
||||
: from(aFrom), to(aTo), name(), body(), type(eType) {}
|
||||
|
||||
explicit MessageBase(const AID &aFrom, const AID &aTo, const std::string &sName, Type eType = Type::KMSG)
|
||||
: from(aFrom), to(aTo), name(sName), body(), type(eType) {}
|
||||
|
||||
explicit MessageBase(const AID &aFrom, const AID &aTo, const std::string &sName, std::string &&sBody,
|
||||
Type eType = Type::KMSG)
|
||||
: from(aFrom), to(aTo), name(sName), body(std::move(sBody)), type(eType) {}
|
||||
|
||||
virtual ~MessageBase() {}
|
||||
|
||||
inline std::string &Name() { return name; }
|
||||
|
||||
inline void SetName(const std::string &aName) { this->name = aName; }
|
||||
|
||||
inline AID &From() { return from; }
|
||||
|
||||
inline std::string &Body() { return body; }
|
||||
|
||||
inline void SetFrom(const AID &aFrom) { from = aFrom; }
|
||||
|
||||
inline AID &To() { return to; }
|
||||
|
||||
inline void SetTo(const AID &aTo) { to = aTo; }
|
||||
|
||||
inline const Type GetType() const { return type; }
|
||||
|
||||
inline void SetType(Type eType) { type = eType; }
|
||||
|
||||
virtual void Run(ActorBase *actor) {}
|
||||
|
||||
friend class ActorBase;
|
||||
friend class TCPMgr;
|
||||
AID from;
|
||||
AID to;
|
||||
std::string name;
|
||||
std::string body;
|
||||
Type type;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // __LITEBUS_MESSAGE_HPP__
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_NAUGHT_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_NAUGHT_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
class Naught;
|
||||
class ActorBase;
|
||||
|
||||
typedef std::shared_ptr<Naught> UniqueNaught;
|
||||
typedef std::shared_ptr<Naught> SharedNaught;
|
||||
typedef std::string BusString;
|
||||
|
||||
// Lite , start from Naught
|
||||
class Naught {
|
||||
public:
|
||||
virtual ~Naught() {}
|
||||
};
|
||||
|
||||
}; // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_APPLY_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_APPLY_H
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
template <typename T, T... Ints>
|
||||
struct IntegerSequenceBase {
|
||||
static constexpr std::size_t Size() noexcept { return sizeof...(Ints); }
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
|
||||
template <typename T, std::size_t N, std::size_t... Ints>
|
||||
struct IntegerSequence : public IntegerSequence<T, N - 1, N - 1, Ints...> {};
|
||||
|
||||
template <typename T, std::size_t... Ints>
|
||||
struct IntegerSequence<T, 0, Ints...> {
|
||||
using type = IntegerSequenceBase<T, Ints...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
using make_integer_sequence = typename internal::IntegerSequence<T, N>::type;
|
||||
|
||||
template <std::size_t... Ints>
|
||||
using index_sequence = IntegerSequenceBase<std::size_t, Ints...>;
|
||||
|
||||
template <std::size_t N>
|
||||
using make_index_sequence = make_integer_sequence<std::size_t, N>;
|
||||
|
||||
template <class... T>
|
||||
using index_sequence_for = make_index_sequence<sizeof...(T)>;
|
||||
|
||||
template <typename Func, typename Tuple, std::size_t... Ints>
|
||||
auto ApplyHelper(Func &&func, Tuple &&tuple, index_sequence<Ints...>)
|
||||
-> decltype(func(std::get<Ints>(std::forward<Tuple>(tuple))...)) {
|
||||
return func(std::get<Ints>(std::forward<Tuple>(tuple))...);
|
||||
}
|
||||
|
||||
template <typename T, typename Func, typename Tuple, std::size_t... Ints>
|
||||
auto ApplyHelper(T *ptr, Func &&func, Tuple &&tuple, index_sequence<Ints...>)
|
||||
-> decltype((ptr->*func)(std::get<Ints>(std::forward<Tuple>(tuple))...)) {
|
||||
return (ptr->*func)(std::get<Ints>(std::forward<Tuple>(tuple))...);
|
||||
}
|
||||
|
||||
template <typename Func, typename Tuple>
|
||||
auto Apply(Func &&func, Tuple &&tuple)
|
||||
-> decltype(ApplyHelper(std::forward<Func>(func), std::forward<Tuple>(tuple),
|
||||
make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{})) {
|
||||
return ApplyHelper(std::forward<Func>(func), std::forward<Tuple>(tuple),
|
||||
make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{});
|
||||
}
|
||||
|
||||
template <typename T, typename Func, typename Tuple>
|
||||
auto Apply(T *ptr, Func &&func, Tuple &&tuple)
|
||||
-> decltype(ApplyHelper(ptr, std::forward<Func>(func), std::forward<Tuple>(tuple),
|
||||
make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{})) {
|
||||
return ApplyHelper(ptr, std::forward<Func>(func), std::forward<Tuple>(tuple),
|
||||
make_index_sequence<std::tuple_size<typename std::decay<Tuple>::type>::value>{});
|
||||
}
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_ASYNCAFTER_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_ASYNCAFTER_H
|
||||
|
||||
#include "async/async.h"
|
||||
|
||||
#include "timer/timertools.h"
|
||||
|
||||
constexpr mindspore::Duration MILLISECONDS = 1;
|
||||
constexpr mindspore::Duration SECONDS = 1000;
|
||||
|
||||
namespace mindspore {
|
||||
template <typename T>
|
||||
Timer AsyncAfter(const Duration &duration, const AID &aid, void (T::*method)()) {
|
||||
return TimerTools::AddTimer(duration, aid, [=]() { Async(aid, method); });
|
||||
}
|
||||
|
||||
template <typename T, typename Arg0, typename Arg1>
|
||||
Timer AsyncAfter(const Duration &duration, const AID &aid, void (T::*method)(Arg0), Arg1 &&arg) {
|
||||
return TimerTools::AddTimer(duration, aid, [=]() { Async(aid, method, arg); });
|
||||
}
|
||||
|
||||
template <typename T, typename... Args0, typename... Args1>
|
||||
Timer AsyncAfter(const Duration &duration, const AID &aid, void (T::*method)(Args0...), Args1 &&... args) {
|
||||
std::function<void(Args0...)> f([=](Args0... args0) { Async(aid, method, args0...); });
|
||||
|
||||
auto handler = std::bind(f, args...);
|
||||
|
||||
return TimerTools::AddTimer(duration, aid, [=]() { Async(aid, std::move(handler)); });
|
||||
}
|
||||
|
||||
}; // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_COLLECT_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_COLLECT_H
|
||||
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
#include "async/common.h"
|
||||
#include "async/future.h"
|
||||
#include "async/spinlock.h"
|
||||
|
||||
#include "actor/actor.h"
|
||||
|
||||
#include "litebus.hpp"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
template <typename T>
|
||||
class Future;
|
||||
|
||||
template <typename T>
|
||||
class Promise;
|
||||
|
||||
template <typename T>
|
||||
class Collected;
|
||||
|
||||
template <typename T>
|
||||
class Collected {
|
||||
public:
|
||||
Collected(const std::list<Future<T>> &f, Promise<std::list<T>> *p) : futures(f), promise(p), ready(0) {}
|
||||
|
||||
virtual ~Collected() {
|
||||
delete promise;
|
||||
promise = nullptr;
|
||||
}
|
||||
|
||||
Collected(const Collected &) = delete;
|
||||
Collected(Collected &&) = default;
|
||||
|
||||
Collected &operator=(const Collected &) = delete;
|
||||
Collected &operator=(Collected &&) = default;
|
||||
|
||||
public:
|
||||
void Discarded() {
|
||||
auto iter = futures.begin();
|
||||
for (; iter != futures.end(); ++iter) {
|
||||
iter->SetFailed(Status::KERROR);
|
||||
}
|
||||
}
|
||||
|
||||
void Waited(const Future<T> &future) {
|
||||
if (future.IsError()) {
|
||||
promise->SetFailed(future.GetErrorCode());
|
||||
} else if (future.IsOK()) {
|
||||
ready.fetch_add(1);
|
||||
if (ready.load() == futures.size()) {
|
||||
std::list<T> values;
|
||||
auto iter = futures.begin();
|
||||
for (; iter != futures.end(); ++iter) {
|
||||
values.push_back(iter->Get());
|
||||
}
|
||||
promise->SetValue(values);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const std::list<Future<T>> futures;
|
||||
Promise<std::list<T>> *promise;
|
||||
std::atomic_ulong ready;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline Future<std::list<T>> Collect(const std::list<Future<T>> &futures) {
|
||||
if (futures.empty()) {
|
||||
return std::list<T>();
|
||||
}
|
||||
|
||||
Promise<std::list<T>> *promise = new (std::nothrow) Promise<std::list<T>>();
|
||||
BUS_OOM_EXIT(promise);
|
||||
using CollectType = Collected<T>;
|
||||
std::shared_ptr<CollectType> collect = std::make_shared<CollectType>(futures, promise);
|
||||
|
||||
//
|
||||
auto iter = futures.begin();
|
||||
for (; iter != futures.end(); ++iter) {
|
||||
iter->OnComplete(Defer(collect, &CollectType::Waited, std::placeholders::_1));
|
||||
}
|
||||
|
||||
Future<std::list<T>> future = promise->GetFuture();
|
||||
future.OnComplete(Defer(collect, &Collected<T>::Discarded));
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Future<std::tuple<Ts...>> Collect(const Future<Ts> &... futures) {
|
||||
std::list<Future<Nothing>> wrappers = {futures.Then([]() { return Nothing(); })...};
|
||||
|
||||
auto f = [](const Future<Ts> &... futures) { return std::make_tuple(futures.Get()...); };
|
||||
|
||||
return Collect(wrappers).Then(std::bind(f, futures...));
|
||||
}
|
||||
|
||||
}; // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_COMMON_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_COMMON_H
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
struct Nothing {};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif /* COMMON_HPP__ */
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_FAILURE_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_FAILURE_H
|
||||
|
||||
#include "async/status.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
class Failure : public Status {
|
||||
public:
|
||||
Failure() : Status(Status::KOK), errorCode(Status::KOK) {}
|
||||
|
||||
Failure(int32_t code) : Status(code), errorCode(code) {}
|
||||
|
||||
~Failure() {}
|
||||
|
||||
const int32_t GetErrorCode() const { return errorCode; }
|
||||
|
||||
private:
|
||||
Status::Code errorCode;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif /* __FAILURE_HPP__ */
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,203 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_FUTURE_BASE_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_FUTURE_BASE_H
|
||||
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
#include "actor/actor.h"
|
||||
#include "actor/buslog.h"
|
||||
#include "async/spinlock.h"
|
||||
#include "async/status.h"
|
||||
|
||||
#include "timer/timertools.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
template <typename T>
|
||||
class Future;
|
||||
|
||||
template <typename T>
|
||||
class Promise;
|
||||
|
||||
class LessFuture {
|
||||
public:
|
||||
LessFuture() {}
|
||||
LessFuture(const LessFuture &obj) {}
|
||||
LessFuture &operator=(const LessFuture &) = delete;
|
||||
virtual ~LessFuture() {}
|
||||
};
|
||||
|
||||
class FutureBase : public LessFuture {
|
||||
public:
|
||||
FutureBase() {}
|
||||
FutureBase(const FutureBase &obj) : LessFuture(obj) {}
|
||||
FutureBase &operator=(const FutureBase &) = delete;
|
||||
~FutureBase() override {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct FutureData {
|
||||
public:
|
||||
typedef std::function<void(const Future<T> &)> CompleteCallback;
|
||||
typedef std::function<void(const Future<T> &)> AbandonedCallback;
|
||||
|
||||
FutureData()
|
||||
: status(Status::KINIT),
|
||||
associated(false),
|
||||
abandoned(false),
|
||||
gotten(false),
|
||||
promise(),
|
||||
future(promise.get_future()),
|
||||
t() {}
|
||||
|
||||
~FutureData() {
|
||||
// try {
|
||||
Clear();
|
||||
// } catch (...) {
|
||||
// }
|
||||
}
|
||||
|
||||
// remove all callbacks
|
||||
void Clear() {
|
||||
onCompleteCallbacks.clear();
|
||||
onAbandonedCallbacks.clear();
|
||||
}
|
||||
|
||||
// status of future
|
||||
SpinLock lock;
|
||||
Status status;
|
||||
|
||||
bool associated;
|
||||
bool abandoned;
|
||||
bool gotten;
|
||||
|
||||
std::promise<T> promise;
|
||||
|
||||
// get from promise
|
||||
std::future<T> future;
|
||||
|
||||
// complete callback
|
||||
std::list<CompleteCallback> onCompleteCallbacks;
|
||||
|
||||
// abandoned callback
|
||||
std::list<AbandonedCallback> onAbandonedCallbacks;
|
||||
|
||||
T t;
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
|
||||
const std::string WAIT_ACTOR_NAME = "WACTOR_";
|
||||
|
||||
class WaitActor : public ActorBase {
|
||||
public:
|
||||
explicit WaitActor(const std::string &name) : mindspore::ActorBase(name) {}
|
||||
|
||||
~WaitActor() override {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class DeferredHelper;
|
||||
|
||||
template <typename T>
|
||||
struct Wrap {
|
||||
typedef Future<T> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Wrap<Future<T>> {
|
||||
typedef Future<T> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Unwrap {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Unwrap<Future<T>> {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct IsFuture : public std::integral_constant<bool, std::is_base_of<FutureBase, T>::value> {};
|
||||
|
||||
template <typename H, typename... Args>
|
||||
static void Run(std::list<H> &&handlers, Args &&... args) {
|
||||
for (auto iter = handlers.begin(); iter != handlers.end(); ++iter) {
|
||||
std::move (*iter)(std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void Complete(const Future<T> &future, const Future<T> &f) {
|
||||
if (f.IsError()) {
|
||||
future.SetFailed(f.GetErrorCode());
|
||||
} else if (f.IsOK()) {
|
||||
future.SetValue(f.Get());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void Abandon(const Future<T> &future, bool abandon) {
|
||||
future.Abandon(abandon);
|
||||
}
|
||||
|
||||
template <typename T, typename R>
|
||||
static void Thenf(const std::function<Future<R>(const T &)> &function, const std::shared_ptr<Promise<R>> &promise,
|
||||
const Future<T> &f) {
|
||||
if (f.IsError()) {
|
||||
promise->SetFailed(f.GetErrorCode());
|
||||
} else if (f.IsOK()) {
|
||||
promise->Associate(function(f.Get()));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename R>
|
||||
static void Then(const std::function<R(const T &)> &function, const std::shared_ptr<Promise<R>> &promise,
|
||||
const Future<T> &f) {
|
||||
if (f.IsError()) {
|
||||
promise->SetFailed(f.GetErrorCode());
|
||||
} else if (f.IsOK()) {
|
||||
promise->SetValue(function(f.Get()));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void Afterf(const std::function<Future<T>(const Future<T> &)> &f, const std::shared_ptr<Promise<T>> &promise,
|
||||
const Future<T> &future) {
|
||||
promise->Associate(f(future));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void After(const std::shared_ptr<Promise<T>> &promise, const mindspore::Timer &timer, const Future<T> &future) {
|
||||
(void)mindspore::TimerTools::Cancel(timer);
|
||||
promise->Associate(future);
|
||||
}
|
||||
|
||||
void Waitf(const AID &aid);
|
||||
|
||||
void Wait(const AID &aid, const mindspore::Timer &timer);
|
||||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_OPTION_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_OPTION_H
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "actor/buslog.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
template <typename T>
|
||||
struct InnerSome {
|
||||
InnerSome(const T &t) : _t(std::move(t)) {}
|
||||
T _t;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
InnerSome<typename std::decay<T>::type> Some(T &&t) {
|
||||
return InnerSome<typename std::decay<T>::type>(std::forward<T>(t));
|
||||
}
|
||||
|
||||
struct None {};
|
||||
|
||||
template <typename T>
|
||||
class Option {
|
||||
public:
|
||||
Option() : data(), state(NONE) {}
|
||||
|
||||
Option(const T &t) : data(t), state(SOME) {}
|
||||
|
||||
Option(T &&t) : data(std::move(t)), state(SOME) {}
|
||||
|
||||
Option(const InnerSome<T> &some) : data(some._t), state(SOME) {}
|
||||
|
||||
Option(const None &none) : data(), state(NONE) {}
|
||||
|
||||
Option(const Option<T> &that) : data(), state(that.state) {
|
||||
if (that.IsSome()) {
|
||||
data = that.data;
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~Option() {}
|
||||
|
||||
bool IsNone() const { return state == NONE; }
|
||||
|
||||
bool IsSome() const { return state == SOME; }
|
||||
|
||||
const T &Get() const & {
|
||||
BUS_ASSERT(IsSome());
|
||||
return data;
|
||||
}
|
||||
|
||||
T &&Get() && {
|
||||
BUS_ASSERT(IsSome());
|
||||
return std::move(data);
|
||||
}
|
||||
|
||||
const T &&Get() const && {
|
||||
BUS_ASSERT(IsSome());
|
||||
return std::move(data);
|
||||
}
|
||||
|
||||
// oprerator override
|
||||
Option<T> &operator=(const Option<T> &that) {
|
||||
if (&that != this) {
|
||||
state = that.state;
|
||||
if (that.IsSome()) {
|
||||
data = that.data;
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const Option<T> &that) const {
|
||||
return (IsNone() && that.IsNone()) || (IsSome() && that.IsSome() && data == that.data);
|
||||
}
|
||||
|
||||
bool operator!=(const Option<T> &that) const { return !(*this == that); }
|
||||
|
||||
bool operator==(const T &that) const { return IsSome() && data == that; }
|
||||
|
||||
bool operator!=(const T &that) const { return !(*this == that); }
|
||||
|
||||
private:
|
||||
enum State { NONE = 0, SOME = 1 };
|
||||
|
||||
T data;
|
||||
State state;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_RESULT_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_RESULT_H
|
||||
|
||||
#include <tuple>
|
||||
|
||||
#include "option.h"
|
||||
#include "status.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
template <typename... Types>
|
||||
class Result {
|
||||
public:
|
||||
Result() : status(Status::KINIT) {}
|
||||
|
||||
Result(Types... types, const Status &s) : tuple(Option<Types>(types)...), status(s) {}
|
||||
|
||||
~Result() {}
|
||||
|
||||
template <std::size_t I>
|
||||
bool IsSome() {
|
||||
return (std::get<I>(tuple)).IsSome();
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
bool IsNone() {
|
||||
return std::get<I>(tuple).IsNone();
|
||||
}
|
||||
|
||||
bool IsOK() { return status.IsOK(); }
|
||||
|
||||
bool IsError() { return status.IsError(); }
|
||||
|
||||
void SetStatus(Status::Code code) { status.SetCode(code); }
|
||||
|
||||
const Status &GetStatus() const { return status; }
|
||||
|
||||
template <std::size_t I>
|
||||
typename std::tuple_element<I, std::tuple<Option<Types>...>>::type Get() const {
|
||||
return GetOption<I>().Get();
|
||||
}
|
||||
|
||||
private:
|
||||
template <std::size_t I>
|
||||
typename std::tuple_element<I, std::tuple<Option<Types>...>>::type GetOption() const {
|
||||
return std::get<I>(tuple);
|
||||
}
|
||||
|
||||
private:
|
||||
std::tuple<Option<Types>...> tuple;
|
||||
Status status;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_SPINLOCK_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_SPINLOCK_H
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
class SpinLock {
|
||||
public:
|
||||
void Lock() {
|
||||
while (locked.test_and_set(std::memory_order_acquire))
|
||||
;
|
||||
}
|
||||
|
||||
void Unlock() { locked.clear(std::memory_order_release); }
|
||||
|
||||
private:
|
||||
std::atomic_flag locked = ATOMIC_FLAG_INIT;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_STATUS_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_STATUS_H
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
class Status {
|
||||
public:
|
||||
typedef int32_t Code;
|
||||
|
||||
static const Code KINIT = 1;
|
||||
static const Code KOK = 0;
|
||||
static const Code KERROR = -1;
|
||||
|
||||
// Create a success status.
|
||||
Status(int32_t c) : code(c) {}
|
||||
|
||||
Status() : code(KINIT) {}
|
||||
|
||||
virtual ~Status() {}
|
||||
|
||||
// Returns true iff the status indicates success.
|
||||
bool IsInit() const { return (code == KINIT); }
|
||||
|
||||
bool IsOK() const { return (code == KOK); }
|
||||
|
||||
bool IsError() const { return (code != KINIT && code != KOK); }
|
||||
|
||||
// Return a success status.
|
||||
Status OK() const { return Status(KOK); }
|
||||
|
||||
Status Error() const { return Status(KERROR); }
|
||||
|
||||
void SetError() {
|
||||
code = KERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
void SetOK() {
|
||||
code = KOK;
|
||||
return;
|
||||
}
|
||||
|
||||
Code GetCode() const { return code; }
|
||||
|
||||
void SetCode(Code c) { code = c; }
|
||||
|
||||
private:
|
||||
Code code;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_TRY_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_TRY_H
|
||||
|
||||
#include "async/failure.h"
|
||||
#include "async/option.h"
|
||||
#include "async/status.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
template <typename T, typename F = Failure>
|
||||
class Try {
|
||||
public:
|
||||
Try() : errorCode(Status::KOK) {}
|
||||
|
||||
Try(const T &t) {
|
||||
data = Some(t);
|
||||
errorCode = Status::KOK;
|
||||
}
|
||||
|
||||
Try(const F &errCode) { errorCode = errCode; }
|
||||
|
||||
virtual ~Try() {}
|
||||
|
||||
bool IsOK() { return !IsError(); }
|
||||
|
||||
bool IsError() { return data.IsNone(); }
|
||||
|
||||
const T &Get() const { return data.Get(); }
|
||||
|
||||
const int GetErrorCode() const { return errorCode.GetErrorCode(); }
|
||||
|
||||
private:
|
||||
Option<T> data;
|
||||
Failure errorCode;
|
||||
};
|
||||
|
||||
} // namespace mindspore
|
||||
|
||||
#endif
|
@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_UUID_BASE_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_UUID_BASE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "async/option.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace uuids {
|
||||
|
||||
const std::size_t UUID_SIZE = 16;
|
||||
|
||||
struct uuid {
|
||||
public:
|
||||
static std::size_t Size();
|
||||
|
||||
static std::string ToBytes(const uuid &u);
|
||||
|
||||
static Option<uuid> FromBytes(const std::string &s);
|
||||
|
||||
static Option<unsigned char> GetValue(char c);
|
||||
|
||||
static Option<uuid> FromString(const std::string &s);
|
||||
|
||||
// To check whether uuid looks like 0000000-000-000-000-000000000000000
|
||||
bool IsNilUUID() const;
|
||||
|
||||
const uint8_t *Get() const;
|
||||
|
||||
private:
|
||||
const uint8_t *BeginAddress() const;
|
||||
|
||||
const uint8_t *EndAddress() const;
|
||||
|
||||
uint8_t *BeginAddress();
|
||||
|
||||
uint8_t *EndAddress();
|
||||
|
||||
friend class RandomBasedGenerator;
|
||||
friend bool operator==(uuid const &left, uuid const &right);
|
||||
friend bool operator!=(uuid const &left, uuid const &right);
|
||||
template <typename T, typename F>
|
||||
friend std::basic_ostream<T, F> &operator<<(std::basic_ostream<T, F> &s, const struct uuid &outputUuid);
|
||||
uint8_t uuidData[UUID_SIZE];
|
||||
};
|
||||
|
||||
class RandomBasedGenerator {
|
||||
public:
|
||||
static uuid GenerateRandomUuid();
|
||||
};
|
||||
|
||||
// operator override
|
||||
inline bool operator==(uuid const &left, uuid const &right) {
|
||||
return std::equal(left.BeginAddress(), left.EndAddress(), right.BeginAddress());
|
||||
}
|
||||
|
||||
// operator override
|
||||
inline bool operator!=(uuid const &left, uuid const &right) { return !(left == right); }
|
||||
|
||||
// operator override
|
||||
template <typename T, typename F>
|
||||
std::basic_ostream<T, F> &operator<<(std::basic_ostream<T, F> &s, const struct uuid &outputUuid) {
|
||||
const int FIRST_DELIM_OFFSET = 3;
|
||||
const int SECOND_DELIM_OFFSET = 5;
|
||||
const int THIRD_DELIM_OFFSET = 7;
|
||||
const int FOURTH_DELIM_OFFSET = 9;
|
||||
const int UUID_WIDTH = 2;
|
||||
s << std::hex << std::setfill(static_cast<T>('0'));
|
||||
|
||||
int i = 0;
|
||||
for (const uint8_t *ptr = outputUuid.BeginAddress(); ptr < outputUuid.EndAddress(); ++ptr, ++i) {
|
||||
s << std::setw(UUID_WIDTH) << (int)(*ptr);
|
||||
if (i == FIRST_DELIM_OFFSET || i == SECOND_DELIM_OFFSET || i == THIRD_DELIM_OFFSET || i == FOURTH_DELIM_OFFSET) {
|
||||
s << '-';
|
||||
}
|
||||
}
|
||||
|
||||
s << std::setfill(static_cast<T>(' ')) << std::dec;
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace uuids
|
||||
} // namespace mindspore
|
||||
|
||||
#endif /* UUID_BASE_HPP_ */
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_UUID_GENERATOR_H
|
||||
#define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_UUID_GENERATOR_H
|
||||
|
||||
#include "uuid_base.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
namespace uuid_generator {
|
||||
struct UUID : public mindspore::uuids::uuid {
|
||||
public:
|
||||
explicit UUID(const mindspore::uuids::uuid &inputUUID) : mindspore::uuids::uuid(inputUUID) {}
|
||||
static UUID GetRandomUUID();
|
||||
std::string ToString();
|
||||
};
|
||||
} // namespace uuid_generator
|
||||
|
||||
namespace localid_generator {
|
||||
int GenLocalActorId();
|
||||
|
||||
#ifdef HTTP_ENABLED
|
||||
int GenHttpClientConnId();
|
||||
int GenHttpServerConnId();
|
||||
#endif
|
||||
|
||||
} // namespace localid_generator
|
||||
|
||||
} // namespace mindspore
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue