ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
sync.h File Reference

Go to the source code of this file.

Functions

asciichat_error_t ui_sync_start (int fd)
void ui_sync_stop (void)
bool ui_sync_is_visible (void)
keyboard_key_t ui_sync_handle_key (keyboard_key_t key)
void ui_sync_render (terminal_size_t size)
int ui_sync_output_fd (void)

Function Documentation

◆ ui_sync_handle_key()

keyboard_key_t ui_sync_handle_key ( keyboard_key_t key)

Definition at line 388 of file sync.c.

388 {
389 if (key == KEY_0) {
392 return KEY_NONE;
393 }
394 if (!ui_sync_is_visible())
395 return key;
396 // Leave sync selected until the replacement has a complete snapshot.
397 if (key == '=' || key == KEY_BACKTICK || key == KEY_QUESTION || key == ']' ||
398 key == 'o' || key == 'O' || key == 15)
399 return key;
400 if (key == KEY_ESCAPE) {
402 return KEY_NONE;
403 }
404 ui_viewport_t viewport = {.selected = (size_t)atomic_load(&g_sync_selected),
405 .page_size =
406 (size_t)atomic_load(&g_sync_page_size)};
407 ui_viewport_key(&viewport, (size_t)atomic_load(&g_sync_total),
408 viewport.page_size, key);
409 atomic_store(&g_sync_selected, (int)viewport.selected);
410 return key == 'q' || key == 3 ? key : KEY_NONE;
411}
For ui viewport, this is the live record that ties selected, first_visible, page_size together....
Definition viewport.h:20
size_t selected
Definition viewport.h:21
size_t page_size
Definition viewport.h:21
bool ui_sync_is_visible(void)
Definition sync.c:383
void ui_controller_select_interactive(int screen)
@ UI_SCREEN_SYNC
bool ui_viewport_key(ui_viewport_t *v, size_t count, size_t rows, keyboard_key_t key)
Definition viewport.c:40

References ui_viewport_t::page_size, ui_viewport_t::selected, ui_controller_select_interactive(), UI_SCREEN_SYNC, ui_sync_is_visible(), and ui_viewport_key().

Referenced by ui_input_poll_sync(), and ui_input_read_key().

◆ ui_sync_is_visible()

bool ui_sync_is_visible ( void )

Definition at line 383 of file sync.c.

383 {
385}
int ui_controller_interactive_screen(void)

References ui_controller_interactive_screen(), and UI_SCREEN_SYNC.

Referenced by ui_sync_handle_key(), and ui_sync_stop().

◆ ui_sync_output_fd()

int ui_sync_output_fd ( void )

Definition at line 386 of file sync.c.

386{ return g_sync_fd; }

Referenced by ui_sync_render().

◆ ui_sync_render()

void ui_sync_render ( terminal_size_t size)

Called only by the presentation thread; uses already-owned snapshots.

Definition at line 517 of file sync.c.

