Skip to content

Commit

Permalink
Event Loop & Socket Type Multi-Support (#692)
Browse files Browse the repository at this point in the history
Co-authored-by: Bret Ambrose <[email protected]>
Co-authored-by: Bret Ambrose <[email protected]>
Co-authored-by: Michael Graeb <[email protected]>
  • Loading branch information
4 people authored Jan 8, 2025
1 parent 4a4ebae commit fdb053b
Show file tree
Hide file tree
Showing 25 changed files with 942 additions and 185 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ jobs:
macos:
runs-on: macos-14 # latest
strategy:
fail-fast: false
matrix:
eventloop: ["kqueue"] # TODO: Add "dispatch_queue" when apple network framework is implemented.
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
Expand All @@ -238,7 +242,7 @@ jobs:
run: |
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
chmod a+x builder
./builder build -p ${{ env.PACKAGE_NAME }}
./builder build -p ${{ env.PACKAGE_NAME }} --cmake-extra=-DAWS_USE_APPLE_NETWORK_FRAMEWORK=${{ matrix.eventloop == 'dispatch_queue' && 'ON' || 'OFF' }}
macos-x64:
runs-on: macos-14-large # latest
Expand All @@ -255,6 +259,10 @@ jobs:
macos-debug:
runs-on: macos-14 # latest
strategy:
fail-fast: false
matrix:
eventloop: ["kqueue"] # TODO: Add "-DAWS_USE_APPLE_NETWORK_FRAMEWORK=ON" when apple network framework is implemented.
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
Expand All @@ -264,7 +272,7 @@ jobs:
run: |
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
chmod a+x builder
./builder build -p ${{ env.PACKAGE_NAME }} --config Debug
./builder build -p ${{ env.PACKAGE_NAME }} --cmake-extra=-DAWS_USE_APPLE_NETWORK_FRAMEWORK=${{ matrix.eventloop == 'dispatch_queue' && 'ON' || 'OFF' }} --config Debug
freebsd:
runs-on: ubuntu-24.04 # latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/proof-alarm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Check
run: |
TMPFILE=$(mktemp)
echo "1fdf8e7a914412cc7242b8d64732fa89 source/linux/epoll_event_loop.c" > $TMPFILE
echo "fb906f599051ed940f141b7d11de0db1 source/linux/epoll_event_loop.c" > $TMPFILE
md5sum --check $TMPFILE
# No further steps if successful
Expand Down
34 changes: 24 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if (WIN32)
)
list(APPEND AWS_IO_OS_SRC ${AWS_IO_IOCP_SRC})

set(EVENT_LOOP_DEFINE "IO_COMPLETION_PORTS")
list(APPEND EVENT_LOOP_DEFINES "IO_COMPLETION_PORTS")
endif ()

if (MSVC)
Expand All @@ -102,7 +102,7 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Androi
)
set(PLATFORM_LIBS "")

set(EVENT_LOOP_DEFINE "EPOLL")
list(APPEND EVENT_LOOP_DEFINES "EPOLL")
set(USE_S2N ON)

elseif (APPLE)
Expand All @@ -117,13 +117,18 @@ elseif (APPLE)
)

find_library(SECURITY_LIB Security)
if (NOT SECURITY_LIB)
message(FATAL_ERROR "Security framework not found")
find_library(NETWORK_LIB Network)

# Enable dispatch queue if the libraries are avaliable
if (NETWORK_LIB AND SECURITY_LIB)
list(APPEND PLATFORM_LIBS "-framework Security -framework Network")
list(APPEND EVENT_LOOP_DEFINES "DISPATCH_QUEUE")
endif ()

#No choice on TLS for apple, darwinssl will always be used.
list(APPEND PLATFORM_LIBS "-framework Security")
set(EVENT_LOOP_DEFINE "KQUEUE")
# Enable KQUEUE on MacOS
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
list(APPEND EVENT_LOOP_DEFINES "KQUEUE")
endif()

elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
file(GLOB AWS_IO_OS_HEADERS
Expand All @@ -134,7 +139,7 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "NetB
"source/posix/*.c"
)

set(EVENT_LOOP_DEFINE "KQUEUE")
list(APPEND EVENT_LOOP_DEFINES "KQUEUE")
set(USE_S2N ON)

endif()
Expand Down Expand Up @@ -187,7 +192,12 @@ aws_add_sanitizers(${PROJECT_NAME})
# We are not ABI stable yet
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION 1.0.0)

