The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ipc: Debounce the no client disconnection

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2462>

authored by

Bones and committed by
Marge Bot
9455eec8 fd228f1e

+46 -4
+6
src/xrt/ipc/server/ipc_server.h
··· 371 371 // Should we exit when no clients are connected. 372 372 bool exit_when_idle; 373 373 374 + // Timestamp when last client disconnected (for exit_when_idle delay) 375 + uint64_t last_client_disconnect_ns; 376 + 377 + // How long to wait after all clients disconnect before exiting (in nanoseconds) 378 + uint64_t exit_when_idle_delay_ns; 379 + 374 380 enum u_logging_level log_level; 375 381 376 382 struct ipc_thread threads[IPC_MAX_CLIENTS];
+30 -3
src/xrt/ipc/server/ipc_server_per_client_thread.c
··· 35 35 * 36 36 */ 37 37 38 + static void * 39 + delayed_exit_thread(void *_server) 40 + { 41 + struct ipc_server *s = (struct ipc_server *)_server; 42 + 43 + // Sleep for the delay duration 44 + os_nanosleep(s->exit_when_idle_delay_ns); 45 + 46 + // Check if we still have no clients after the delay 47 + os_mutex_lock(&s->global_state.lock); 48 + bool should_exit = 49 + (s->exit_when_idle && s->global_state.connected_client_count == 0 && s->last_client_disconnect_ns > 0); 50 + os_mutex_unlock(&s->global_state.lock); 51 + 52 + if (should_exit) { 53 + IPC_INFO(s, "All clients disconnected for %u milliseconds, shutting down.", 54 + (unsigned int)(s->exit_when_idle_delay_ns / U_TIME_1MS_IN_NS)); 55 + s->running = false; 56 + } 57 + 58 + return NULL; 59 + } 60 + 38 61 static void 39 62 common_shutdown(volatile struct ipc_client_state *ics) 40 63 { ··· 119 142 if (ics->server->exit_on_disconnect) { 120 143 ics->server->running = false; 121 144 } 122 - 123 - // Should we stop the server when all clients disconnect? 145 + // Should we stop when all clients disconnect? 124 146 if (ics->server->exit_when_idle && ics->server->global_state.connected_client_count == 0) { 125 - ics->server->running = false; 147 + ics->server->last_client_disconnect_ns = os_monotonic_get_ns(); 148 + 149 + struct os_thread thread; 150 + os_thread_start(&thread, delayed_exit_thread, (void *)ics->server); 151 + // We intentionally don't join this thread - it's fire and forget 126 152 } 153 + 127 154 128 155 ipc_server_deactivate_session(ics); 129 156 }
+10 -1
src/xrt/ipc/server/ipc_server_process.c
··· 56 56 */ 57 57 58 58 DEBUG_GET_ONCE_BOOL_OPTION(exit_on_disconnect, "IPC_EXIT_ON_DISCONNECT", false) 59 - DEBUG_GET_ONCE_BOOL_OPTION(exit_when_idle, "IPC_EXIT_ON_IDLE", false) 59 + DEBUG_GET_ONCE_BOOL_OPTION(exit_when_idle, "IPC_EXIT_WHEN_IDLE", false) 60 + DEBUG_GET_ONCE_NUM_OPTION(exit_when_idle_delay_ms, "IPC_EXIT_WHEN_IDLE_DELAY_MS", 5000) 60 61 DEBUG_GET_ONCE_LOG_OPTION(ipc_log, "IPC_LOG", U_LOGGING_INFO) 61 62 62 63 ··· 493 494 s->running = true; 494 495 s->exit_on_disconnect = debug_get_bool_option_exit_on_disconnect(); 495 496 s->exit_when_idle = debug_get_bool_option_exit_when_idle(); 497 + s->last_client_disconnect_ns = 0; 498 + uint64_t delay_ms = debug_get_num_option_exit_when_idle_delay_ms(); 499 + s->exit_when_idle_delay_ns = delay_ms * U_TIME_1MS_IN_NS; 496 500 497 501 xret = xrt_instance_create(NULL, &s->xinst); 498 502 if (xret != XRT_SUCCESS) { ··· 543 547 u_var_add_log_level(s, &s->log_level, "Log level"); 544 548 u_var_add_bool(s, &s->exit_on_disconnect, "exit_on_disconnect"); 545 549 u_var_add_bool(s, &s->exit_when_idle, "exit_when_idle"); 550 + u_var_add_u64(s, &s->exit_when_idle_delay_ns, "exit_when_idle_delay_ns"); 546 551 u_var_add_bool(s, (bool *)&s->running, "running"); 547 552 548 553 return 0; ··· 919 924 920 925 // Increment the connected client counter 921 926 vs->global_state.connected_client_count++; 927 + 928 + // A client connected, so we're no longer in a delayed exit state 929 + // (The delay thread will still check the client count before exiting) 930 + vs->last_client_disconnect_ns = 0; 922 931 923 932 // find the next free thread in our array (server_thread_index is -1) 924 933 // and have it handle this connection