517 {
519 : STDOUT_FILENO);
520 terminal_capabilities_t caps =
522 terminal_color_mode_t mode = GET_OPTION(color_mode);
523 g_sync_use_color =
524 mode != TERM_COLOR_NONE && GET_OPTION(color) != COLOR_SETTING_FALSE &&
525 !GET_OPTION(strip_ansi) &&
526 (mode != TERM_COLOR_AUTO || caps.color_level != TERM_COLOR_NONE);
527 g_sync_use_utf8 = terminal_should_use_utf8(GET_OPTION(force_utf8));
528 // Copy only the visible page before terminal writes; the collector never
529 // waits for output, and the renderer never waits for collection.
531 size_t count = 0, total = 0, cycles = 0;
532 size_t mutexes = 0, conditions = 0, rwlocks = 0, atomics = 0;
533 uint64_t sampled = 0;
534 bool waits_complete = false, limited = false;
535 enum { header_rows = 4, detail_rows = 3, footer_rows = 1 };
536 const int fixed_rows = header_rows + detail_rows + footer_rows;
537 int name_width = (int)strlen("Name");
538 ui_layout_t layout;
539 if (ui_layout_measure(size, header_rows, detail_rows, footer_rows, &layout) !=
541 return;
542 int per_page = layout.body_rows > 0 ? layout.body_rows : 1;
543 if (per_page > 128)
544 per_page = 128;
545 atomic_store(&g_sync_page_size, per_page);
546 int selected = atomic_load(&g_sync_selected);
547 int page = 0, pages = 1;
548 int slot = atomic_load(&g_sync_published);
549 if (slot >= 0) {
550 // Keep the last complete screen if collection is reusing this mailbox.
551 if (atomic_flag_test_and_set(&g_sync_samples[slot].busy)) {
552 return;
553 }
554 sync_sample_t *s = &g_sync_samples[slot];
555 total = s->count;
556 atomic_store(&g_sync_total, total > INT_MAX ? INT_MAX : (int)total);
557 if ((size_t)selected >= total) {
558 int previous = selected;
559 selected = total ? (int)total - 1 : 0;
560 // Do not overwrite a key received while the renderer copies a sample.
561 atomic_compare_exchange_strong(&g_sync_selected, &previous, selected);
562 }
563 pages = total ? (int)((total + per_page - 1) / per_page) : 1;
564 page = selected / per_page;
565 size_t first = (size_t)page * per_page;
566 count = total > first ? total - first : 0;
567 if (count > (size_t)per_page)
568 count = per_page;
569 if (count)
570 memcpy(rows, s->rows + first, count * sizeof(*rows));
571 for (size_t i = 0; i < total; ++i) {
572 cycles += s->rows[i].cycle;
573 const char *type = s->rows[i].type;
574 mutexes += !strcmp(type, "mutex");
575 conditions += !strcmp(type, "cond");
576 rwlocks += !strcmp(type, "rwlock");
577 atomics += !strncmp(type, "atomic", 6);
578 char label[TUI_SYNC_LABEL_BUFFER_SIZE];
579 sync_row_label(&s->rows[i], label, sizeof(label));
580 int width = (int)strlen(label);
581 if (width > name_width)
582 name_width = width;
583 }
584 sampled = s->sampled_ns;
585 waits_complete = s->waits_complete;
586 limited = s->limited;
587 atomic_flag_clear(&s->busy);
588 }
589 if (name_width > 30)
590 name_width = 30;
591 uint64_t now = time_get_ns();
592 selected %= per_page;
593 if ((size_t)selected >= count)
594 selected = count ? (int)count - 1 : 0;
595 char deadlock[TUI_SYNC_DEADLOCK_BUFFER_SIZE];
596 snprintf(deadlock, sizeof(deadlock), "%s%s%s", sync_color(LOG_COLOR_ERROR),
597 g_sync_use_utf8 ? "DEADLOCK ❌" : "DEADLOCK!",
598 sync_color(LOG_COLOR_RESET));
599 int measured_width = 0;
600 for (int pass = 0; pass < 2; ++pass) {
601 int *measure = pass == 0 ? &measured_width : NULL;
603 if (!measure)
604 sync_write("\033[?2026h\033[H", 11);
605 snprintf(
606 line, sizeof(line),
607 "Sync primitives | %zu objects | page %d/%d | item %zu/%zu | age %.1fs",
608 total, page + 1, pages,
609 count ? (size_t)page * per_page + selected + 1 : 0, total,
610 sampled ? (double)(now - sampled) / NS_PER_SEC_INT : 0.0);
611 sync_write_numbers(line, size.cols, measure);
612 snprintf(line, sizeof(line),
613 "%s%s | M=%zu RW=%zu C=%zu A=%zu | %zu mutexes in wait cycles%s",
614 cycles ? sync_color(LOG_COLOR_ERROR) : "",
615 !sampled ? "Waiting for registry"
616 : now - sampled > NS_PER_SEC_INT ? "STALE: collection unavailable"
617 : "Live registered",
618 mutexes, rwlocks, conditions, atomics, cycles,
619 !waits_complete ? " | wait tracker busy"
620 : limited ? " | wait tracker capacity reached"
621 : "");
622 sync_write_numbers(line, size.cols, measure);
623 snprintf(line, sizeof(line), "%s %-8s %-*s %-12s %13s %6s %6s %7s %12s%s",
624 sync_color(LOG_COLOR_GREY), "Type", name_width, "Name",
625 "State/value", "Total Actions", "L/s", "U/s", "Change/s",
626 "Total Wait", sync_color(LOG_COLOR_RESET));
627 sync_write_line(line, size.cols, true, measure);
628 sync_separator(size.cols, measure);
629 for (size_t i = 0; i < count; ++i) {
630 sync_row_t *r = &rows[i];
631 // Parent names may be media paths; keep the primitive visible in the
632 // table. The detail row identifies the source location and address.
634 sync_row_label(r, name, sizeof(name));
635 if (strlen(name) > (size_t)name_width) {
636 const char *suffix = strrchr(name, '.');
637 size_t suffix_bytes =
638 suffix && strspn(suffix + 1, "0123456789") == strlen(suffix + 1)
639 ? strlen(suffix)
640 : 0;
641 if (suffix_bytes >= (size_t)name_width - 1)
642 suffix_bytes = 0;
643 size_t used = 0;
644 int columns = 0;
645 while (name[used]) {
646 int bytes = utf8_next_char_bytes(name + used, strlen(name + used));
647 if (bytes <= 0)
648 break;
649 int width = utf8_display_width_n(name + used, bytes);
650 if (columns + width > name_width - 1 - (int)suffix_bytes)
651 break;
652 used += bytes;
653 columns += width;
654 }
655 name[used++] = '~';
656 if (suffix_bytes) {
657 memmove(name + used, suffix, suffix_bytes);
658 used += suffix_bytes;
659 }
660 name[used] = '\0';
661 }
662 char state[TUI_SYNC_STATE_BUFFER_SIZE];
663 if (r->cycle)
664 snprintf(state, sizeof(state), "DEADLOCK");
665 else if (!strcmp(r->type, "mutex"))
666 snprintf(state, sizeof(state), "%s t=%" PRIxPTR " w=%u",
667 r->cycle ? "CYCLE"
668 : r->owner ? "HELD"
669 : "FREE",
670 r->owner, r->waiters);
671 else if (!strcmp(r->type, "rwlock"))
672 snprintf(state, sizeof(state), "W=%" PRIxPTR " R=%" PRIu64, r->owner,
673 r->value);
674 else if (!strcmp(r->type, "cond"))
675 snprintf(state, sizeof(state), "waiting=%" PRIu64, r->value);
676 else
677 snprintf(state, sizeof(state), "0x%" PRIx64 " (%" PRIu64 ")", r->value,
678 r->value);
679 // Prefer the complete hex value over a clipped redundant decimal copy.
680 if (!strncmp(r->type, "atomic", 6) && strlen(state) > 12)
681 snprintf(state, sizeof(state), "0x%" PRIx64, r->value);
682 char padded_state[TUI_SYNC_PADDED_STATE_BUFFER_SIZE];
683 if (r->cycle) {
684 int width =
685 utf8_display_width(g_sync_use_utf8 ? "DEADLOCK ❌" : "DEADLOCK!");
686 snprintf(padded_state, sizeof(padded_state), "%s%*s", deadlock,
687 12 - width, "");
688 } else {
689 snprintf(padded_state, sizeof(padded_state), "%-12.12s", state);
690 }
691 char plain_actions[TUI_SYNC_PLAIN_ACTIONS_BUFFER_SIZE],
693 format_number_compact(r->actions, plain_actions, sizeof(plain_actions));
694 char padded_actions[TUI_SYNC_PADDED_ACTIONS_BUFFER_SIZE];
695 snprintf(padded_actions, sizeof(padded_actions), "%13s", plain_actions);
696 sync_color_numbers(padded_actions, actions, sizeof(actions));
697 char changes[TUI_SYNC_CHANGES_BUFFER_SIZE];
698 if (!strncmp(r->type, "atomic", 6))
699 snprintf(changes, sizeof(changes), "%s%7.1f",
700 sync_rate_color(r->change_rate), r->change_rate);
701 else
702 snprintf(changes, sizeof(changes), "%s%7s", sync_color(LOG_COLOR_RESET),
703 "-");
704 char wait[TIME_DURATION_BUFFER_SIZE] = "-";
705 if (!strcmp(r->type, "cond")) {
706 if (r->wait_known)
707 time_pretty(r->wait_ns, -1, wait, sizeof(wait));
708 else
709 snprintf(wait, sizeof(wait), "busy");
710 }
711 char padded_wait[TUI_SYNC_PADDED_WAIT_BUFFER_SIZE],
713 snprintf(padded_wait, sizeof(padded_wait), "%12s", wait);
714 sync_color_numbers(padded_wait, colored_wait, sizeof(colored_wait));
715 char locks[TUI_SYNC_LOCKS_BUFFER_SIZE],
717 snprintf(locks, sizeof(locks), "%s%6.1f%s", sync_rate_color(r->lock_rate),
718 r->lock_rate, sync_color(LOG_COLOR_RESET));
719 snprintf(unlocks, sizeof(unlocks), "%s%6.1f%s",
720 sync_rate_color(r->unlock_rate), r->unlock_rate,
721 sync_color(LOG_COLOR_RESET));
722 const ui_table_column_t columns[] = {
723 {"Type", 8, UI_ALIGN_LEFT, false},
724 {"Name", name_width, UI_ALIGN_LEFT, false},
725 {"State/value", 13, UI_ALIGN_LEFT, false},
726 {"Total Actions", 13, UI_ALIGN_RIGHT, false},
727 {"L/s", 6, UI_ALIGN_RIGHT, false},
728 {"U/s", 6, UI_ALIGN_RIGHT, false},
729 {"Change/s", 7, UI_ALIGN_RIGHT, false},
730 {"Total Wait", 12, UI_ALIGN_RIGHT, false}};
731 const char *cells[] = {r->type, name, padded_state, actions,
732 locks, unlocks, changes, colored_wait};
733 ui_table_row(line, sizeof(line), columns, cells, 8, name_width + 83, true,
734 (int)i == selected);
735 sync_write_line(line, size.cols, true, measure);
736 }
737 if (!measure) {
738 // Clear only unused table rows, leaving the live footer in place.
739 for (int row = header_rows + (int)count + 1; row < size.rows - 3; ++row) {
740 snprintf(line, sizeof(line), "\033[%d;1H\033[K", row);
741 sync_write(line, strlen(line));
742 }
743 snprintf(line, sizeof(line), "\033[%d;1H",
744 size.rows > 3 ? size.rows - 3 : 1);
745 sync_write(line, strlen(line));
746 }
747 sync_separator(size.cols, measure);
748 if (count) {
749 sync_row_t *r = &rows[selected];
750 snprintf(line, sizeof(line), "%d @0x%" PRIxPTR, r->line, r->key);
751 char numbers[TUI_SYNC_NUMBERS_BUFFER_SIZE];
752 sync_color_numbers(line, numbers, sizeof(numbers));
753 char full_name[TUI_SYNC_FULL_NAME_BUFFER_SIZE],
755 sync_row_label(r, full_name, sizeof(full_name));
756 sync_color_numbers(full_name, colored_name, sizeof(colored_name));
757 snprintf(line, sizeof(line), "%.*s%.*s%.*s:%.*s | %.*s",
758 (int)(sizeof(line) / 5), sync_color(LOG_COLOR_DEV),
759 (int)(sizeof(line) / 5), r->file, (int)(sizeof(line) / 5),
760 sync_color(LOG_COLOR_RESET), (int)(sizeof(line) / 5), numbers,
761 (int)(sizeof(line) / 5), colored_name);
762 sync_write_line(line, size.cols, true, measure);
763 snprintf(line, sizeof(line),
764 "owner=0x%" PRIxPTR " waiter=0x%" PRIxPTR " | value=%" PRIu64
765 " | last operation %.2fs ago%s%s",
766 r->owner, r->waiter, r->value,
767 r->last_ns ? (double)(now - r->last_ns) / NS_PER_SEC_INT : 0.0,
768 r->cycle ? " " : "", r->cycle ? deadlock : "");
769 sync_write_numbers(line, size.cols, measure);
770 } else if (!measure) {
771 snprintf(line, sizeof(line), "\033[%d;1H\033[K\033[%d;1H\033[K",
772 size.rows - 2, size.rows - 1);
773 sync_write(line, strlen(line));
774 }
775 snprintf(line, sizeof(line), "\033[%d;1H", size.rows);
776 if (!measure)
777 sync_write(line, strlen(line));
778 snprintf(line, sizeof(line),
779 "%s0%s/%sEsc%s close | %s?%s help | %sLeft%s/%sRight%s page | "
780 "%sUp%s/%sDown%s select | "
781 "%sHome%s/%sEnd%s first/last",
782 sync_color(LOG_COLOR_FATAL), sync_color(LOG_COLOR_RESET),
783 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET),
784 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET),
785 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET),
786 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET),
787 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET),
788 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET),
789 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET),
790 sync_color(LOG_COLOR_WARN), sync_color(LOG_COLOR_RESET));
791 sync_write_line(line, size.cols, false, measure);
792 if (measure) {
793 terminal_size_t minimum = {.cols = measured_width + 1,
794 .rows = fixed_rows + 1};
795 if (ui_too_small(size, minimum)) {
797 size_t length =
798 ui_too_small_format(output, sizeof(output), size, minimum);
799 ui_controller_write(g_sync_fd, output, length);
800 return;
801 }
802 }
803 }
804 sync_write("\033[?2026l", 8);
805 sync_flush();
806}
@ LOG_COLOR_RESET
Definition color.h:14
@ LOG_COLOR_GREY
Definition color.h:13
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_ERROR
Definition color.h:11
@ LOG_COLOR_DEV
Definition color.h:7
@ LOG_COLOR_WARN
Definition color.h:10
int color_mode
Definition fft.c:26
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int width
Definition fft.c:24
unsigned long long uint64_t
@ ASCIICHAT_OK
Definition error_codes.h:50
#define NS_PER_SEC_INT
Definition time.h:173
int time_pretty(uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
Format nanoseconds as a compact duration with configurable sub-minute precision.
Definition time.c:406
#define TIME_DURATION_BUFFER_SIZE
Definition time.h:40
#define GET_OPTION(field)
Definition options.h:1339
#define options_terminal_settings()
Definition options.h:1287
@ COLOR_SETTING_FALSE
Force colors OFF: disable all colors and logging colors.
Definition options.h:250
int utf8_display_width_n(const char *str, size_t max_bytes)
Calculate terminal display width of a UTF-8 string up to byte limit.
Definition utf8.c:106
int colored_string_set_output_fd(int fd)
int utf8_display_width(const char *str)
Calculate terminal display width of a UTF-8 string.
Definition utf8.c:102
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:183
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
char type[24]
Definition sync.c:69
uint64_t actions
Definition sync.c:66
double unlock_rate
Definition sync.c:68
unsigned waiters
Definition sync.c:72
bool cycle
Definition sync.c:73
uintptr_t key
Definition sync.c:65
int line
Definition sync.c:71
char file[160]
Definition sync.c:70
uint64_t wait_ns
Definition sync.c:67
uint64_t value
Definition sync.c:66
uintptr_t owner
Definition sync.c:65
double change_rate
Definition sync.c:68
uint64_t last_ns
Definition sync.c:66
uintptr_t waiter
Definition sync.c:65
double lock_rate
Definition sync.c:68
bool wait_known
Definition sync.c:73
sync row keeps the values that move through the subsystem: key, owner, waiter, generation....
Definition sync.c:84
bool waits_complete
Definition sync.c:89
size_t count
Definition sync.c:87
sync_row_t * rows
Definition sync.c:86
bool limited
Definition sync.c:89
atomic_flag busy
Definition sync.c:85
uint64_t sampled_ns
Definition sync.c:88
int body_rows
Definition ui/layout.h:9
#define TUI_SYNC_STATE_BUFFER_SIZE
Definition sync.c:12
#define TUI_SYNC_FULL_NAME_BUFFER_SIZE
Definition sync.c:23
#define TUI_SYNC_DEADLOCK_BUFFER_SIZE
Definition sync.c:9
#define TUI_SYNC_ACTIONS_BUFFER_SIZE
Definition sync.c:15
#define TUI_SYNC_CHANGES_BUFFER_SIZE
Definition sync.c:17
#define TUI_SYNC_PLAIN_ACTIONS_BUFFER_SIZE
Definition sync.c:14
#define TUI_SYNC_PADDED_ACTIONS_BUFFER_SIZE
Definition sync.c:16
#define TUI_SYNC_NAME_EXTENDED_BUFFER_SIZE
Definition sync.c:11
#define SYNC_ROW_CAPACITY
Definition sync.c:29
#define TUI_SYNC_LOCKS_BUFFER_SIZE
Definition sync.c:20
#define TUI_SYNC_LABEL_BUFFER_SIZE
Definition sync.c:8
#define TUI_SYNC_COLORED_WAIT_BUFFER_SIZE
Definition sync.c:19
#define TUI_SYNC_COLORED_NAME_BUFFER_SIZE
Definition sync.c:24
#define TUI_SYNC_PADDED_WAIT_BUFFER_SIZE
Definition sync.c:18
#define TUI_SYNC_NUMBERS_BUFFER_SIZE
Definition sync.c:22
#define TUI_SYNC_UNLOCKS_BUFFER_SIZE
Definition sync.c:21
#define TUI_SYNC_OUTPUT_SHORT_BUFFER_SIZE
Definition sync.c:25
int ui_sync_output_fd(void)
Definition sync.c:386
#define TUI_SYNC_PADDED_STATE_BUFFER_SIZE
Definition sync.c:13
#define TUI_SYNC_LINE_SHORT_BUFFER_SIZE
Definition sync.c:10
size_t ui_table_row(char *out, size_t cap, const ui_table_column_t *cols, const char *const *cells, size_t count, int width, bool selectable, bool selected)
When code needs to work with ui table, it keeps columns, column_count, row_count, viewport here inste...
Definition table.c:18
@ UI_ALIGN_RIGHT
Definition text.h:14
@ UI_ALIGN_LEFT
Definition text.h:14
size_t ui_too_small_format(char *output, size_t capacity, terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:15
bool ui_too_small(terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:10
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
asciichat_error_t ui_layout_measure(terminal_size_t size, int header, int detail, int footer, ui_layout_t *out)
Definition ui/layout.c:3
void format_number_compact(uint64_t value, char *out, size_t out_capacity)
Definition util/format.c:53

References sync_row_t::actions, ASCIICHAT_OK, ui_layout_t::body_rows, sync_sample_t::busy, cells, sync_row_t::change_rate, color_mode, COLOR_SETTING_FALSE, colored_string_set_output_fd(), sync_sample_t::count, sync_row_t::cycle, detect_terminal_capabilities(), sync_row_t::file, format_number_compact(), GET_OPTION, sync_row_t::key, sync_row_t::last_ns, sync_sample_t::limited, sync_row_t::line, sync_row_t::lock_rate, LOG_COLOR_DEV, LOG_COLOR_ERROR, LOG_COLOR_FATAL, LOG_COLOR_GREY, LOG_COLOR_RESET, LOG_COLOR_WARN, NS_PER_SEC_INT, options_terminal_settings, sync_row_t::owner, sync_sample_t::rows, sync_sample_t::sampled_ns, SYNC_ROW_CAPACITY, terminal_should_use_utf8(), TIME_DURATION_BUFFER_SIZE, time_pretty(), TUI_SYNC_ACTIONS_BUFFER_SIZE, TUI_SYNC_CHANGES_BUFFER_SIZE, TUI_SYNC_COLORED_NAME_BUFFER_SIZE, TUI_SYNC_COLORED_WAIT_BUFFER_SIZE, TUI_SYNC_DEADLOCK_BUFFER_SIZE, TUI_SYNC_FULL_NAME_BUFFER_SIZE, TUI_SYNC_LABEL_BUFFER_SIZE, TUI_SYNC_LINE_SHORT_BUFFER_SIZE, TUI_SYNC_LOCKS_BUFFER_SIZE, TUI_SYNC_NAME_EXTENDED_BUFFER_SIZE, TUI_SYNC_NUMBERS_BUFFER_SIZE, TUI_SYNC_OUTPUT_SHORT_BUFFER_SIZE, TUI_SYNC_PADDED_ACTIONS_BUFFER_SIZE, TUI_SYNC_PADDED_STATE_BUFFER_SIZE, TUI_SYNC_PADDED_WAIT_BUFFER_SIZE, TUI_SYNC_PLAIN_ACTIONS_BUFFER_SIZE, TUI_SYNC_STATE_BUFFER_SIZE, TUI_SYNC_UNLOCKS_BUFFER_SIZE, sync_row_t::type, UI_ALIGN_LEFT, UI_ALIGN_RIGHT, ui_controller_write(), ui_layout_measure(), ui_sync_output_fd(), ui_table_row(), ui_too_small(), ui_too_small_format(), sync_row_t::unlock_rate, utf8_display_width(), utf8_display_width_n(), utf8_next_char_bytes(), sync_row_t::value, sync_row_t::wait_known, sync_row_t::wait_ns, sync_row_t::waiter, sync_row_t::waiters, sync_sample_t::waits_complete, and width.

◆ ui_sync_start()

asciichat_error_t ui_sync_start ( int fd)

Debug-only service. Collection and input never wait on application locks.

Definition at line 335 of file sync.c.

335 {
336 while (atomic_flag_test_and_set(&g_sync_starting))
337 platform_sleep_ms(1);
339 if (!g_sync_started && platform_isatty(fd) && platform_isatty(STDIN_FILENO)) {
340 g_sync_fd = platform_dup(fd);
341 if (g_sync_fd < 0) {
343 "Cannot retain sync display terminal");
344 } else if ((result = keyboard_init()) != ASCIICHAT_OK) {
345 platform_close(g_sync_fd);
346 g_sync_fd = -1;
347 } else {
348 atomic_store(&g_sync_stop, false);
349 result = platform_thread_create(&g_sync_thread, "sync_collector",
350 sync_collect_main, NULL);
351 if (result == ASCIICHAT_OK) {
352 g_sync_started = true;
353 } else {
355 platform_close(g_sync_fd);
356 g_sync_fd = -1;
357 result = SET_ERRNO(ERROR_THREAD, "Cannot start sync collector");
358 }
359 }
360 }
361 atomic_flag_clear(&g_sync_starting);
362 return result;
363}
#define SET_ERRNO(code,...)
#define SET_ERRNO_SYS(code,...)
int platform_close(int fd)
Definition filesystem.c:91
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_FILE_OPERATION
@ ERROR_THREAD
void keyboard_destroy(void)
Definition keyboard.c:15
int platform_isatty(int fd)
Definition util.c:70

References ASCIICHAT_OK, ERROR_FILE_OPERATION, ERROR_THREAD, keyboard_destroy(), platform_close(), platform_isatty(), SET_ERRNO, and SET_ERRNO_SYS.

Referenced by ui_controller_start_terminal().

◆ ui_sync_stop()

void ui_sync_stop ( void )

Definition at line 365 of file sync.c.

365 {
366 if (!g_sync_started)
367 return;
368 atomic_store(&g_sync_stop, true);
369 thread_join(&g_sync_thread, NULL);
371 platform_close(g_sync_fd);
372 g_sync_fd = -1;
373 for (int i = 0; i < 3; ++i) {
374 UNTRACKED_FREE(g_sync_samples[i].rows);
375 g_sync_samples[i].capacity = g_sync_samples[i].count = 0;
376 }
377 atomic_store(&g_sync_published, -1);
378 if (ui_sync_is_visible())
380 g_sync_started = false;
381}
#define UNTRACKED_FREE(ptr)

References keyboard_destroy(), platform_close(), ui_controller_select_interactive(), ui_sync_is_visible(), and UNTRACKED_FREE.

Referenced by ui_controller_shutdown().