Skip to content

Commit 6a1ce90

Browse files
committed
add thread safety predicate to SQLite3
1 parent 46ae4ee commit 6a1ce90

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

ext/sqlite3/sqlite3.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ static VALUE libversion(VALUE UNUSED(klass))
6565
return INT2NUM(sqlite3_libversion_number());
6666
}
6767

68+
/* Returns the compile time setting of the SQLITE_THREADSAFE flag.
69+
* See: https://www.sqlite.org/c3ref/threadsafe.html
70+
*/
71+
static VALUE threadsafe_p(VALUE UNUSED(klass))
72+
{
73+
return INT2NUM(sqlite3_threadsafe());
74+
}
75+
6876
void Init_sqlite3_native()
6977
{
7078
/*
@@ -92,6 +100,7 @@ void Init_sqlite3_native()
92100
#endif
93101

94102
rb_define_singleton_method(mSqlite3, "libversion", libversion, 0);
103+
rb_define_singleton_method(mSqlite3, "threadsafe", threadsafe_p, 0);
95104
rb_define_const(mSqlite3, "SQLITE_VERSION", rb_str_new2(SQLITE_VERSION));
96105
rb_define_const(mSqlite3, "SQLITE_VERSION_NUMBER", INT2FIX(SQLITE_VERSION_NUMBER));
97106
}

lib/sqlite3.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88

99
require 'sqlite3/database'
1010
require 'sqlite3/version'
11+
12+
module SQLite3
13+
# Was sqlite3 compiled with thread safety on?
14+
def self.threadsafe?; threadsafe > 0; end
15+
end

test/test_sqlite3.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,17 @@ class TestSQLite3 < SQLite3::TestCase
55
def test_libversion
66
assert_not_nil SQLite3.libversion
77
end
8+
9+
def test_threadsafe
10+
assert_not_nil SQLite3.threadsafe
11+
end
12+
13+
def test_threadsafe?
14+
if SQLite3.threadsafe > 0
15+
assert SQLite3.threadsafe?
16+
else
17+
refute SQLite3.threadsafe?
18+
end
19+
end
820
end
921
end

0 commit comments

Comments
 (0)