2008-06-18 09:45:56 +00:00
# 2008 June 17
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
2009-04-23 14:58:39 +00:00
# $Id: mutex1.test,v 1.20 2009/04/23 14:58:40 danielk1977 Exp $
2008-06-18 09:45:56 +00:00
set testdir [file dirname $argv0]
source $testdir/tester.tcl
2008-07-12 15:55:54 +00:00
2008-10-07 15:25:48 +00:00
ifcapable !mutex {
finish_test
return
}
2008-07-12 15:55:54 +00:00
if {[info exists tester_do_binarylog]} {
finish_test
return
}
2008-06-19 17:54:33 +00:00
sqlite3_reset_auto_extension
2009-01-09 14:29:35 +00:00
clear_mutex_counters
2008-06-18 09:45:56 +00:00
proc mutex_counters {varname} {
upvar $varname var
set var(total) 0
foreach {name value} [read_mutex_counters] {
set var($name) $value
incr var(total) $value
}
}
#-------------------------------------------------------------------------
# Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if
2015-07-03 23:11:36 +00:00
# is called at the wrong time. And that the first time sqlite3_initialize
2020-06-19 13:33:53 +00:00
# is called it obtains the 'static_main' mutex 3 times and a recursive
2015-07-03 23:11:36 +00:00
# mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops
2008-06-26 08:29:34 +00:00
# that do not require any mutexes.
2008-06-18 09:45:56 +00:00
#
do_test mutex1-1.0 {
install_mutex_counters 1
} {SQLITE_MISUSE}
do_test mutex1-1.1 {
db close
install_mutex_counters 1
} {SQLITE_MISUSE}
do_test mutex1-1.2 {
sqlite3_shutdown
install_mutex_counters 1
} {SQLITE_OK}
do_test mutex1-1.3 {
install_mutex_counters 0
} {SQLITE_OK}
do_test mutex1-1.4 {
install_mutex_counters 1
} {SQLITE_OK}
do_test mutex1-1.5 {
mutex_counters counters
set counters(total)
} {0}
do_test mutex1-1.6 {
sqlite3_initialize
} {SQLITE_OK}
do_test mutex1-1.7 {
mutex_counters counters
2020-06-19 13:33:53 +00:00
# list $counters(total) $counters(static_main)
2008-06-28 11:23:00 +00:00
expr {$counters(total)>0}
} {1}
2008-06-18 09:45:56 +00:00
do_test mutex1-1.8 {
clear_mutex_counters
sqlite3_initialize
} {SQLITE_OK}
do_test mutex1-1.9 {
mutex_counters counters
2020-06-19 13:33:53 +00:00
list $counters(total) $counters(static_main)
2008-06-18 09:45:56 +00:00
} {0 0}
2008-06-18 17:09:10 +00:00
#-------------------------------------------------------------------------
# Tests mutex1-2.* test the three thread-safety related modes that
# can be selected using sqlite3_config:
#
# * Serialized mode,
# * Multi-threaded mode,
# * Single-threaded mode.
#
2016-03-18 00:19:48 +00:00
ifcapable threadsafe1&&shared_cache {
2009-04-23 14:58:39 +00:00
set enable_shared_cache [sqlite3_enable_shared_cache 1]
2008-06-18 17:59:03 +00:00
foreach {mode mutexes} {
singlethread {}
2011-01-26 07:25:32 +00:00
multithread {
2015-07-03 23:11:36 +00:00
fast static_app1 static_app2 static_app3
2020-06-19 13:33:53 +00:00
static_lru static_main static_mem static_open
2015-07-03 23:11:36 +00:00
static_prng static_pmem static_vfs1 static_vfs2
static_vfs3
2011-01-26 07:25:32 +00:00
}
serialized {
2015-07-03 23:11:36 +00:00
fast recursive static_app1 static_app2
2020-06-19 13:33:53 +00:00
static_app3 static_lru static_main static_mem
2015-07-03 23:11:36 +00:00
static_open static_prng static_pmem static_vfs1
static_vfs2 static_vfs3
2011-01-26 07:25:32 +00:00
}
2008-06-18 17:59:03 +00:00
} {
2008-07-08 02:12:37 +00:00
2023-10-23 14:24:09 +00:00
# For journal_mode=memory, the static_prng mutex is not required. This
# is because the header of an in-memory journal does not contain
# any random bytes, and so no call to sqlite3_randomness() is made.
if {[permutation]=="inmemory_journal"} {
set idx [lsearch $mutexes static_prng]
if {$idx>=0} { set mutexes [lreplace $mutexes $idx $idx] }
}
2008-06-18 17:59:03 +00:00
do_test mutex1.2.$mode.1 {
catch {db close}
sqlite3_shutdown
2023-02-04 21:25:17 +00:00
sqlite3_config_memstatus 1
2008-06-18 17:59:03 +00:00
sqlite3_config $mode
} SQLITE_OK
2008-06-26 08:29:34 +00:00
2008-06-18 17:59:03 +00:00
do_test mutex1.2.$mode.2 {
2008-06-26 08:29:34 +00:00
sqlite3_initialize
2008-06-18 17:59:03 +00:00
clear_mutex_counters
2009-03-24 15:08:09 +00:00
sqlite3 db test.db -nomutex 0 -fullmutex 0
2008-06-18 17:59:03 +00:00
catchsql { CREATE TABLE abc(a, b, c) }
db eval {
INSERT INTO abc VALUES(1, 2, 3);
2008-06-18 17:09:10 +00:00
}
2008-06-18 17:59:03 +00:00
} {}
2011-01-18 16:13:27 +00:00
ifcapable !memorymanage {
regsub { static_lru} $mutexes {} mutexes
}
2015-07-03 23:11:36 +00:00
if {$mode ne "singlethread"} {
do_test mutex1.2.$mode.3 {
#
# NOTE: Make sure all the app and vfs mutexes get used.
#
enter_static_mutex static_app1
leave_static_mutex static_app1
enter_static_mutex static_app2
leave_static_mutex static_app2
enter_static_mutex static_app3
leave_static_mutex static_app3
enter_static_mutex static_vfs1
leave_static_mutex static_vfs1
enter_static_mutex static_vfs2
leave_static_mutex static_vfs2
enter_static_mutex static_vfs3
leave_static_mutex static_vfs3
} {}
}
do_test mutex1.2.$mode.4 {
2008-06-18 17:59:03 +00:00
mutex_counters counters
2015-07-03 23:11:36 +00:00
2008-06-18 17:59:03 +00:00
set res [list]
foreach {key value} [array get counters] {
if {$key ne "total" && $value > 0} {
lappend res $key
}
}
lsort $res
2008-07-08 02:12:37 +00:00
} [lsort $mutexes]
2008-06-18 17:59:03 +00:00
}
2009-04-23 14:58:39 +00:00
sqlite3_enable_shared_cache $enable_shared_cache
2008-06-18 09:45:56 +00:00
2009-04-23 14:58:39 +00:00
# Open and use a connection in "nomutex" mode. Test that no recursive
# mutexes are obtained.
2008-07-15 00:27:34 +00:00
do_test mutex1.3.1 {
catch {db close}
clear_mutex_counters
sqlite3 db test.db -nomutex 1
execsql { SELECT * FROM abc }
} {1 2 3 1 2 3 1 2 3}
do_test mutex1.3.2 {
mutex_counters counters
set counters(recursive)
} {0}
}
2008-07-10 17:52:49 +00:00
2008-11-04 14:55:47 +00:00
# Test the sqlite3_db_mutex() function.
#
do_test mutex1.4.1 {
catch {db close}
sqlite3 db test.db
enter_db_mutex db
db eval {SELECT 1, 2, 3}
} {1 2 3}
do_test mutex1.4.2 {
leave_db_mutex db
db eval {SELECT 1, 2, 3}
} {1 2 3}
do_test mutex1.4.3 {
catch {db close}
sqlite3 db test.db -nomutex 1
enter_db_mutex db
db eval {SELECT 1, 2, 3}
} {1 2 3}
do_test mutex1.4.4 {
leave_db_mutex db
db eval {SELECT 1, 2, 3}
} {1 2 3}
2008-06-18 09:45:56 +00:00
do_test mutex1-X {
2008-06-18 17:59:03 +00:00
catch {db close}
2008-06-18 09:45:56 +00:00
sqlite3_shutdown
clear_mutex_counters
install_mutex_counters 0
2008-06-18 17:59:03 +00:00
sqlite3_initialize
2008-06-18 09:45:56 +00:00
} {SQLITE_OK}
2008-06-19 17:54:33 +00:00
autoinstall_test_functions
2008-06-18 09:45:56 +00:00
finish_test