Skip to content

Commit

Permalink
Implement non block safePipe on windows
Browse files Browse the repository at this point in the history
Though we try to avoid the usage of it, implement it anyway.
  • Loading branch information
wengxt committed Feb 4, 2025
1 parent 113704d commit 912788d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/lib/fcitx-utils/misc_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
#include <unordered_map>
#include <utility>
#include <fcitx-utils/endian_p.h>
#include "config.h"
#include "config.h" // IWYU pragma: keep
#include "environ.h"

#ifdef _WIN32
#include <windows.h>
#include <io.h>
#include <namedpipeapi.h>
#endif

namespace fcitx {

template <typename M, typename K>
Expand Down Expand Up @@ -286,8 +292,20 @@ class OrderedMap {
};

static inline int safePipe(int pipefd[2]) {
#ifdef HAVE_PIPE2
#if defined(HAVE_PIPE2)
return ::pipe2(pipefd, O_NONBLOCK | O_CLOEXEC);
#elif defined(_WIN32)
auto ret = ::_pipe(pipefd, 256, O_BINARY | O_NOINHERIT);
if (ret == -1) {
return -1;
}
std::array handle = {_get_osfhandle(pipefd[0]), _get_osfhandle(pipefd[1])};
DWORD mode = PIPE_NOWAIT;
SetNamedPipeHandleState(reinterpret_cast<HANDLE>(handle[0]), &mode,
nullptr, nullptr);
SetNamedPipeHandleState(reinterpret_cast<HANDLE>(handle[1]), &mode,
nullptr, nullptr);
return ret;
#else
int ret = ::pipe(pipefd);
if (ret == -1)
Expand Down

0 comments on commit 912788d

Please sign in to comment.