forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
575 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) Microsoft | ||
// Licenced under MIT license. See LICENSE.txt for details. | ||
/////////////////////////////////////////////////////////////////////////////// | ||
#ifndef CPPCORO_DETAIL_DARWIN_HPP_INCLUDED | ||
#define CPPCORO_DETAIL_DARWIN_HPP_INCLUDED | ||
|
||
#include <cppcoro/config.hpp> | ||
|
||
#if !CPPCORO_OS_DARWIN | ||
#error <cppcoro/detail/darwin.hpp> is only supported on the Linux platform. | ||
#endif | ||
|
||
#include <cstdint> | ||
#include <functional> | ||
#include <sys/event.h> | ||
#include <utility> | ||
|
||
namespace cppcoro | ||
{ | ||
class io_service; | ||
namespace detail | ||
{ | ||
namespace darwin | ||
{ | ||
using fd_t = int; | ||
|
||
class safe_fd | ||
{ | ||
public: | ||
safe_fd(); | ||
|
||
explicit safe_fd(fd_t fd); | ||
~safe_fd() noexcept; | ||
safe_fd(const safe_fd& other) noexcept; | ||
safe_fd& operator=(const safe_fd& other) noexcept; | ||
safe_fd(safe_fd&& other) noexcept; | ||
safe_fd& operator=(safe_fd&& other) noexcept; | ||
constexpr fd_t fd() const { return m_fd; } | ||
constexpr fd_t handle() const { return m_fd; } | ||
/// Calls close() and sets the fd to -1. | ||
void close() noexcept; | ||
bool operator==(const safe_fd& other) const { return m_fd == other.m_fd; } | ||
bool operator!=(const safe_fd& other) const { return m_fd != other.m_fd; } | ||
bool operator==(fd_t fd) const { return m_fd == fd; } | ||
bool operator!=(fd_t fd) const { return m_fd != fd; } | ||
|
||
private: | ||
fd_t m_fd; | ||
}; | ||
|
||
struct io_state | ||
{ | ||
explicit io_state(io_service* ioService) noexcept | ||
: m_ioService(ioService) | ||
, m_fd(-1) | ||
, m_res(0) | ||
, m_completeFunc([] { return 0; }) | ||
{ | ||
} | ||
|
||
std::size_t get_result(); | ||
void on_operation_completed_base(); | ||
void cancel() noexcept; | ||
|
||
io_service* m_ioService; | ||
fd_t m_fd; | ||
std::int32_t m_res; | ||
std::function<int()> m_completeFunc; | ||
}; | ||
|
||
safe_fd create_timer_fd(); | ||
safe_fd create_kqueue_fd(); | ||
|
||
} // namespace darwin | ||
} // namespace detail | ||
} // namespace cppcoro | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) Microsoft | ||
// Licenced under MIT license. See LICENSE.txt for details. | ||
/////////////////////////////////////////////////////////////////////////////// | ||
#include <cppcoro/io_service.hpp> | ||
#include <cppcoro/detail/darwin.hpp> | ||
#include <cassert> | ||
#include <cstring> | ||
#include <errno.h> | ||
#include <sys/event.h> | ||
#include <sys/time.h> | ||
#include <sys/types.h> | ||
#include <system_error> | ||
#include <unistd.h> | ||
|
||
namespace cppcoro | ||
{ | ||
namespace detail | ||
{ | ||
namespace darwin | ||
{ | ||
safe_fd create_timer_fd() | ||
{ | ||
int fd = kqueue(); | ||
|
||
if (fd == -1) | ||
{ | ||
throw std::system_error{ static_cast<int>(errno), | ||
std::system_category(), | ||
"Error creating io_service: timer fd create" }; | ||
} | ||
|
||
return safe_fd{ fd }; | ||
} | ||
|
||
safe_fd create_kqueue_fd() | ||
{ | ||
int fd = kqueue(); | ||
|
||
if (fd == -1) | ||
{ | ||
throw std::system_error{ static_cast<int>(errno), | ||
std::system_category(), | ||
"Error creating timer thread: kqueue create" }; | ||
} | ||
|
||
return safe_fd{ fd }; | ||
} | ||
|
||
safe_fd::safe_fd() | ||
: m_fd(-1) | ||
{ | ||
} | ||
|
||
safe_fd::safe_fd(fd_t fd) | ||
: m_fd(fd) | ||
{ | ||
} | ||
|
||
safe_fd::~safe_fd() noexcept | ||
{ | ||
close(); | ||
} | ||
|
||
safe_fd::safe_fd(const safe_fd& other) noexcept | ||
: m_fd(dup(other.m_fd)) | ||
{ | ||
} | ||
|
||
safe_fd& safe_fd::operator=(const safe_fd& other) noexcept | ||
{ | ||
m_fd = dup(other.m_fd); | ||
return *this; | ||
} | ||
|
||
safe_fd::safe_fd(safe_fd&& other) noexcept | ||
: m_fd(std::exchange(other.m_fd, -1)) | ||
{ | ||
} | ||
|
||
safe_fd& safe_fd::operator=(safe_fd&& other) noexcept | ||
{ | ||
m_fd = std::exchange(other.m_fd, -1); | ||
return *this; | ||
} | ||
|
||
void safe_fd::close() noexcept | ||
{ | ||
if (m_fd != -1) | ||
{ | ||
::close(m_fd); | ||
m_fd = -1; | ||
} | ||
} | ||
|
||
std::size_t io_state::get_result() | ||
{ | ||
if (m_res < 0) | ||
{ | ||
throw std::system_error{ -m_res, std::system_category() }; | ||
} | ||
|
||
return m_res; | ||
} | ||
|
||
void io_state::on_operation_completed_base() | ||
{ | ||
m_ioService->get_io_context().unwatch_handle(m_fd); | ||
m_res = m_completeFunc(); | ||
if (m_res < 0) | ||
{ | ||
m_res = -errno; | ||
} | ||
} | ||
|
||
void io_state::cancel() noexcept | ||
{ | ||
m_ioService->get_io_context().unwatch_handle(m_fd); | ||
m_res = -ECANCELED; | ||
m_ioService->get_io_context().enqueue_message( | ||
{ message_type::CALLBACK_TYPE, static_cast<void*>(this) }); | ||
} | ||
} // namespace darwin | ||
} // namespace detail | ||
} // namespace cppcoro |
Oops, something went wrong.