Skip to content

Navigation Menu

Sign in
Appearance settings
Sign up
Appearance settings

Commit c7ca1f0

Browse filesBrowse the repository at this point in the historyBrowse files
ryanofskySjors
andcommitted
util, refactor: Add SocketId type alias and use it
Add SocketId = int and SocketError = -1 type aliases and apply SocketId to SpawnProcess (return type and callback parameter) and callers. SocketId type will be different on Windows, so this provides more portability. Co-authored-by: Sjors Provoost <sjors@sprovoost.nl>
1 parent be46a35 commit c7ca1f0
Copy full SHA for c7ca1f0

8 files changed

+19-17Lines changed: 19 additions & 17 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎example/calculator.cpp‎

Copy file name to clipboardExpand all lines: example/calculator.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int main(int argc, char** argv)
5151
std::cout << "Usage: mpcalculator <fd>\n";
5252
return 1;
5353
}
54-
int fd;
54+
mp::SocketId fd;
5555
if (std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {
5656
std::cerr << argv[1] << " is not a number or is larger than an int\n";
5757
return 1;
Collapse file

‎example/example.cpp‎

Copy file name to clipboardExpand all lines: example/example.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace fs = std::filesystem;
2626
static auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const std::string& new_exe_name)
2727
{
2828
mp::ProcessId pid;
29-
const int fd = mp::SpawnProcess(pid, [&](int fd) -> std::vector<std::string> {
29+
const mp::SocketId fd = mp::SpawnProcess(pid, [&](mp::SocketId fd) -> std::vector<std::string> {
3030
fs::path path = process_argv0;
3131
path.remove_filename();
3232
path.append(new_exe_name);
Collapse file

‎example/printer.cpp‎

Copy file name to clipboardExpand all lines: example/printer.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main(int argc, char** argv)
4444
std::cout << "Usage: mpprinter <fd>\n";
4545
return 1;
4646
}
47-
int fd;
47+
mp::SocketId fd;
4848
if (std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {
4949
std::cerr << argv[1] << " is not a number or is larger than an int\n";
5050
return 1;
Collapse file

‎include/mp/proxy-io.h‎

Copy file name to clipboardExpand all lines: include/mp/proxy-io.h
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,10 @@ class EventLoop
312312
std::optional<CleanupList> m_async_fns MP_GUARDED_BY(m_mutex);
313313

314314
//! Pipe read handle used to wake up the event loop thread.
315-
int m_wait_fd = -1;
315+
SocketId m_wait_fd = SocketError;
316316

317317
//! Pipe write handle used to wake up the event loop thread.
318-
int m_post_fd = -1;
318+
SocketId m_post_fd = SocketError;
319319

320320
//! Number of EventLoopRef instances referencing this event loop. This is a
321321
//! sum of the number of client and server objects (Connection, ProxyClient,
@@ -917,10 +917,10 @@ void ServeStream(EventLoop& loop, int fd, InitImpl& init)
917917
[] {});
918918
}
919919

920-
//! Given listening socket file descriptor and an init object, handle incoming
920+
//! Given listening socket identifier and an init object, handle incoming
921921
//! connections and requests by calling methods on the Init object.
922922
template <typename InitInterface, typename InitImpl>
923-
void ListenConnections(EventLoop& loop, int fd, InitImpl& init, std::optional<size_t> max_connections = std::nullopt)
923+
void ListenConnections(EventLoop& loop, SocketId fd, InitImpl& init, std::optional<size_t> max_connections = std::nullopt)
924924
{
925925
loop.sync([&]() {
926926
auto listener{std::make_shared<Listener>(
Collapse file

‎include/mp/util.h‎

Copy file name to clipboardExpand all lines: include/mp/util.h
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,11 @@ std::string ThreadName(const char* exe_name);
256256
std::string LogEscape(const kj::StringTree& string, size_t max_size);
257257

258258
using ProcessId = int;
259+
using SocketId = int;
260+
constexpr SocketId SocketError{-1};
259261

260262
//! Callback type used by SpawnProcess below.
261-
using FdToArgsFn = std::function<std::vector<std::string>(int fd)>;
263+
using FdToArgsFn = std::function<std::vector<std::string>(SocketId fd)>;
262264

263265
//! Spawn a new process that communicates with the current process over a socket
264266
//! pair. Returns pid through an output argument, and file descriptor for the
@@ -267,7 +269,7 @@ using FdToArgsFn = std::function<std::vector<std::string>(int fd)>;
267269
//! It must not rely on child pid/state, and must return the command line
268270
//! arguments that should be used to execute the process. Embed the remote file
269271
//! descriptor number in whatever format the child process expects.
270-
int SpawnProcess(ProcessId& pid, FdToArgsFn&& fd_to_args);
272+
SocketId SpawnProcess(ProcessId& pid, FdToArgsFn&& fd_to_args);
271273

272274
//! Call execvp with vector args.
273275
//! Not safe to call in a post-fork child of a multi-threaded process.
Collapse file

‎src/mp/proxy.cpp‎

Copy file name to clipboardExpand all lines: src/mp/proxy.cpp
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ EventLoop::EventLoop(const char* exe_name, LogOptions log_opts, void* context)
206206
m_log_opts(std::move(log_opts)),
207207
m_context(context)
208208
{
209-
int fds[2];
209+
SocketId fds[2];
210210
KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
211211
m_wait_fd = fds[0];
212212
m_post_fd = fds[1];
@@ -218,8 +218,8 @@ EventLoop::~EventLoop()
218218
const Lock lock(m_mutex);
219219
KJ_ASSERT(m_post_fn == nullptr);
220220
KJ_ASSERT(!m_async_fns);
221-
KJ_ASSERT(m_wait_fd == -1);
222-
KJ_ASSERT(m_post_fd == -1);
221+
KJ_ASSERT(m_wait_fd == SocketError);
222+
KJ_ASSERT(m_post_fd == SocketError);
223223
KJ_ASSERT(m_num_refs == 0);
224224

225225
// Spin event loop. wait for any promises triggered by RPC shutdown.
@@ -270,8 +270,8 @@ void EventLoop::loop()
270270
wait_stream = nullptr;
271271
KJ_SYSCALL(::close(post_fd));
272272
const Lock lock(m_mutex);
273-
m_wait_fd = -1;
274-
m_post_fd = -1;
273+
m_wait_fd = SocketError;
274+
m_post_fd = SocketError;
275275
m_async_fns.reset();
276276
m_cv.notify_all();
277277
}
Collapse file

‎src/mp/util.cpp‎

Copy file name to clipboardExpand all lines: src/mp/util.cpp
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ std::string LogEscape(const kj::StringTree& string, size_t max_size)
116116
return result;
117117
}
118118

119-
int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
119+
SocketId SpawnProcess(ProcessId& pid, FdToArgsFn&& fd_to_args)
120120
{
121-
int fds[2];
121+
SocketId fds[2];
122122
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0) {
123123
throw std::system_error(errno, std::system_category(), "socketpair");
124124
}
Collapse file

‎test/mp/test/spawn_tests.cpp‎

Copy file name to clipboardExpand all lines: test/mp/test/spawn_tests.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ KJ_TEST("SpawnProcess does not run callback in child")
8989
});
9090

9191
ProcessId pid{-1};
92-
const int fd{SpawnProcess(pid, [&](int child_fd) -> std::vector<std::string> {
92+
const SocketId fd{SpawnProcess(pid, [&](SocketId child_fd) -> std::vector<std::string> {
9393
// If this callback runs in the post-fork child, target_mutex appears
9494
// locked forever (the owning thread does not exist), so this deadlocks.
9595
std::lock_guard<std::mutex> g(target_mutex);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.