| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/io/pipe.h> |
| 7 |
|
| 8 |
#include <aws/io/event_loop.h> |
| 9 |
#include <aws/io/private/event_loop_impl.h> |
| 10 |
|
| 11 |
#ifdef __GLIBC__ |
| 12 |
# define __USE_GNU |
| 13 |
#endif |
| 14 |
|
| 15 |
|
| 16 |
#if !defined(COMPAT_MODE) && defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9) || __GLIBC__ > 2) |
| 17 |
# define HAVE_PIPE2 1 |
| 18 |
#else |
| 19 |
# define HAVE_PIPE2 0 |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
#include <fcntl.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#ifndef O_CLOEXEC |
| 33 |
# define O_CLOEXEC 02000000 |
| 34 |
#endif |
| 35 |
|
| 36 |
struct read_end_impl { |
| 37 |
struct aws_allocator *alloc; |
| 38 |
struct aws_io_handle handle; |
| 39 |
struct aws_event_loop *event_loop; |
| 40 |
aws_pipe_on_readable_fn *on_readable_user_callback; |
| 41 |
void *on_readable_user_data; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
bool *did_user_callback_clean_up_read_end; |
| 46 |
|
| 47 |
bool is_subscribed; |
| 48 |
}; |
| 49 |
|
| 50 |
struct pipe_write_request { |
| 51 |
struct aws_byte_cursor original_cursor; |
| 52 |
struct aws_byte_cursor cursor; |
| 53 |
size_t num_bytes_written; |
| 54 |
aws_pipe_on_write_completed_fn *user_callback; |
| 55 |
void *user_data; |
| 56 |
struct aws_linked_list_node list_node; |
| 57 |
|
| 58 |
|
| 59 |
bool did_user_callback_clean_up_write_end; |
| 60 |
}; |
| 61 |
|
| 62 |
struct write_end_impl { |
| 63 |
struct aws_allocator *alloc; |
| 64 |
struct aws_io_handle handle; |
| 65 |
struct aws_event_loop *event_loop; |
| 66 |
struct aws_linked_list write_list; |
| 67 |
|
| 68 |
|
| 69 |
struct pipe_write_request *currently_invoking_write_callback; |
| 70 |
|
| 71 |
bool is_writable; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
}; |
| 76 |
|
| 77 |
static void s_write_end_on_event( |
| 78 |
struct aws_event_loop *event_loop, |
| 79 |
struct aws_io_handle *handle, |
| 80 |
int events, |
| 81 |
void *user_data); |
| 82 |
|
| 83 |
static int s_translate_posix_error(int err) { |
| 84 |
AWS_ASSERT(err); |
| 85 |
|
| 86 |
switch (err) { |
| 87 |
case EPIPE: |
| 88 |
return AWS_IO_BROKEN_PIPE; |
| 89 |
default: |
| 90 |
return AWS_ERROR_SYS_CALL_FAILURE; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static int s_raise_posix_error(int err) { |
| 95 |
return aws_raise_error(s_translate_posix_error(err)); |
| 96 |
} |
| 97 |
|
| 98 |
AWS_IO_API int aws_open_nonblocking_posix_pipe(int pipe_fds[2]) { |
| 99 |
int err; |
| 100 |
|
| 101 |
#if HAVE_PIPE2 |
| 102 |
err = pipe2(pipe_fds, O_NONBLOCK | O_CLOEXEC); |
| 103 |
if (err) { |
| 104 |
return s_raise_posix_error(err); |
| 105 |
} |
| 106 |
|
| 107 |
return AWS_OP_SUCCESS; |
| 108 |
#else |
| 109 |
err = pipe(pipe_fds); |
| 110 |
if (err) { |
| 111 |
return s_raise_posix_error(err); |
| 112 |
} |
| 113 |
|
| 114 |
for (int i = 0; i < 2; ++i) { |
| 115 |
int flags = fcntl(pipe_fds[i], F_GETFL); |
| 116 |
if (flags == -1) { |
| 117 |
s_raise_posix_error(err); |
| 118 |
goto error; |
| 119 |
} |
| 120 |
|
| 121 |
flags |= O_NONBLOCK | O_CLOEXEC; |
| 122 |
if (fcntl(pipe_fds[i], F_SETFL, flags) == -1) { |
| 123 |
s_raise_posix_error(err); |
| 124 |
goto error; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return AWS_OP_SUCCESS; |
| 129 |
error: |
| 130 |
close(pipe_fds[0]); |
| 131 |
close(pipe_fds[1]); |
| 132 |
return AWS_OP_ERR; |
| 133 |
#endif |
| 134 |
} |
| 135 |
|
| 136 |
int aws_pipe_init( |
| 137 |
struct aws_pipe_read_end *read_end, |
| 138 |
struct aws_event_loop *read_end_event_loop, |
| 139 |
struct aws_pipe_write_end *write_end, |
| 140 |
struct aws_event_loop *write_end_event_loop, |
| 141 |
struct aws_allocator *allocator) { |
| 142 |
|
| 143 |
AWS_ASSERT(read_end); |
| 144 |
AWS_ASSERT(read_end_event_loop); |
| 145 |
AWS_ASSERT(write_end); |
| 146 |
AWS_ASSERT(write_end_event_loop); |
| 147 |
AWS_ASSERT(allocator); |
| 148 |
|
| 149 |
AWS_ZERO_STRUCT(*read_end); |
| 150 |
AWS_ZERO_STRUCT(*write_end); |
| 151 |
|
| 152 |
struct read_end_impl *read_impl = NULL; |
| 153 |
struct write_end_impl *write_impl = NULL; |
| 154 |
int err; |
| 155 |
|
| 156 |
|
| 157 |
int pipe_fds[2]; |
| 158 |
err = aws_open_nonblocking_posix_pipe(pipe_fds); |
| 159 |
if (err) { |
| 160 |
return AWS_OP_ERR; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
read_impl = aws_mem_calloc(allocator, 1, sizeof(struct read_end_impl)); |
| 165 |
if (!read_impl) { |
| 166 |
goto error; |
| 167 |
} |
| 168 |
|
| 169 |
read_impl->alloc = allocator; |
| 170 |
read_impl->handle.data.fd = pipe_fds[0]; |
| 171 |
read_impl->event_loop = read_end_event_loop; |
| 172 |
|
| 173 |
|
| 174 |
write_impl = aws_mem_calloc(allocator, 1, sizeof(struct write_end_impl)); |
| 175 |
if (!write_impl) { |
| 176 |
goto error; |
| 177 |
} |
| 178 |
|
| 179 |
write_impl->alloc = allocator; |
| 180 |
write_impl->handle.data.fd = pipe_fds[1]; |
| 181 |
write_impl->event_loop = write_end_event_loop; |
| 182 |
write_impl->is_writable = true; |
| 183 |
aws_linked_list_init(&write_impl->write_list); |
| 184 |
|
| 185 |
read_end->impl_data = read_impl; |
| 186 |
write_end->impl_data = write_impl; |
| 187 |
|
| 188 |
err = aws_event_loop_subscribe_to_io_events( |
| 189 |
write_end_event_loop, &write_impl->handle, AWS_IO_EVENT_TYPE_WRITABLE, s_write_end_on_event, write_end); |
| 190 |
if (err) { |
| 191 |
goto error; |
| 192 |
} |
| 193 |
|
| 194 |
return AWS_OP_SUCCESS; |
| 195 |
|
| 196 |
error: |
| 197 |
close(pipe_fds[0]); |
| 198 |
close(pipe_fds[1]); |
| 199 |
|
| 200 |
if (read_impl) { |
| 201 |
aws_mem_release(allocator, read_impl); |
| 202 |
} |
| 203 |
|
| 204 |
if (write_impl) { |
| 205 |
aws_mem_release(allocator, write_impl); |
| 206 |
} |
| 207 |
|
| 208 |
read_end->impl_data = NULL; |
| 209 |
write_end->impl_data = NULL; |
| 210 |
|
| 211 |
return AWS_OP_ERR; |
| 212 |
} |
| 213 |
|
| 214 |
int aws_pipe_clean_up_read_end(struct aws_pipe_read_end *read_end) { |
| 215 |
struct read_end_impl *read_impl = read_end->impl_data; |
| 216 |
if (!read_impl) { |
| 217 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 218 |
} |
| 219 |
|
| 220 |
if (!aws_event_loop_thread_is_callers_thread(read_impl->event_loop)) { |
| 221 |
return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY); |
| 222 |
} |
| 223 |
|
| 224 |
if (read_impl->is_subscribed) { |
| 225 |
int err = aws_pipe_unsubscribe_from_readable_events(read_end); |
| 226 |
if (err) { |
| 227 |
return AWS_OP_ERR; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if (read_impl->did_user_callback_clean_up_read_end) { |
| 233 |
*read_impl->did_user_callback_clean_up_read_end = true; |
| 234 |
} |
| 235 |
|
| 236 |
close(read_impl->handle.data.fd); |
| 237 |
|
| 238 |
aws_mem_release(read_impl->alloc, read_impl); |
| 239 |
AWS_ZERO_STRUCT(*read_end); |
| 240 |
return AWS_OP_SUCCESS; |
| 241 |
} |
| 242 |
|
| 243 |
struct aws_event_loop *aws_pipe_get_read_end_event_loop(const struct aws_pipe_read_end *read_end) { |
| 244 |
const struct read_end_impl *read_impl = read_end->impl_data; |
| 245 |
if (!read_impl) { |
| 246 |
aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
|
| 250 |
return read_impl->event_loop; |
| 251 |
} |
| 252 |
|
| 253 |
struct aws_event_loop *aws_pipe_get_write_end_event_loop(const struct aws_pipe_write_end *write_end) { |
| 254 |
const struct write_end_impl *write_impl = write_end->impl_data; |
| 255 |
if (!write_impl) { |
| 256 |
aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
return write_impl->event_loop; |
| 261 |
} |
| 262 |
|
| 263 |
int aws_pipe_read(struct aws_pipe_read_end *read_end, struct aws_byte_buf *dst_buffer, size_t *num_bytes_read) { |
| 264 |
AWS_ASSERT(dst_buffer && dst_buffer->buffer); |
| 265 |
|
| 266 |
struct read_end_impl *read_impl = read_end->impl_data; |
| 267 |
if (!read_impl) { |
| 268 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 269 |
} |
| 270 |
|
| 271 |
if (num_bytes_read) { |
| 272 |
*num_bytes_read = 0; |
| 273 |
} |
| 274 |
|
| 275 |
size_t num_bytes_to_read = dst_buffer->capacity - dst_buffer->len; |
| 276 |
|
| 277 |
ssize_t read_val = read(read_impl->handle.data.fd, dst_buffer->buffer + dst_buffer->len, num_bytes_to_read); |
| 278 |
|
| 279 |
if (read_val < 0) { |
| 280 |
int errno_value = errno; |
| 281 |
if (errno_value == EAGAIN || errno_value == EWOULDBLOCK) { |
| 282 |
return aws_raise_error(AWS_IO_READ_WOULD_BLOCK); |
| 283 |
} |
| 284 |
return s_raise_posix_error(errno_value); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
dst_buffer->len += read_val; |
| 289 |
|
| 290 |
if (num_bytes_read) { |
| 291 |
*num_bytes_read = read_val; |
| 292 |
} |
| 293 |
|
| 294 |
return AWS_OP_SUCCESS; |
| 295 |
} |
| 296 |
|
| 297 |
static void s_read_end_on_event( |
| 298 |
struct aws_event_loop *event_loop, |
| 299 |
struct aws_io_handle *handle, |
| 300 |
int events, |
| 301 |
void *user_data) { |
| 302 |
|
| 303 |
(void)event_loop; |
| 304 |
(void)handle; |
| 305 |
|
| 306 |
|
| 307 |
struct aws_pipe_read_end *read_end = user_data; |
| 308 |
struct read_end_impl *read_impl = read_end->impl_data; |
| 309 |
AWS_ASSERT(read_impl); |
| 310 |
AWS_ASSERT(read_impl->event_loop == event_loop); |
| 311 |
AWS_ASSERT(&read_impl->handle == handle); |
| 312 |
AWS_ASSERT(read_impl->is_subscribed); |
| 313 |
AWS_ASSERT(events != 0); |
| 314 |
AWS_ASSERT(read_impl->did_user_callback_clean_up_read_end == NULL); |
| 315 |
|
| 316 |
|
| 317 |
bool did_user_callback_clean_up_read_end = false; |
| 318 |
read_impl->did_user_callback_clean_up_read_end = &did_user_callback_clean_up_read_end; |
| 319 |
|
| 320 |
|
| 321 |
if (events & AWS_IO_EVENT_TYPE_READABLE) { |
| 322 |
read_impl->on_readable_user_callback(read_end, AWS_ERROR_SUCCESS, read_impl->on_readable_user_data); |
| 323 |
|
| 324 |
if (did_user_callback_clean_up_read_end) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
events &= ~AWS_IO_EVENT_TYPE_READABLE; |
| 329 |
} |
| 330 |
|
| 331 |
if (events) { |
| 332 |
|
| 333 |
if (read_impl->is_subscribed) { |
| 334 |
read_impl->on_readable_user_callback(read_end, AWS_IO_BROKEN_PIPE, read_impl->on_readable_user_data); |
| 335 |
|
| 336 |
if (did_user_callback_clean_up_read_end) { |
| 337 |
return; |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
read_impl->did_user_callback_clean_up_read_end = NULL; |
| 343 |
} |
| 344 |
|
| 345 |
int aws_pipe_subscribe_to_readable_events( |
| 346 |
struct aws_pipe_read_end *read_end, |
| 347 |
aws_pipe_on_readable_fn *on_readable, |
| 348 |
void *user_data) { |
| 349 |
|
| 350 |
AWS_ASSERT(on_readable); |
| 351 |
|
| 352 |
struct read_end_impl *read_impl = read_end->impl_data; |
| 353 |
if (!read_impl) { |
| 354 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 355 |
} |
| 356 |
|
| 357 |
if (!aws_event_loop_thread_is_callers_thread(read_impl->event_loop)) { |
| 358 |
return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY); |
| 359 |
} |
| 360 |
|
| 361 |
if (read_impl->is_subscribed) { |
| 362 |
return aws_raise_error(AWS_ERROR_IO_ALREADY_SUBSCRIBED); |
| 363 |
} |
| 364 |
|
| 365 |
read_impl->is_subscribed = true; |
| 366 |
read_impl->on_readable_user_callback = on_readable; |
| 367 |
read_impl->on_readable_user_data = user_data; |
| 368 |
|
| 369 |
int err = aws_event_loop_subscribe_to_io_events( |
| 370 |
read_impl->event_loop, &read_impl->handle, AWS_IO_EVENT_TYPE_READABLE, s_read_end_on_event, read_end); |
| 371 |
if (err) { |
| 372 |
read_impl->is_subscribed = false; |
| 373 |
read_impl->on_readable_user_callback = NULL; |
| 374 |
read_impl->on_readable_user_data = NULL; |
| 375 |
|
| 376 |
return AWS_OP_ERR; |
| 377 |
} |
| 378 |
|
| 379 |
return AWS_OP_SUCCESS; |
| 380 |
} |
| 381 |
|
| 382 |
int aws_pipe_unsubscribe_from_readable_events(struct aws_pipe_read_end *read_end) { |
| 383 |
struct read_end_impl *read_impl = read_end->impl_data; |
| 384 |
if (!read_impl) { |
| 385 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 386 |
} |
| 387 |
|
| 388 |
if (!aws_event_loop_thread_is_callers_thread(read_impl->event_loop)) { |
| 389 |
return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY); |
| 390 |
} |
| 391 |
|
| 392 |
if (!read_impl->is_subscribed) { |
| 393 |
return aws_raise_error(AWS_ERROR_IO_NOT_SUBSCRIBED); |
| 394 |
} |
| 395 |
|
| 396 |
int err = aws_event_loop_unsubscribe_from_io_events(read_impl->event_loop, &read_impl->handle); |
| 397 |
if (err) { |
| 398 |
return AWS_OP_ERR; |
| 399 |
} |
| 400 |
|
| 401 |
read_impl->is_subscribed = false; |
| 402 |
read_impl->on_readable_user_callback = NULL; |
| 403 |
read_impl->on_readable_user_data = NULL; |
| 404 |
|
| 405 |
return AWS_OP_SUCCESS; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
static bool s_write_end_complete_front_write_request(struct aws_pipe_write_end *write_end, int error_code) { |
| 411 |
struct write_end_impl *write_impl = write_end->impl_data; |
| 412 |
|
| 413 |
AWS_ASSERT(!aws_linked_list_empty(&write_impl->write_list)); |
| 414 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&write_impl->write_list); |
| 415 |
struct pipe_write_request *request = AWS_CONTAINER_OF(node, struct pipe_write_request, list_node); |
| 416 |
|
| 417 |
struct aws_allocator *alloc = write_impl->alloc; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
bool write_end_cleaned_up_during_callback = false; |
| 422 |
struct pipe_write_request *prev_invoking_request = write_impl->currently_invoking_write_callback; |
| 423 |
write_impl->currently_invoking_write_callback = request; |
| 424 |
|
| 425 |
if (request->user_callback) { |
| 426 |
request->user_callback(write_end, error_code, request->original_cursor, request->user_data); |
| 427 |
write_end_cleaned_up_during_callback = request->did_user_callback_clean_up_write_end; |
| 428 |
} |
| 429 |
|
| 430 |
if (!write_end_cleaned_up_during_callback) { |
| 431 |
write_impl->currently_invoking_write_callback = prev_invoking_request; |
| 432 |
} |
| 433 |
|
| 434 |
aws_mem_release(alloc, request); |
| 435 |
|
| 436 |
return write_end_cleaned_up_during_callback; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
static void s_write_end_process_requests(struct aws_pipe_write_end *write_end) { |
| 441 |
struct write_end_impl *write_impl = write_end->impl_data; |
| 442 |
AWS_ASSERT(write_impl); |
| 443 |
|
| 444 |
while (!aws_linked_list_empty(&write_impl->write_list)) { |
| 445 |
struct aws_linked_list_node *node = aws_linked_list_front(&write_impl->write_list); |
| 446 |
struct pipe_write_request *request = AWS_CONTAINER_OF(node, struct pipe_write_request, list_node); |
| 447 |
|
| 448 |
int completed_error_code = AWS_ERROR_SUCCESS; |
| 449 |
|
| 450 |
if (request->cursor.len > 0) { |
| 451 |
ssize_t write_val = write(write_impl->handle.data.fd, request->cursor.ptr, request->cursor.len); |
| 452 |
|
| 453 |
if (write_val < 0) { |
| 454 |
int errno_value = errno; |
| 455 |
if (errno_value == EAGAIN || errno_value == EWOULDBLOCK) { |
| 456 |
|
| 457 |
write_impl->is_writable = false; |
| 458 |
return; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
completed_error_code = s_translate_posix_error(errno_value); |
| 463 |
|
| 464 |
} else { |
| 465 |
aws_byte_cursor_advance(&request->cursor, write_val); |
| 466 |
|
| 467 |
if (request->cursor.len > 0) { |
| 468 |
|
| 469 |
continue; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
bool write_end_cleaned_up = s_write_end_complete_front_write_request(write_end, completed_error_code); |
| 477 |
if (write_end_cleaned_up) { |
| 478 |
|
| 479 |
return; |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
static void s_write_end_on_event( |
| 486 |
struct aws_event_loop *event_loop, |
| 487 |
struct aws_io_handle *handle, |
| 488 |
int events, |
| 489 |
void *user_data) { |
| 490 |
|
| 491 |
(void)event_loop; |
| 492 |
(void)handle; |
| 493 |
|
| 494 |
|
| 495 |
struct aws_pipe_write_end *write_end = user_data; |
| 496 |
struct write_end_impl *write_impl = write_end->impl_data; |
| 497 |
AWS_ASSERT(write_impl); |
| 498 |
AWS_ASSERT(write_impl->event_loop == event_loop); |
| 499 |
AWS_ASSERT(&write_impl->handle == handle); |
| 500 |
|
| 501 |
|
| 502 |
if ((events & AWS_IO_EVENT_TYPE_WRITABLE) == 0) { |
| 503 |
return; |
| 504 |
} |
| 505 |
|
| 506 |
write_impl->is_writable = true; |
| 507 |
|
| 508 |
s_write_end_process_requests(write_end); |
| 509 |
} |
| 510 |
|
| 511 |
int aws_pipe_write( |
| 512 |
struct aws_pipe_write_end *write_end, |
| 513 |
struct aws_byte_cursor src_buffer, |
| 514 |
aws_pipe_on_write_completed_fn *on_completed, |
| 515 |
void *user_data) { |
| 516 |
|
| 517 |
AWS_ASSERT(src_buffer.ptr); |
| 518 |
|
| 519 |
struct write_end_impl *write_impl = write_end->impl_data; |
| 520 |
if (!write_impl) { |
| 521 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 522 |
} |
| 523 |
|
| 524 |
if (!aws_event_loop_thread_is_callers_thread(write_impl->event_loop)) { |
| 525 |
return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY); |
| 526 |
} |
| 527 |
|
| 528 |
struct pipe_write_request *request = aws_mem_calloc(write_impl->alloc, 1, sizeof(struct pipe_write_request)); |
| 529 |
if (!request) { |
| 530 |
return AWS_OP_ERR; |
| 531 |
} |
| 532 |
|
| 533 |
request->original_cursor = src_buffer; |
| 534 |
request->cursor = src_buffer; |
| 535 |
request->user_callback = on_completed; |
| 536 |
request->user_data = user_data; |
| 537 |
|
| 538 |
aws_linked_list_push_back(&write_impl->write_list, &request->list_node); |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
if (write_impl->is_writable && !write_impl->currently_invoking_write_callback) { |
| 543 |
s_write_end_process_requests(write_end); |
| 544 |
} |
| 545 |
|
| 546 |
return AWS_OP_SUCCESS; |
| 547 |
} |
| 548 |
|
| 549 |
int aws_pipe_clean_up_write_end(struct aws_pipe_write_end *write_end) { |
| 550 |
struct write_end_impl *write_impl = write_end->impl_data; |
| 551 |
if (!write_impl) { |
| 552 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 553 |
} |
| 554 |
|
| 555 |
if (!aws_event_loop_thread_is_callers_thread(write_impl->event_loop)) { |
| 556 |
return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY); |
| 557 |
} |
| 558 |
|
| 559 |
int err = aws_event_loop_unsubscribe_from_io_events(write_impl->event_loop, &write_impl->handle); |
| 560 |
if (err) { |
| 561 |
return AWS_OP_ERR; |
| 562 |
} |
| 563 |
|
| 564 |
close(write_impl->handle.data.fd); |
| 565 |
|
| 566 |
|
| 567 |
AWS_ZERO_STRUCT(*write_end); |
| 568 |
|
| 569 |
|
| 570 |
if (write_impl->currently_invoking_write_callback) { |
| 571 |
write_impl->currently_invoking_write_callback->did_user_callback_clean_up_write_end = true; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
while (!aws_linked_list_empty(&write_impl->write_list)) { |
| 576 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&write_impl->write_list); |
| 577 |
struct pipe_write_request *request = AWS_CONTAINER_OF(node, struct pipe_write_request, list_node); |
| 578 |
if (request->user_callback) { |
| 579 |
request->user_callback(NULL, AWS_IO_BROKEN_PIPE, request->original_cursor, request->user_data); |
| 580 |
} |
| 581 |
aws_mem_release(write_impl->alloc, request); |
| 582 |
} |
| 583 |
|
| 584 |
aws_mem_release(write_impl->alloc, write_impl); |
| 585 |
return AWS_OP_SUCCESS; |
| 586 |
} |