fix(ai): use atomic operations for refcounting
Some checks failed
CI Code / Check coding style (pull_request) Successful in 1m8s
CI Code / Check spelling (pull_request) Successful in 1m7s
CI Code / Linux (debian) (pull_request) Failing after 2m58s
CI Code / Linux (arch) (pull_request) Failing after 3m7s
CI Code / Linux (ubuntu) (pull_request) Failing after 3m8s
CI Code / Code Coverage (pull_request) Successful in 2m54s
Some checks failed
CI Code / Check coding style (pull_request) Successful in 1m8s
CI Code / Check spelling (pull_request) Successful in 1m7s
CI Code / Linux (debian) (pull_request) Failing after 2m58s
CI Code / Linux (arch) (pull_request) Failing after 3m7s
CI Code / Linux (ubuntu) (pull_request) Failing after 3m8s
CI Code / Code Coverage (pull_request) Successful in 2m54s
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)
|
||||
{
|
||||
if (provider) {
|
||||
provider->ref_count++;
|
||||
g_atomic_int_inc(&provider->ref_count);
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
@@ -203,9 +203,9 @@ ai_provider_unref(AIProvider* provider)
|
||||
if (!provider)
|
||||
return;
|
||||
|
||||
provider->ref_count--;
|
||||
if (provider->ref_count > 0)
|
||||
if (!g_atomic_int_dec_and_test(&provider->ref_count)) {
|
||||
return;
|
||||
}
|
||||
|
||||
g_free(provider->name);
|
||||
g_free(provider->api_url);
|
||||
@@ -451,7 +451,7 @@ AISession*
|
||||
ai_session_ref(AISession* session)
|
||||
{
|
||||
if (session) {
|
||||
session->ref_count++;
|
||||
g_atomic_int_inc(&session->ref_count);
|
||||
}
|
||||
return session;
|
||||
}
|
||||
@@ -462,9 +462,9 @@ ai_session_unref(AISession* session)
|
||||
if (!session)
|
||||
return;
|
||||
|
||||
session->ref_count--;
|
||||
if (session->ref_count > 0)
|
||||
if (!g_atomic_int_dec_and_test(&session->ref_count)) {
|
||||
return;
|
||||
}
|
||||
|
||||
g_free(session->provider_name);
|
||||
ai_provider_unref(session->provider);
|
||||
|
||||
Reference in New Issue
Block a user