Skip to content

Commit

Permalink
fix(sqlite): use AtomicUsize for thread IDs (#2864)
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander authored Nov 7, 2023
1 parent b91e4de commit ea8e0ab
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions sqlx-sqlite/src/connection/establish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ use std::ffi::{c_void, CStr, CString};
use std::io;
use std::os::raw::c_int;
use std::ptr::{addr_of_mut, null, null_mut};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

static THREAD_ID: AtomicU64 = AtomicU64::new(0);
// This was originally `AtomicU64` but that's not supported on MIPS (or PowerPC):
// https://github.com/launchbadge/sqlx/issues/2859
// https://doc.rust-lang.org/stable/std/sync/atomic/index.html#portability
static THREAD_ID: AtomicUsize = AtomicUsize::new(0);

enum SqliteLoadExtensionMode {
/// Enables only the C-API, leaving the SQL function disabled.
Expand Down Expand Up @@ -142,14 +145,16 @@ impl EstablishParams {
})
.collect::<Result<IndexMap<CString, Option<CString>>, io::Error>>()?;

let thread_id = THREAD_ID.fetch_add(1, Ordering::AcqRel);

Ok(Self {
filename,
open_flags: flags,
busy_timeout: options.busy_timeout,
statement_cache_capacity: options.statement_cache_capacity,
log_settings: options.log_settings.clone(),
extensions,
thread_name: (options.thread_name)(THREAD_ID.fetch_add(1, Ordering::AcqRel)),
thread_name: (options.thread_name)(thread_id as u64),
command_channel_size: options.command_channel_size,
#[cfg(feature = "regexp")]
register_regexp_function: options.register_regexp_function,
Expand Down

0 comments on commit ea8e0ab

Please sign in to comment.