target_compile_definitions(${PROJECT_NAME} PUBLIC "-DAWS_USE_${EVENT_LOOP_DEFINE}")
if (NOT EVENT_LOOP_DEFINES)
message(FATAL_ERROR "Event Loop is not setup on the platform.")
endif()
foreach(EVENT_LOOP_DEFINE IN LISTS EVENT_LOOP_DEFINES)
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DAWS_ENABLE_${EVENT_LOOP_DEFINE}")
endforeach()

if (BYO_CRYPTO)
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DBYO_CRYPTO")
Expand All @@ -202,7 +212,11 @@ if (BUILD_RELOCATABLE_BINARIES)
endif()

if (USE_VSOCK)
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DUSE_VSOCK")
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DUSE_VSOCK")
endif()

if (AWS_USE_APPLE_NETWORK_FRAMEWORK)
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DAWS_USE_APPLE_NETWORK_FRAMEWORK")
endif()

target_include_directories(${PROJECT_NAME} PUBLIC
Expand Down
26 changes: 26 additions & 0 deletions include/aws/io/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ AWS_PUSH_SANE_WARNING_LEVEL

struct aws_event_loop;
struct aws_event_loop_group;
struct aws_event_loop_options;
struct aws_shutdown_callback_options;
struct aws_task;

Expand Down Expand Up @@ -47,6 +48,25 @@ struct aws_event_loop_vtable {
bool (*is_on_callers_thread)(struct aws_event_loop *event_loop);
};

/**
* Event Loop Type. If set to `AWS_EVENT_LOOP_PLATFORM_DEFAULT`, the event loop will automatically use the platform’s
* default.
*
* Default Event Loop Type
* Linux | AWS_EVENT_LOOP_EPOLL
* Windows | AWS_EVENT_LOOP_IOCP
* BSD Variants| AWS_EVENT_LOOP_KQUEUE
* macOS | AWS_EVENT_LOOP_KQUEUE
* iOS | AWS_EVENT_LOOP_DISPATCH_QUEUE
*/
enum aws_event_loop_type {
AWS_EVENT_LOOP_PLATFORM_DEFAULT = 0,
AWS_EVENT_LOOP_EPOLL,
AWS_EVENT_LOOP_IOCP,
AWS_EVENT_LOOP_KQUEUE,
AWS_EVENT_LOOP_DISPATCH_QUEUE,
};

/**
* Event loop group configuration options
*/
Expand All @@ -58,6 +78,12 @@ struct aws_event_loop_group_options {
*/
uint16_t loop_count;

/**
* Event loop type. If the event loop type is set to AWS_EVENT_LOOP_PLATFORM_DEFAULT, the
* creation function will automatically use the platform’s default event loop type.
*/
enum aws_event_loop_type type;

/**
* Optional callback to invoke when the event loop group finishes destruction.
*/
Expand Down
33 changes: 25 additions & 8 deletions include/aws/io/private/event_loop_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,30 @@ struct aws_event_loop_local_object {
struct aws_event_loop_options {
aws_io_clock_fn *clock;
struct aws_thread_options *thread_options;

/**
* Event loop type. If the event loop type is set to AWS_EVENT_LOOP_PLATFORM_DEFAULT, the
* creation function will automatically use the platform’s default event loop type.
*/
enum aws_event_loop_type type;
};

struct aws_event_loop *aws_event_loop_new_with_iocp(
struct aws_allocator *alloc,
const struct aws_event_loop_options *options);

struct aws_event_loop *aws_event_loop_new_with_dispatch_queue(
struct aws_allocator *alloc,
const struct aws_event_loop_options *options);

struct aws_event_loop *aws_event_loop_new_with_kqueue(
struct aws_allocator *alloc,
const struct aws_event_loop_options *options);

struct aws_event_loop *aws_event_loop_new_with_epoll(
struct aws_allocator *alloc,
const struct aws_event_loop_options *options);

typedef struct aws_event_loop *(aws_new_event_loop_fn)(struct aws_allocator *alloc,
const struct aws_event_loop_options *options,
void *new_loop_user_data);
Expand All @@ -105,7 +127,7 @@ struct aws_event_loop_group {

AWS_EXTERN_C_BEGIN

#ifdef AWS_USE_IO_COMPLETION_PORTS
#ifdef AWS_ENABLE_IO_COMPLETION_PORTS

/**
* Prepares aws_overlapped for use, and sets a function to call when the overlapped operation completes.
Expand All @@ -128,6 +150,7 @@ void aws_overlapped_reset(struct aws_overlapped *overlapped);
*/
AWS_IO_API
struct _OVERLAPPED *aws_overlapped_to_windows_overlapped(struct aws_overlapped *overlapped);
#endif /* AWS_ENABLE_IO_COMPLETION_PORTS */

/**
* Associates an aws_io_handle with the event loop's I/O Completion Port.
Expand All @@ -144,8 +167,6 @@ int aws_event_loop_connect_handle_to_io_completion_port(
struct aws_event_loop *event_loop,
struct aws_io_handle *handle);

#else

/**
* Subscribes on_event to events on the event-loop for handle. events is a bitwise concatenation of the events that were
* received. The definition for these values can be found in aws_io_event_type. Currently, only
Expand All @@ -161,8 +182,6 @@ int aws_event_loop_subscribe_to_io_events(
aws_event_loop_on_event_fn *on_event,
void *user_data);

#endif /* AWS_USE_IO_COMPLETION_PORTS */

/**
* Creates an instance of the default event loop implementation for the current architecture and operating system.
*/
Expand All @@ -174,9 +193,7 @@ struct aws_event_loop *aws_event_loop_new_default(struct aws_allocator *alloc, a
* extendable options.
*/
AWS_IO_API
struct aws_event_loop *aws_event_loop_new_default_with_options(
struct aws_allocator *alloc,
const struct aws_event_loop_options *options);
struct aws_event_loop *aws_event_loop_new(struct aws_allocator *alloc, const struct aws_event_loop_options *options);

/**
* Initializes common event-loop data structures.
Expand Down
72 changes: 72 additions & 0 deletions include/aws/io/private/socket_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef AWS_IO_SOCKET_IMPL_H
#define AWS_IO_SOCKET_IMPL_H

/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/io/io.h>
#include <aws/io/socket.h>

/* These are hacks for working around headers and functions we need for IO work but aren't directly includable or
linkable. these are purposely not exported. These functions only get called internally. The awkward aws_ prefixes are
just in case someone includes this header somewhere they were able to get these definitions included. */
#ifdef _WIN32
typedef void (*aws_ms_fn_ptr)(void);

void aws_check_and_init_winsock(void);
aws_ms_fn_ptr aws_winsock_get_connectex_fn(void);
aws_ms_fn_ptr aws_winsock_get_acceptex_fn(void);
#endif

int aws_socket_init_posix(
struct aws_socket *socket,
struct aws_allocator *alloc,
const struct aws_socket_options *options);

int aws_socket_init_winsock(
struct aws_socket *socket,
struct aws_allocator *alloc,
const struct aws_socket_options *options);

int aws_socket_init_apple_nw_socket(
struct aws_socket *socket,
struct aws_allocator *alloc,
const struct aws_socket_options *options);

struct aws_socket_vtable {
void (*socket_cleanup_fn)(struct aws_socket *socket);
int (*socket_connect_fn)(
struct aws_socket *socket,
const struct aws_socket_endpoint *remote_endpoint,
struct aws_event_loop *event_loop,
aws_socket_on_connection_result_fn *on_connection_result,
void *user_data);
int (*socket_bind_fn)(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint);
int (*socket_listen_fn)(struct aws_socket *socket, int backlog_size);
int (*socket_start_accept_fn)(
struct aws_socket *socket,
struct aws_event_loop *accept_loop,
aws_socket_on_accept_result_fn *on_accept_result,
void *user_data);
int (*socket_stop_accept_fn)(struct aws_socket *socket);
int (*socket_close_fn)(struct aws_socket *socket);
int (*socket_shutdown_dir_fn)(struct aws_socket *socket, enum aws_channel_direction dir);
int (*socket_set_options_fn)(struct aws_socket *socket, const struct aws_socket_options *options);
int (*socket_assign_to_event_loop_fn)(struct aws_socket *socket, struct aws_event_loop *event_loop);
int (*socket_subscribe_to_readable_events_fn)(
struct aws_socket *socket,
aws_socket_on_readable_fn *on_readable,
void *user_data);
int (*socket_read_fn)(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read);
int (*socket_write_fn)(
struct aws_socket *socket,
const struct aws_byte_cursor *cursor,
aws_socket_on_write_completed_fn *written_fn,
void *user_data);
int (*socket_get_error_fn)(struct aws_socket *socket);
bool (*socket_is_open_fn)(struct aws_socket *socket);
};

#endif // AWS_IO_SOCKET_IMPL_H
Loading

0 comments on commit fdb053b

Please sign in to comment.