mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-30 09:36:21 +00:00
fix(ai): use atomic operations for refcounting
Replace plain ref_count++/-- with g_atomic_int_inc and g_atomic_int_dec_and_test for both AIProvider and AISession refcounts. Prevents data races when ref/unref is called from worker threads.
This commit is contained in:
@@ -192,7 +192,7 @@ static AIProvider*
|
|||||||
ai_provider_ref(AIProvider* provider)
|
ai_provider_ref(AIProvider* provider)
|
||||||
{
|
{
|
||||||
if (provider) {
|
if (provider) {
|
||||||
provider->ref_count++;
|
g_atomic_int_inc(&provider->ref_count);
|
||||||
}
|
}
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
@@ -203,9 +203,9 @@ ai_provider_unref(AIProvider* provider)
|
|||||||
if (!provider)
|
if (!provider)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
provider->ref_count--;
|
if (!g_atomic_int_dec_and_test(&provider->ref_count)) {
|
||||||
if (provider->ref_count > 0)
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
g_free(provider->name);
|
g_free(provider->name);
|
||||||
g_free(provider->api_url);
|
g_free(provider->api_url);
|
||||||
@@ -451,7 +451,7 @@ AISession*
|
|||||||
ai_session_ref(AISession* session)
|
ai_session_ref(AISession* session)
|
||||||
{
|
{
|
||||||
if (session) {
|
if (session) {
|
||||||
session->ref_count++;
|
g_atomic_int_inc(&session->ref_count);
|
||||||
}
|
}
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
@@ -462,9 +462,9 @@ ai_session_unref(AISession* session)
|
|||||||
if (!session)
|
if (!session)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
session->ref_count--;
|
if (!g_atomic_int_dec_and_test(&session->ref_count)) {
|
||||||
if (session->ref_count > 0)
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
g_free(session->provider_name);
|
g_free(session->provider_name);
|
||||||
ai_provider_unref(session->provider);
|
ai_provider_unref(session->provider);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ typedef struct ai_provider_t
|
|||||||
gchar* org_id; /* Optional organization ID */
|
gchar* org_id; /* Optional organization ID */
|
||||||
gchar* project_id; /* Optional project ID (for some providers) */
|
gchar* project_id; /* Optional project ID (for some providers) */
|
||||||
GList* models; /* List of available models (gchar*) */
|
GList* models; /* List of available models (gchar*) */
|
||||||
guint ref_count; /* Reference count */
|
gint32 ref_count; /* Reference count (atomic) */
|
||||||
} AIProvider;
|
} AIProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,7 +35,7 @@ typedef struct ai_session_t
|
|||||||
gchar* model; /* Model identifier (e.g., "gpt-4", "sonar") */
|
gchar* model; /* Model identifier (e.g., "gpt-4", "sonar") */
|
||||||
gchar* api_key; /* API key for this session */
|
gchar* api_key; /* API key for this session */
|
||||||
GList* history; /* Conversation history (GList of AIMessage*) */
|
GList* history; /* Conversation history (GList of AIMessage*) */
|
||||||
guint ref_count; /* Reference count */
|
gint32 ref_count; /* Reference count (atomic) */
|
||||||
} AISession;
|
} AISession;
|
||||||
|
|
||||||
/* ========================================================================
|
/* ========================================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user