| 1 |
# 2008 June 17 |
| 2 |
# |
| 3 |
# The author disclaims copyright to this source code. In place of |
| 4 |
# a legal notice, here is a blessing: |
| 5 |
# |
| 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. |
| 9 |
# |
| 10 |
#*********************************************************************** |
| 11 |
# |
| 12 |
# $Id: mutex1.test,v 1.20 2009/04/23 14:58:40 danielk1977 Exp $ |
| 13 |
|
| 14 |
set testdir [file dirname $argv0] |
| 15 |
source $testdir/tester.tcl |
| 16 |
|
| 17 |
ifcapable !mutex { |
| 18 |
finish_test |
| 19 |
return |
| 20 |
} |
| 21 |
if {[info exists tester_do_binarylog]} { |
| 22 |
finish_test |
| 23 |
return |
| 24 |
} |
| 25 |
|
| 26 |
sqlite3_reset_auto_extension |
| 27 |
clear_mutex_counters |
| 28 |
|
| 29 |
proc mutex_counters {varname} { |
| 30 |
upvar $varname var |
| 31 |
set var(total) 0 |
| 32 |
foreach {name value} [read_mutex_counters] { |
| 33 |
set var($name) $value |
| 34 |
incr var(total) $value |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 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. |
| 44 |
# |
| 45 |
do_test mutex1-1.0 { |
| 46 |
install_mutex_counters 1 |
| 47 |
} {SQLITE_MISUSE} |
| 48 |
|
| 49 |
do_test mutex1-1.1 { |
| 50 |
db close |
| 51 |
install_mutex_counters 1 |
| 52 |
} {SQLITE_MISUSE} |
| 53 |
|
| 54 |
do_test mutex1-1.2 { |
| 55 |
sqlite3_shutdown |
| 56 |
install_mutex_counters 1 |
| 57 |
} {SQLITE_OK} |
| 58 |
|
| 59 |
do_test mutex1-1.3 { |
| 60 |
install_mutex_counters 0 |
| 61 |
} {SQLITE_OK} |
| 62 |
|
| 63 |
do_test mutex1-1.4 { |
| 64 |
install_mutex_counters 1 |
| 65 |
} {SQLITE_OK} |
| 66 |
|
| 67 |
do_test mutex1-1.5 { |
| 68 |
mutex_counters counters |
| 69 |
set counters(total) |
| 70 |
} {0} |
| 71 |
|
| 72 |
do_test mutex1-1.6 { |
| 73 |
sqlite3_initialize |
| 74 |
} {SQLITE_OK} |
| 75 |
|
| 76 |
do_test mutex1-1.7 { |
| 77 |
mutex_counters counters |
| 78 |
# list $counters(total) $counters(static_main) |
| 79 |
expr {$counters(total)>0} |
| 80 |
} {1} |
| 81 |
|
| 82 |
do_test mutex1-1.8 { |
| 83 |
clear_mutex_counters |
| 84 |
sqlite3_initialize |
| 85 |
} {SQLITE_OK} |
| 86 |
|
| 87 |
do_test mutex1-1.9 { |
| 88 |
mutex_counters counters |
| 89 |
list $counters(total) $counters(static_main) |
| 90 |
} {0 0} |
| 91 |
|
| 92 |
#------------------------------------------------------------------------- |
| 93 |
# Tests mutex1-2.* test the three thread-safety related modes that |
| 94 |
# can be selected using sqlite3_config: |
| 95 |
# |
| 96 |
# * Serialized mode, |
| 97 |
# * Multi-threaded mode, |
| 98 |
# * Single-threaded mode. |
| 99 |
# |
| 100 |
ifcapable threadsafe1&&shared_cache { |
| 101 |
set enable_shared_cache [sqlite3_enable_shared_cache 1] |
| 102 |
foreach {mode mutexes} { |
| 103 |
singlethread {} |
| 104 |
multithread { |
| 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 |
| 108 |
static_vfs3 |
| 109 |
} |
| 110 |
serialized { |
| 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 |
| 115 |
} |
| 116 |
} { |
| 117 |
|
| 118 |
ifcapable thread_misuse_warnings { |
| 119 |
if {$mode ne "serialized"} continue |
| 120 |
} |
| 121 |
|
| 122 |
# For journal_mode=memory, the static_prng mutex is not required. This |
| 123 |
# is because the header of an in-memory journal does not contain |
| 124 |
# any random bytes, and so no call to sqlite3_randomness() is made. |
| 125 |
if {[permutation]=="inmemory_journal"} { |
| 126 |
set idx [lsearch $mutexes static_prng] |
| 127 |
if {$idx>=0} { set mutexes [lreplace $mutexes $idx $idx] } |
| 128 |
} |
| 129 |
|
| 130 |
do_test mutex1.2.$mode.1 { |
| 131 |
catch {db close} |
| 132 |
sqlite3_shutdown |
| 133 |
sqlite3_config_memstatus 1 |
| 134 |
sqlite3_config $mode |
| 135 |
} SQLITE_OK |
| 136 |
|
| 137 |
do_test mutex1.2.$mode.2 { |
| 138 |
sqlite3_initialize |
| 139 |
clear_mutex_counters |
| 140 |
sqlite3 db test.db -nomutex 0 -fullmutex 0 |
| 141 |
catchsql { CREATE TABLE abc(a, b, c) } |
| 142 |
db eval { |
| 143 |
INSERT INTO abc VALUES(1, 2, 3); |
| 144 |
} |
| 145 |
} {} |
| 146 |
ifcapable !memorymanage { |
| 147 |
regsub { static_lru} $mutexes {} mutexes |
| 148 |
} |
| 149 |
if {$mode ne "singlethread"} { |
| 150 |
do_test mutex1.2.$mode.3 { |
| 151 |
# |
| 152 |
# NOTE: Make sure all the app and vfs mutexes get used. |
| 153 |
# |
| 154 |
enter_static_mutex static_app1 |
| 155 |
leave_static_mutex static_app1 |
| 156 |
enter_static_mutex static_app2 |
| 157 |
leave_static_mutex static_app2 |
| 158 |
enter_static_mutex static_app3 |
| 159 |
leave_static_mutex static_app3 |
| 160 |
enter_static_mutex static_vfs1 |
| 161 |
leave_static_mutex static_vfs1 |
| 162 |
enter_static_mutex static_vfs2 |
| 163 |
leave_static_mutex static_vfs2 |
| 164 |
enter_static_mutex static_vfs3 |
| 165 |
leave_static_mutex static_vfs3 |
| 166 |
} {} |
| 167 |
} |
| 168 |
do_test mutex1.2.$mode.4 { |
| 169 |
mutex_counters counters |
| 170 |
|
| 171 |
set res [list] |
| 172 |
foreach {key value} [array get counters] { |
| 173 |
if {$key ne "total" && $value > 0} { |
| 174 |
lappend res $key |
| 175 |
} |
| 176 |
} |
| 177 |
lsort $res |
| 178 |
} [lsort $mutexes] |
| 179 |
} |
| 180 |
sqlite3_enable_shared_cache $enable_shared_cache |
| 181 |
|
| 182 |
# Open and use a connection in "nomutex" mode. Test that no recursive |
| 183 |
# mutexes are obtained. |
| 184 |
ifcapable !thread_misuse_warnings { |
| 185 |
do_test mutex1.3.1 { |
| 186 |
catch {db close} |
| 187 |
clear_mutex_counters |
| 188 |
sqlite3 db test.db -nomutex 1 |
| 189 |
execsql { SELECT * FROM abc } |
| 190 |
} {1 2 3 1 2 3 1 2 3} |
| 191 |
do_test mutex1.3.2 { |
| 192 |
mutex_counters counters |
| 193 |
set counters(recursive) |
| 194 |
} {0} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
# Test the sqlite3_db_mutex() function. |
| 199 |
# |
| 200 |
do_test mutex1.4.1 { |
| 201 |
catch {db close} |
| 202 |
sqlite3 db test.db |
| 203 |
enter_db_mutex db |
| 204 |
db eval {SELECT 1, 2, 3} |
| 205 |
} {1 2 3} |
| 206 |
do_test mutex1.4.2 { |
| 207 |
leave_db_mutex db |
| 208 |
db eval {SELECT 1, 2, 3} |
| 209 |
} {1 2 3} |
| 210 |
do_test mutex1.4.3 { |
| 211 |
catch {db close} |
| 212 |
sqlite3 db test.db -nomutex 1 |
| 213 |
enter_db_mutex db |
| 214 |
db eval {SELECT 1, 2, 3} |
| 215 |
} {1 2 3} |
| 216 |
do_test mutex1.4.4 { |
| 217 |
leave_db_mutex db |
| 218 |
db eval {SELECT 1, 2, 3} |
| 219 |
} {1 2 3} |
| 220 |
|
| 221 |
do_test mutex1-X { |
| 222 |
catch {db close} |
| 223 |
sqlite3_shutdown |
| 224 |
clear_mutex_counters |
| 225 |
install_mutex_counters 0 |
| 226 |
sqlite3_initialize |
| 227 |
} {SQLITE_OK} |
| 228 |
|
| 229 |
autoinstall_test_functions |
| 230 |
finish_test |