Skip to content

Commit a89782d

Browse files
committed
Added linux scheduler
1 parent e8c08d1 commit a89782d

File tree

7 files changed

+465
-11
lines changed

7 files changed

+465
-11
lines changed

include/cppcoro/detail/linux.hpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// Copyright (c) Microsoft
3+
// Licenced under MIT license. See LICENSE.txt for details.
4+
///////////////////////////////////////////////////////////////////////////////
5+
#ifndef CPPCORO_DETAIL_LINUX_HPP_INCLUDED
6+
#define CPPCORO_DETAIL_LINUX_HPP_INCLUDED
7+
8+
#include <fcntl.h>
9+
#include <linux/limits.h>
10+
#include <sys/epoll.h>
11+
#include <sys/eventfd.h>
12+
#include <sys/resource.h>
13+
#include <sys/stat.h>
14+
#include <sys/timerfd.h>
15+
#include <unistd.h>
16+
#include <utility>
17+
18+
namespace cppcoro
19+
{
20+
namespace detail
21+
{
22+
namespace linux
23+
{
24+
using fd_t = int;
25+
26+
enum message_type
27+
{
28+
CALLBACK_TYPE,
29+
RESUME_TYPE
30+
};
31+
32+
class safe_fd
33+
{
34+
public:
35+
safe_fd()
36+
: m_fd(-1)
37+
{
38+
}
39+
40+
explicit safe_fd(fd_t fd)
41+
: m_fd(fd)
42+
{
43+
}
44+
45+
safe_fd(const safe_fd& other) = delete;
46+
47+
safe_fd(safe_fd&& other) noexcept
48+
: m_fd(other.m_fd)
49+
{
50+
other.m_fd = -1;
51+
}
52+
53+
~safe_fd() { close(); }
54+
55+
safe_fd& operator=(safe_fd fd) noexcept
56+
{
57+
swap(fd);
58+
return *this;
59+
}
60+
61+
constexpr fd_t fd() const { return m_fd; }
62+
63+
/// Calls close() and sets the fd to -1.
64+
void close() noexcept;
65+
66+
void swap(safe_fd& other) noexcept { std::swap(m_fd, other.m_fd); }
67+
68+
bool operator==(const safe_fd& other) const { return m_fd == other.m_fd; }
69+
70+
bool operator!=(const safe_fd& other) const { return m_fd != other.m_fd; }
71+
72+
bool operator==(fd_t fd) const { return m_fd == fd; }
73+
74+
bool operator!=(fd_t fd) const { return m_fd != fd; }
75+
76+
private:
77+
fd_t m_fd;
78+
};
79+
80+
struct message
81+
{
82+
enum message_type m_type;
83+
void* m_ptr;
84+
};
85+
86+
struct io_state : linux::message
87+
{
88+
using callback_type = void(io_state* state);
89+
callback_type* m_callback;
90+
};
91+
92+
class message_queue
93+
{
94+
private:
95+
int m_pipefd[2];
96+
safe_fd m_epollfd;
97+
struct epoll_event m_ev;
98+
99+
public:
100+
message_queue();
101+
~message_queue();
102+
bool enqueue_message(void* message, message_type type);
103+
bool dequeue_message(void*& message, message_type& type, bool wait);
104+
};
105+
106+
safe_fd create_event_fd();
107+
safe_fd create_timer_fd();
108+
safe_fd create_epoll_fd();
109+
} // namespace linux
110+
} // namespace detail
111+
} // namespace cppcoro
112+
113+
#endif

include/cppcoro/io_service.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#if CPPCORO_OS_WINNT
1313
# include <cppcoro/detail/win32.hpp>
14+
#elif CPPCORO_OS_LINUX
15+
# include <cppcoro/detail/linux.hpp>
1416
#endif
1517

1618
#include <optional>
@@ -172,6 +174,9 @@ namespace cppcoro
172174

173175
std::atomic<bool> m_winsockInitialised;
174176
std::mutex m_winsockInitialisationMutex;
177+
178+
#elif CPPCORO_OS_LINUX
179+
detail::linux::message_queue m_mq;
175180
#endif
176181

177182
// Head of a linked-list of schedule operations that are

include/cppcoro/resume_on.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,23 @@ namespace cppcoro
117117
template<typename SCHEDULER, typename T>
118118
async_generator<T> resume_on(SCHEDULER& scheduler, async_generator<T> source)
119119
{
120+
<<<<<<< HEAD
120121
auto iter = co_await source.begin();
121122
auto endIter = source.end();
122123

123124
while (iter != endIter)
125+
=======
126+
for (detail::async_generator_iterator<T> iter = co_await source.begin(); iter != source.end();)
127+
>>>>>>> 4fbba52 (Added linux scheduler)
124128
{
125129
auto& value = *iter;
126130
co_await scheduler.schedule();
127131
co_yield value;
132+
<<<<<<< HEAD
128133
co_await ++iter;
134+
=======
135+
co_await ++iter; // moved due to error: insufficient contextual information to determine type on old compilers
136+
>>>>>>> 4fbba52 (Added linux scheduler)
129137
}
130138
}
131139

lib/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ if(WIN32)
156156
# TODO remove this when experimental/non-experimental include are fixed
157157
list(APPEND compile_definition _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING=1)
158158
endif()
159+
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
160+
set(linuxDetailIncludes
161+
linux.hpp
162+
)
163+
list(TRANSFORM linuxDetailIncludes PREPEND "${PROJECT_SOURCE_DIR}/include/cppcoro/detail/")
164+
list(APPEND detailIncludes ${linuxDetailIncludes})
165+
166+
list(APPEND sources
167+
linux.cpp
168+
io_service.cpp
169+
)
159170
endif()
160171

161172
add_library(cppcoro

0 commit comments

Comments
 (0)