AryaWu/sqlite
0
1# 2008 June 172#3# The author disclaims copyright to this source code. In place of4# 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 14set testdir [file dirname $argv0]15source $testdir/tester.tcl16 17ifcapable !mutex {18 finish_test19 return20}21if {[info exists tester_do_binarylog]} {22 finish_test23 return24}25 26sqlite3_reset_auto_extension27clear_mutex_counters28 29proc mutex_counters {varname} {30 upvar $varname var31 set var(total) 032 foreach {name value} [read_mutex_counters] {33 set var($name) $value34 incr var(total) $value35 }36}37 38#-------------------------------------------------------------------------39# Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if40# is called at the wrong time. And that the first time sqlite3_initialize41# is called it obtains the 'static_main' mutex 3 times and a recursive42# mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops43# that do not require any mutexes.44#45do_test mutex1-1.0 {46 install_mutex_counters 147} {SQLITE_MISUSE}48 49do_test mutex1-1.1 {50 db close51 install_mutex_counters 152} {SQLITE_MISUSE}53 54do_test mutex1-1.2 {55 sqlite3_shutdown56 install_mutex_counters 157} {SQLITE_OK}58 59do_test mutex1-1.3 {60 install_mutex_counters 061} {SQLITE_OK}62 63do_test mutex1-1.4 {64 install_mutex_counters 165} {SQLITE_OK}66 67do_test mutex1-1.5 {68 mutex_counters counters69 set counters(total)70} {0}71 72do_test mutex1-1.6 {73 sqlite3_initialize74} {SQLITE_OK}75 76do_test mutex1-1.7 {77 mutex_counters counters78 # list $counters(total) $counters(static_main)79 expr {$counters(total)>0}80} {1}81 82do_test mutex1-1.8 {83 clear_mutex_counters84 sqlite3_initialize85} {SQLITE_OK}86 87do_test mutex1-1.9 {88 mutex_counters counters89 list $counters(total) $counters(static_main)90} {0 0}91 92#-------------------------------------------------------------------------93# Tests mutex1-2.* test the three thread-safety related modes that94# can be selected using sqlite3_config:95#96# * Serialized mode,97# * Multi-threaded mode,98# * Single-threaded mode.99#100ifcapable 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_app3106 static_lru static_main static_mem static_open107 static_prng static_pmem static_vfs1 static_vfs2108 static_vfs3109 }110 serialized {111 fast recursive static_app1 static_app2112 static_app3 static_lru static_main static_mem113 static_open static_prng static_pmem static_vfs1114 static_vfs2 static_vfs3115 }116 } {117 118 # For journal_mode=memory, the static_prng mutex is not required. This119 # is because the header of an in-memory journal does not contain120 # 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] }124 }125 126 do_test mutex1.2.$mode.1 {127 catch {db close}128 sqlite3_shutdown129 sqlite3_config_memstatus 1130 sqlite3_config $mode131 } SQLITE_OK132 133 do_test mutex1.2.$mode.2 {134 sqlite3_initialize135 clear_mutex_counters136 sqlite3 db test.db -nomutex 0 -fullmutex 0137 catchsql { CREATE TABLE abc(a, b, c) }138 db eval {139 INSERT INTO abc VALUES(1, 2, 3);140 }141 } {}142 ifcapable !memorymanage {143 regsub { static_lru} $mutexes {} mutexes144 }145 if {$mode ne "singlethread"} {146 do_test mutex1.2.$mode.3 {147 #148 # NOTE: Make sure all the app and vfs mutexes get used.149 #150 enter_static_mutex static_app1151 leave_static_mutex static_app1152 enter_static_mutex static_app2153 leave_static_mutex static_app2154 enter_static_mutex static_app3155 leave_static_mutex static_app3156 enter_static_mutex static_vfs1157 leave_static_mutex static_vfs1158 enter_static_mutex static_vfs2159 leave_static_mutex static_vfs2160 enter_static_mutex static_vfs3161 leave_static_mutex static_vfs3162 } {}163 }164 do_test mutex1.2.$mode.4 {165 mutex_counters counters166 167 set res [list]168 foreach {key value} [array get counters] {169 if {$key ne "total" && $value > 0} {170 lappend res $key171 }172 }173 lsort $res174 } [lsort $mutexes]175 }176 sqlite3_enable_shared_cache $enable_shared_cache177 178 # Open and use a connection in "nomutex" mode. Test that no recursive179 # mutexes are obtained.180 do_test mutex1.3.1 {181 catch {db close}182 clear_mutex_counters183 sqlite3 db test.db -nomutex 1184 execsql { SELECT * FROM abc }185 } {1 2 3 1 2 3 1 2 3}186 do_test mutex1.3.2 {187 mutex_counters counters188 set counters(recursive)189 } {0}190}191 192# Test the sqlite3_db_mutex() function.193#194do_test mutex1.4.1 {195 catch {db close}196 sqlite3 db test.db197 enter_db_mutex db198 db eval {SELECT 1, 2, 3}199} {1 2 3}200do_test mutex1.4.2 {201 leave_db_mutex db202 db eval {SELECT 1, 2, 3}203} {1 2 3}204do_test mutex1.4.3 {205 catch {db close}206 sqlite3 db test.db -nomutex 1207 enter_db_mutex db208 db eval {SELECT 1, 2, 3}209} {1 2 3}210do_test mutex1.4.4 {211 leave_db_mutex db212 db eval {SELECT 1, 2, 3}213} {1 2 3}214 215do_test mutex1-X {216 catch {db close}217 sqlite3_shutdown218 clear_mutex_counters219 install_mutex_counters 0220 sqlite3_initialize221} {SQLITE_OK}222 223autoinstall_test_functions224finish_test225 