| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#define STOP_TASK_FN_PTR |
| 18 |
#include "preamble.h" |
| 19 |
|
| 20 |
static void s_stop_task(struct aws_task *task, void *args, enum aws_task_status status) { |
| 21 |
|
| 22 |
(void)task; |
| 23 |
struct aws_event_loop *event_loop = args; |
| 24 |
struct epoll_loop *epoll_loop = event_loop->impl_data; |
| 25 |
|
| 26 |
|
| 27 |
_(unwrap &epoll_loop->stop_task_ptr) |
| 28 |
aws_atomic_store_ptr(&epoll_loop->stop_task_ptr, NULL); |
| 29 |
_(wrap &epoll_loop->stop_task_ptr) |
| 30 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
_(unwrap epoll_loop::status) |
| 35 |
epoll_loop->should_continue = false; |
| 36 |
_(wrap epoll_loop::status) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static int s_stop(struct aws_event_loop *event_loop |
| 41 |
_(ghost \claim(c_event_loop)) |
| 42 |
_(ghost \claim(c_mutex)) |
| 43 |
) { |
| 44 |
_(assert \always_by_claim(c_event_loop, event_loop)) |
| 45 |
struct epoll_loop *epoll_loop = event_loop->impl_data; |
| 46 |
|
| 47 |
void *expected_ptr = NULL; |
| 48 |
_(unwrap &epoll_loop->stop_task_ptr) |
| 49 |
bool update_succeeded = |
| 50 |
aws_atomic_compare_exchange_ptr(&epoll_loop->stop_task_ptr, &expected_ptr, &epoll_loop->stop_task); |
| 51 |
_(wrap &epoll_loop->stop_task_ptr) |
| 52 |
if (!update_succeeded) { |
| 53 |
|
| 54 |
return AWS_OP_SUCCESS; |
| 55 |
} |
| 56 |
AWS_LOGF_INFO(AWS_LS_IO_EVENT_LOOP, "id=%p: Stopping event-loop thread.", (void *)event_loop); |
| 57 |
aws_task_init(&epoll_loop->stop_task, s_stop_task, event_loop, "epoll_event_loop_stop"); |
| 58 |
s_schedule_task_now(event_loop, &epoll_loop->stop_task _(ghost c_event_loop) _(ghost c_mutex)); |
| 59 |
|
| 60 |
return AWS_OP_SUCCESS; |
| 61 |
} |
| 62 |
|
| 63 |
int aws_thread_join(struct aws_thread *thread |
| 64 |
_(ghost struct aws_event_loop *event_loop) _(ghost \claim(c_event_loop)) _(ghost \claim(c_mutex)) |
| 65 |
) |
| 66 |
_(requires c_event_loop != c_mutex) |
| 67 |
_(requires \wrapped0(c_event_loop) && \claims(c_event_loop, event_loop->\closed) && \claims_object(c_event_loop, event_loop)) |
| 68 |
_(writes c_event_loop, event_loop) |
| 69 |
_(ensures !c_event_loop->\closed) |
| 70 |
_(ensures \wrapped0(event_loop) && \nested(epoll_loop_of(event_loop))) |
| 71 |
_(ensures ownership_of_epoll_loop_objects(epoll_loop_of(event_loop))) |
| 72 |
_(ensures epoll_loop_of(event_loop)->task_pre_queue_mutex.locked == 0) |
| 73 |
_(maintains \malloc_root(epoll_loop_of(event_loop))) |
| 74 |
_(maintains \wrapped0(c_mutex) && \claims_object(c_mutex, &epoll_loop_of(event_loop)->task_pre_queue_mutex)) |
| 75 |
; |
| 76 |
|
| 77 |
static int s_wait_for_stop_completion(struct aws_event_loop *event_loop |
| 78 |
_(ghost \claim(c_event_loop)) _(ghost \claim(c_mutex)) |
| 79 |
) { |
| 80 |
struct epoll_loop *epoll_loop = _(by_claim c_event_loop) event_loop->impl_data; |
| 81 |
int result = aws_thread_join(&epoll_loop->thread_created_on _(ghost event_loop) _(ghost c_event_loop) _(ghost c_mutex)); |
| 82 |
aws_thread_decrement_unjoined_count(); |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
int aws_thread_launch( |
| 87 |
struct aws_thread *thread, |
| 88 |
void (*func)(void *arg), |
| 89 |
void *arg, |
| 90 |
const struct aws_thread_options *options) |
| 91 |
_(requires \wrapped0(event_loop_of(arg))) |
| 92 |
_(requires func->\valid) |
| 93 |
; |
| 94 |
|
| 95 |
|
| 96 |
void dummy_main_loop(void *arg); |
| 97 |
|
| 98 |
static int s_run(struct aws_event_loop *event_loop _(ghost \claim(c_mutex))) { |
| 99 |
struct epoll_loop *epoll_loop = event_loop->impl_data; |
| 100 |
|
| 101 |
AWS_LOGF_INFO(AWS_LS_IO_EVENT_LOOP, "id=%p: Starting event-loop thread.", (void *)event_loop); |
| 102 |
|
| 103 |
epoll_loop->should_continue = true; |
| 104 |
aws_thread_increment_unjoined_count(); |
| 105 |
if (aws_thread_launch(&epoll_loop->thread_created_on, &dummy_main_loop, event_loop, &epoll_loop->thread_options)) { |
| 106 |
aws_thread_decrement_unjoined_count(); |
| 107 |
AWS_LOGF_FATAL(AWS_LS_IO_EVENT_LOOP, "id=%p: thread creation failed.", (void *)event_loop); |
| 108 |
epoll_loop->should_continue = false; |
| 109 |
return AWS_OP_ERR; |
| 110 |
} |
| 111 |
|
| 112 |
return AWS_OP_SUCCESS; |
| 113 |
} |
| 114 |
|