+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * src/avx_instance.c - Library initialization and subsystem lifecycle |
| 7 | */ |
| 8 | |
| 9 | #include "avx_internal.h" |
| 10 | #include "avx_video_codec.h" |
| 11 | #include <string.h> |
| 12 | #include <stdio.h> |
| 13 | |
| 14 | avx_global_state_t g_avx_state = { |
| 15 | .global_lock = AVX_MUTEX_INITIALIZER, |
| 16 | .initialized = false |
| 17 | }; |
| 18 | |
| 19 | /* Initializes platform socket subsystems and global session slot tables. */ |
| 20 | OPENAVX_API bool avx_engine_initialize(void) { |
| 21 | avx_platform_sockets_init(); |
| 22 | avx_mutex_lock(&g_avx_state.global_lock); |
| 23 | if (!g_avx_state.initialized) { |
| 24 | memset(g_avx_state.find_slots, 0, sizeof(g_avx_state.find_slots)); |
| 25 | memset(g_avx_state.send_slots, 0, sizeof(g_avx_state.send_slots)); |
| 26 | memset(g_avx_state.recv_slots, 0, sizeof(g_avx_state.recv_slots)); |
| 27 | memset(g_avx_state.routing_slots, 0, sizeof(g_avx_state.routing_slots)); |
| 28 | g_avx_state.initialized = true; |
| 29 | avx_config_init(); |
| 30 | } |
| 31 | avx_mutex_unlock(&g_avx_state.global_lock); |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | /* Destroys active sessions outside global_lock to prevent lock inversion deadlocks. */ |
| 36 | OPENAVX_API void avx_engine_destroy(void) { |
| 37 | avx_mutex_lock(&g_avx_state.global_lock); |
| 38 | if (!g_avx_state.initialized) { |
| 39 | avx_mutex_unlock(&g_avx_state.global_lock); |
| 40 | return; |
| 41 | } |
| 42 | g_avx_state.initialized = false; |
| 43 | |
| 44 | /* Snapshot active slots under global_lock, then destroy them outside global_lock |
| 45 | * to eliminate recursive mutex self-deadlocks and lock hierarchy inversions */ |
| 46 | avx_find_ctx_t* find_to_destroy[AVX_MAX_SESSIONS]; |
| 47 | int n_find = 0; |
| 48 | for (int i = 0; i < AVX_MAX_SESSIONS; ++i) { |
| 49 | if (g_avx_state.find_slots[i].in_use) { |
| 50 | find_to_destroy[n_find++] = &g_avx_state.find_slots[i]; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | avx_send_ctx_t* send_to_destroy[AVX_MAX_SESSIONS]; |
| 55 | int n_send = 0; |
| 56 | for (int i = 0; i < AVX_MAX_SESSIONS; ++i) { |
| 57 | if (g_avx_state.send_slots[i].in_use) { |
| 58 | send_to_destroy[n_send++] = &g_avx_state.send_slots[i]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | avx_recv_ctx_t* recv_to_destroy[AVX_MAX_SESSIONS]; |
| 63 | int n_recv = 0; |
| 64 | for (int i = 0; i < AVX_MAX_SESSIONS; ++i) { |
| 65 | if (g_avx_state.recv_slots[i].in_use) { |
| 66 | recv_to_destroy[n_recv++] = &g_avx_state.recv_slots[i]; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | avx_routing_ctx_t* routing_to_destroy[AVX_MAX_SESSIONS]; |
| 71 | int n_routing = 0; |
| 72 | for (int i = 0; i < AVX_MAX_SESSIONS; ++i) { |
| 73 | if (g_avx_state.routing_slots[i].in_use) { |
| 74 | routing_to_destroy[n_routing++] = &g_avx_state.routing_slots[i]; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | avx_mutex_unlock(&g_avx_state.global_lock); |
| 79 | |
| 80 | /* Now destroy all active sessions safely with global_lock released */ |
| 81 | for (int i = 0; i < n_routing; ++i) { |
| 82 | avx_router_destroy(routing_to_destroy[i]); |
| 83 | } |
| 84 | for (int i = 0; i < n_recv; ++i) { |
| 85 | avx_rx_destroy(recv_to_destroy[i]); |
| 86 | } |
| 87 | for (int i = 0; i < n_send; ++i) { |
| 88 | avx_tx_destroy(send_to_destroy[i]); |
| 89 | } |
| 90 | for (int i = 0; i < n_find; ++i) { |
| 91 | avx_find_destroy(find_to_destroy[i]); |
| 92 | } |
| 93 | |
| 94 | avx_video_codec_destroy(); |
| 95 | avx_config_cleanup(); |
| 96 | avx_platform_sockets_cleanup(); |
| 97 | } |
| 98 | |
| 99 | const char* avx_engine_version(void) { |
| 100 | return OPENAVX_VERSION; |
| 101 | } |
| 102 | |
| 103 | bool avx_engine_is_supported_cpu(void) { |
| 104 | /* Standard x86_64 CPU is universally supported */ |
| 105 | return true; |
| 106 | } |