-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathunix_socket.hpp
More file actions
56 lines (44 loc) · 1.22 KB
/
unix_socket.hpp
File metadata and controls
56 lines (44 loc) · 1.22 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#ifndef MSCCLPP_UNIX_SOCKET_HPP_
#define MSCCLPP_UNIX_SOCKET_HPP_
#include <cstdint>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
namespace mscclpp {
class UnixSocketServer {
public:
static UnixSocketServer& instance();
static std::string generateSocketPath(int localRankId);
void start(int localRankId);
void stop();
uint32_t registerFd(int fd);
void unregisterFd(uint32_t fdId);
std::string getSocketPath() const;
private:
int listenUnixSockFd_ = -1;
std::string listenUnixSockPath_;
std::thread mainThread_;
std::unique_ptr<uint32_t> abortFlagStorage_;
volatile uint32_t* abortFlag_;
std::mutex mutex_;
std::unordered_map<uint32_t, int> fdMap_;
UnixSocketServer();
void mainLoop();
};
class UnixSocketClient {
public:
static UnixSocketClient& instance();
int requestFd(const std::string& socketPath, uint32_t fdId);
~UnixSocketClient();
private:
std::unordered_map<std::string, int> cachedFds_;
std::mutex mutex_;
UnixSocketClient() = default;
int requestFdInternal(int connFd, uint32_t fdId);
};
} // namespace mscclpp
#endif // MSCCLPP_UNIX_SOCKET_HPP_