Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secitem branch with latest nw_socket #703

Closed
wants to merge 45 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b1d0202
Switch CI to use roles (#698)
DmitriyMusatkin Dec 2, 2024
6fcd019
Merge branch 'main' into runtime_select_event_loop
xiazhvera Dec 3, 2024
85bf6ce
move aws_io_handle
xiazhvera Dec 3, 2024
ef012d3
schedule service entry on dispatch queue resume
xiazhvera Dec 3, 2024
31a9a95
update cr
xiazhvera Dec 4, 2024
8bb613e
Merge branch 'runtime_select_event_loop' of github.com:awslabs/aws-c-…
xiazhvera Dec 4, 2024
7cdd319
WIP code review update
xiazhvera Dec 5, 2024
e1d7513
remove apple network framewokr CI so that we dont block merge
xiazhvera Dec 9, 2024
d45eb98
wip update cr
xiazhvera Dec 10, 2024
98c558e
update vcc and related hash
xiazhvera Dec 17, 2024
c8e57c5
update checksum
xiazhvera Dec 17, 2024
cfa0399
Merge branch 'runtime_select_event_loop' of github.com:awslabs/aws-c-…
xiazhvera Dec 18, 2024
f4414aa
refactor dispatch queue release process/update iteartion schedule pro…
xiazhvera Dec 18, 2024
e1ce086
set up impl_data of event loop ealier
xiazhvera Dec 18, 2024
a93216b
revert ci change
xiazhvera Dec 18, 2024
2c6ef60
Fix CI for GCC-13 on Ubuntu-18 (#700)
waahm7 Dec 19, 2024
6b9a05b
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Dec 20, 2024
1cc1633
Merge branch 'main' into runtime_select_event_loop
xiazhvera Jan 6, 2025
4a4ebae
fix typo in log message (#702)
graebm Jan 6, 2025
7adfffb
update code review
xiazhvera Jan 7, 2025
485e376
Merge branch 'runtime_select_event_loop' of github.com:awslabs/aws-c-…
xiazhvera Jan 7, 2025
5e1e728
Merge branch 'main' into runtime_select_event_loop
xiazhvera Jan 7, 2025
893f94b
Merge branch 'runtime_select_event_loop' of github.com:awslabs/aws-c-…
xiazhvera Jan 7, 2025
18b5d82
fix CI flag
xiazhvera Jan 7, 2025
f9ff79a
lint and fix warning
xiazhvera Jan 7, 2025
98ea949
Merge branch 'runtime_select_event_loop' of github.com:awslabs/aws-c-…
xiazhvera Jan 7, 2025
6933da0
rename private headers
xiazhvera Jan 7, 2025
630d0bb
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 7, 2025
16c36e8
cr update
xiazhvera Jan 7, 2025
d986649
improve comments and lock results
xiazhvera Jan 8, 2025
1dc7c7d
Merge branch 'grand_dispatch_queue' of github.com:awslabs/aws-c-io in…
xiazhvera Jan 8, 2025
0b7ba13
update renamed files
xiazhvera Jan 8, 2025
c23e531
merge fixes
sbSteveK Jan 8, 2025
8aab223
fixes
sbSteveK Jan 8, 2025
ab17513
fixes
sbSteveK Jan 8, 2025
5fac7a2
more merge fixes
sbSteveK Jan 8, 2025
603e7bf
use common aws_min_u64 instead of MIN
sbSteveK Jan 8, 2025
f1c53ec
add sanitizers to macOS
sbSteveK Jan 8, 2025
a367bce
separate asan/tsan jobs
sbSteveK Jan 8, 2025
4ea2651
asan options
sbSteveK Jan 9, 2025
ca0ab21
extra socket bind checks and logging
sbSteveK Jan 9, 2025
685ac02
clean up s_setup_tls_options_from_context
sbSteveK Jan 9, 2025
869d910
check whether any tls options need to be retrieved before assigning
sbSteveK Jan 9, 2025
213aca3
check whether any tls options need to be retrieved before assigning f…
sbSteveK Jan 9, 2025
cc97e64
remove retrieve check
sbSteveK Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve comments and lock results
xiazhvera committed Jan 8, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit d9866495bf70979d9cd0ecd54ab655e17cc5d720
87 changes: 49 additions & 38 deletions source/darwin/dispatch_queue_event_loop.c
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ static struct aws_event_loop_vtable s_vtable = {
* Functions ************
* `s_run_iteration`: The function execute on each single iteration
* `begin_iteration`: Decide if we should run the iteration
* `end_iteration`: Clean up the related resource and decide if we should schedule next iteration
* `end_iteration`: Clean up the related resource and determine if we should schedule next iteration
*
*/

@@ -98,6 +98,12 @@ struct dispatch_scheduling_state {

/* Internal ref-counted dispatch loop context to processing Apple Dispatch Queue Resources */
struct dispatch_loop_context {
/**
* The conetxt lock is a read-write lock used to protect dispatch_loop.
* The write lock will be acquired when we make changes to dispatch_loop. And the read lock will be acquired
* when we need verify if the dispatch_loop is alive. This makes sure that the dispatch_loop will not be destroyed
* from other thread while we are using it.
*/
struct aws_rw_lock lock;
struct dispatch_loop *io_dispatch_loop;
struct dispatch_scheduling_state scheduling_state;
@@ -179,16 +185,18 @@ static void s_scheduled_service_entry_destroy(struct scheduled_service_entry *en
aws_mem_release(entry->allocator, entry);
}

// checks to see if another scheduled iteration already exists that will either
// handle our needs or reschedule at the end to do so
static bool s_should_schedule_iteration(
struct aws_linked_list *scheduled_iterations,
uint64_t proposed_iteration_time) {
if (aws_linked_list_empty(scheduled_iterations)) {
/**
* Helper function to check if another scheduled iteration already exists that will handle our needs
*
* The function should be wrapped with the following locks:
* scheduled_services lock: To safely access the scheduled_services list
*/
static bool s_should_schedule_iteration(struct aws_linked_list *scheduled_services, uint64_t proposed_iteration_time) {
if (aws_linked_list_empty(scheduled_services)) {
return true;
}

struct aws_linked_list_node *head_node = aws_linked_list_front(scheduled_iterations);
struct aws_linked_list_node *head_node = aws_linked_list_front(scheduled_services);
struct scheduled_service_entry *entry = AWS_CONTAINER_OF(head_node, struct scheduled_service_entry, node);

// is the next scheduled iteration later than what we require?
@@ -231,15 +239,15 @@ static void s_dispatch_event_loop_destroy(void *context) {
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "id=%p: Destroyed Dispatch Queue Event Loop.", (void *)event_loop);
}

/** Return a aws_string* with unique dispatch queue id string. The id is In format of
* "com.amazonaws.commonruntime.eventloop.<UUID>"*/
// static const size_t AWS_IO_APPLE_DISPATCH_QUEUE_ID_PREFIX_LENGTH = 37;
static const char AWS_LITERAL_APPLE_DISPATCH_QUEUE_ID_PREFIX[] = "com.amazonaws.commonruntime.eventloop.";
static const size_t AWS_IO_APPLE_DISPATCH_QUEUE_ID_PREFIX_LENGTH =
AWS_ARRAY_SIZE(AWS_LITERAL_APPLE_DISPATCH_QUEUE_ID_PREFIX);
static const size_t AWS_IO_APPLE_DISPATCH_QUEUE_ID_LENGTH =
AWS_IO_APPLE_DISPATCH_QUEUE_ID_PREFIX_LENGTH + AWS_UUID_STR_LEN;

/**
* Generates a unique identifier for a dispatch queue in the format "com.amazonaws.commonruntime.eventloop.<UUID>".
* This identifier will be stored in the provided `result` buffer.
*/
static void s_get_unique_dispatch_queue_id(char result[AWS_IO_APPLE_DISPATCH_QUEUE_ID_LENGTH]) {
struct aws_uuid uuid;
AWS_FATAL_ASSERT(aws_uuid_init(&uuid) == AWS_OP_SUCCESS);
@@ -407,8 +415,12 @@ static int s_stop(struct aws_event_loop *event_loop) {
return AWS_OP_SUCCESS;
}

// returns true if we should execute an iteration, false otherwise
// The function should be wrapped with dispatch_loop->context.lock
/**
* The function decides if we should run this iteration.
* Returns true if we should execute an iteration, false otherwise
*
* The function should be wrapped with dispatch_loop->context.lock to retain the dispatch loop while running.
*/
static bool begin_iteration(struct scheduled_service_entry *entry) {
struct dispatch_loop *dispatch_loop = entry->dispatch_queue_context->io_dispatch_loop;

@@ -418,8 +430,10 @@ static bool begin_iteration(struct scheduled_service_entry *entry) {
return true;
}

// conditionally schedule another iteration as needed
// The function should be wrapped with dispatch_loop->context.lock
/**
* Clean up the related resource and determine if we should schedule next iteration.
* The function should be wrapped with dispatch_loop->context.lock to retain the dispatch loop while running.
* */
static void end_iteration(struct scheduled_service_entry *entry) {

struct dispatch_loop_context *context = entry->dispatch_queue_context;
@@ -430,28 +444,21 @@ static void end_iteration(struct scheduled_service_entry *entry) {

// Remove the node before do scheduling so we didnt consider the entry itself
aws_linked_list_remove(&entry->node);
// if there are any cross-thread tasks, reschedule an iteration for now

bool should_schedule = false;
uint64_t should_schedule_at_time = 0;
if (!aws_linked_list_empty(&dispatch_loop->synced_data.cross_thread_tasks)) {
// added during service which means nothing was scheduled because will_schedule was true
should_schedule = true;
}
/* we already know there are tasks to be scheduled, we just want the next run time. */
else if (aws_task_scheduler_has_tasks(&dispatch_loop->scheduler, &should_schedule_at_time)) {
should_schedule = true;
}

if (should_schedule) {
s_lock_service_entries(context);
s_try_schedule_new_iteration(context, 0);
s_try_schedule_new_iteration(context, should_schedule_at_time);
s_unlock_service_entries(context);
} else {
// no cross thread tasks, so check internal time-based scheduler
uint64_t next_task_time = 0;
/* we already know it has tasks, we just scheduled one. We just want the next run time. */
bool has_task = aws_task_scheduler_has_tasks(&dispatch_loop->scheduler, &next_task_time);

if (has_task) {
// only schedule an iteration if there isn't an existing dispatched iteration for the next task time or
// earlier
s_lock_service_entries(context);
if (s_should_schedule_iteration(
&dispatch_loop->context->scheduling_state.scheduled_services, next_task_time)) {
s_try_schedule_new_iteration(context, next_task_time);
}
s_unlock_service_entries(context);
}
}

s_unlock_cross_thread_data(dispatch_loop);
@@ -511,7 +518,10 @@ static void s_run_iteration(void *context) {
*
* If timestamp==0, the function will always schedule a new iteration as long as the event loop is not suspended.
*
* The function should be wrapped with dispatch_loop->context->lock & dispatch_loop->synced_data.lock
* The function should be wrapped with the following locks:
* dispatch_loop->context->lock: To retain the dispatch loop
* dispatch_loop->synced_data.lock : To verify if the dispatch loop is suspended
* dispatch_loop_context->scheduling_state->services_lock: To modify the scheduled_services list
*/
static void s_try_schedule_new_iteration(struct dispatch_loop_context *dispatch_loop_context, uint64_t timestamp) {
struct dispatch_loop *dispatch_loop = dispatch_loop_context->io_dispatch_loop;
@@ -530,12 +540,13 @@ static void s_try_schedule_new_iteration(struct dispatch_loop_context *dispatch_
/**
* The Apple dispatch queue uses automatic reference counting (ARC). If an iteration remains in the queue, it will
* persist until it is executed. Scheduling a block far into the future can keep the dispatch queue alive
* unnecessarily, even if the app is destroyed. To avoid this, Ensure an iteration is scheduled within a 1-second
* interval to prevent it from remaining in the Apple dispatch queue indefinitely.
* unnecessarily, even if the app has shutdown. To avoid this, Ensure an iteration is scheduled within a
* 1-second interval to prevent it from remaining in the Apple dispatch queue indefinitely.
*/
delta = MIN(delta, AWS_TIMESTAMP_NANOS);

if (delta == 0) {
// dispatch_after_f(0 , ...) is equivclient to dispatch_async_f(...) functionality wise, while
// dispatch_after_f(0 , ...) is not as optimal as dispatch_async_f(...)
// https://developer.apple.com/documentation/dispatch/1452878-dispatch_after_f
dispatch_async_f(dispatch_loop->dispatch_queue, entry, s_run_iteration);
4 changes: 4 additions & 0 deletions source/darwin/dispatch_queue_event_loop_private.h
Original file line number Diff line number Diff line change
@@ -29,6 +29,10 @@ struct dispatch_loop {

/* Synced data handle cross thread tasks and events, and event loop operations*/
struct {
/**
* The lock is used to protect synced_data across the threads. It should be acquired whenever we touched the
* data in this synced_data struct.
*/
struct aws_mutex lock;
/*
* `is_executing` flag and `current_thread_id` together are used