3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # $Id: mutex1.test,v 1.20 2009/04/23 14:58:40 danielk1977 Exp $
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
21 if {[info exists tester_do_binarylog]} {
26 sqlite3_reset_auto_extension
29 proc mutex_counters {varname} {
32 foreach {name value} [read_mutex_counters] {
34 incr var(total) $value
38 #-------------------------------------------------------------------------
39 # Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if
40 # is called at the wrong time. And that the first time sqlite3_initialize
41 # is called it obtains the 'static_main' mutex 3 times and a recursive
42 # mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops
43 # that do not require any mutexes.
46 install_mutex_counters 1
51 install_mutex_counters 1
56 install_mutex_counters 1
60 install_mutex_counters 0
64 install_mutex_counters 1
68 mutex_counters counters
77 mutex_counters counters
78 # list $counters(total) $counters(static_main)
79 expr {$counters(total)>0}
88 mutex_counters counters
89 list $counters(total) $counters(static_main)
92 #-------------------------------------------------------------------------
93 # Tests mutex1-2.* test the three thread-safety related modes that
94 # can be selected using sqlite3_config:
97 # * Multi-threaded mode,
98 # * Single-threaded mode.
100 ifcapable threadsafe1&&shared_cache {
101 set enable_shared_cache [sqlite3_enable_shared_cache 1]
102 foreach {mode mutexes} {
105 fast static_app1 static_app2 static_app3
106 static_lru static_main static_mem static_open
107 static_prng static_pmem static_vfs1 static_vfs2
111 fast recursive static_app1 static_app2
112 static_app3 static_lru static_main static_mem
113 static_open static_prng static_pmem static_vfs1
114 static_vfs2 static_vfs3
118 do_test mutex1.2.$mode.1 {
124 do_test mutex1.2.$mode.2 {
127 sqlite3 db test.db -nomutex 0 -fullmutex 0
128 catchsql { CREATE TABLE abc(a, b, c) }
130 INSERT INTO abc VALUES(1, 2, 3);
133 ifcapable !memorymanage {
134 regsub { static_lru} $mutexes {} mutexes
136 if {$mode ne "singlethread"} {
137 do_test mutex1.2.$mode.3 {
139 # NOTE: Make sure all the app and vfs mutexes get used.
141 enter_static_mutex static_app1
142 leave_static_mutex static_app1
143 enter_static_mutex static_app2
144 leave_static_mutex static_app2
145 enter_static_mutex static_app3
146 leave_static_mutex static_app3
147 enter_static_mutex static_vfs1
148 leave_static_mutex static_vfs1
149 enter_static_mutex static_vfs2
150 leave_static_mutex static_vfs2
151 enter_static_mutex static_vfs3
152 leave_static_mutex static_vfs3
155 do_test mutex1.2.$mode.4 {
156 mutex_counters counters
159 foreach {key value} [array get counters] {
160 if {$key ne "total" && $value > 0} {
167 sqlite3_enable_shared_cache $enable_shared_cache
169 # Open and use a connection in "nomutex" mode. Test that no recursive
170 # mutexes are obtained.
174 sqlite3 db test.db -nomutex 1
175 execsql { SELECT * FROM abc }
176 } {1 2 3 1 2 3 1 2 3}
178 mutex_counters counters
179 set counters(recursive)
183 # Test the sqlite3_db_mutex() function.
189 db eval {SELECT 1, 2, 3}
193 db eval {SELECT 1, 2, 3}
197 sqlite3 db test.db -nomutex 1
199 db eval {SELECT 1, 2, 3}
203 db eval {SELECT 1, 2, 3}
210 install_mutex_counters 0
214 autoinstall_test_functions