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 # For journal_mode=memory, the static_prng mutex is not required. This
119 # is because the header of an in-memory journal does not contain
120 # any random bytes, and so no call to sqlite3_randomness() is made.
121 if {[permutation]=="inmemory_journal"} {
122 set idx [lsearch $mutexes static_prng]
123 if {$idx>=0} { set mutexes [lreplace $mutexes $idx $idx] }
126 do_test mutex1.2.$mode.1 {
129 sqlite3_config_memstatus 1
133 do_test mutex1.2.$mode.2 {
136 sqlite3 db test.db -nomutex 0 -fullmutex 0
137 catchsql { CREATE TABLE abc(a, b, c) }
139 INSERT INTO abc VALUES(1, 2, 3);
142 ifcapable !memorymanage {
143 regsub { static_lru} $mutexes {} mutexes
145 if {$mode ne "singlethread"} {
146 do_test mutex1.2.$mode.3 {
148 # NOTE: Make sure all the app and vfs mutexes get used.
150 enter_static_mutex static_app1
151 leave_static_mutex static_app1
152 enter_static_mutex static_app2
153 leave_static_mutex static_app2
154 enter_static_mutex static_app3
155 leave_static_mutex static_app3
156 enter_static_mutex static_vfs1
157 leave_static_mutex static_vfs1
158 enter_static_mutex static_vfs2
159 leave_static_mutex static_vfs2
160 enter_static_mutex static_vfs3
161 leave_static_mutex static_vfs3
164 do_test mutex1.2.$mode.4 {
165 mutex_counters counters
168 foreach {key value} [array get counters] {
169 if {$key ne "total" && $value > 0} {
176 sqlite3_enable_shared_cache $enable_shared_cache
178 # Open and use a connection in "nomutex" mode. Test that no recursive
179 # mutexes are obtained.
183 sqlite3 db test.db -nomutex 1
184 execsql { SELECT * FROM abc }
185 } {1 2 3 1 2 3 1 2 3}
187 mutex_counters counters
188 set counters(recursive)
192 # Test the sqlite3_db_mutex() function.
198 db eval {SELECT 1, 2, 3}
202 db eval {SELECT 1, 2, 3}
206 sqlite3 db test.db -nomutex 1
208 db eval {SELECT 1, 2, 3}
212 db eval {SELECT 1, 2, 3}
219 install_mutex_counters 0
223 autoinstall_test_functions