danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 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 | # |
danielk1977 | a3f0659 | 2009-04-23 14:58:39 +0000 | [diff] [blame] | 12 | # $Id: mutex1.test,v 1.20 2009/04/23 14:58:40 danielk1977 Exp $ |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 13 | |
| 14 | set testdir [file dirname $argv0] |
| 15 | source $testdir/tester.tcl |
danielk1977 | 185eac9 | 2008-07-12 15:55:54 +0000 | [diff] [blame] | 16 | |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 17 | ifcapable !mutex { |
| 18 | finish_test |
| 19 | return |
| 20 | } |
danielk1977 | 185eac9 | 2008-07-12 15:55:54 +0000 | [diff] [blame] | 21 | if {[info exists tester_do_binarylog]} { |
| 22 | finish_test |
| 23 | return |
| 24 | } |
| 25 | |
drh | 55b0cf0 | 2008-06-19 17:54:33 +0000 | [diff] [blame] | 26 | sqlite3_reset_auto_extension |
drh | 5807c4c | 2009-01-09 14:29:35 +0000 | [diff] [blame] | 27 | clear_mutex_counters |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 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 |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 40 | # is called at the wrong time. And that the first time sqlite3_initialize |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 41 | # is called it obtains the 'static_main' mutex 3 times and a recursive |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 42 | # mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 43 | # that do not require any mutexes. |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 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 |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 78 | # list $counters(total) $counters(static_main) |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 79 | expr {$counters(total)>0} |
| 80 | } {1} |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 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 |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 89 | list $counters(total) $counters(static_main) |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 90 | } {0 0} |
| 91 | |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 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 | # |
drh | b99185f | 2016-03-18 00:19:48 +0000 | [diff] [blame] | 100 | ifcapable threadsafe1&&shared_cache { |
danielk1977 | a3f0659 | 2009-04-23 14:58:39 +0000 | [diff] [blame] | 101 | set enable_shared_cache [sqlite3_enable_shared_cache 1] |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 102 | foreach {mode mutexes} { |
| 103 | singlethread {} |
dan | 6d4fb83 | 2011-01-26 07:25:32 +0000 | [diff] [blame] | 104 | multithread { |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 105 | fast static_app1 static_app2 static_app3 |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 106 | static_lru static_main static_mem static_open |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 107 | static_prng static_pmem static_vfs1 static_vfs2 |
| 108 | static_vfs3 |
dan | 6d4fb83 | 2011-01-26 07:25:32 +0000 | [diff] [blame] | 109 | } |
| 110 | serialized { |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 111 | fast recursive static_app1 static_app2 |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 112 | static_app3 static_lru static_main static_mem |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 113 | static_open static_prng static_pmem static_vfs1 |
| 114 | static_vfs2 static_vfs3 |
dan | 6d4fb83 | 2011-01-26 07:25:32 +0000 | [diff] [blame] | 115 | } |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 116 | } { |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 117 | |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 118 | do_test mutex1.2.$mode.1 { |
| 119 | catch {db close} |
| 120 | sqlite3_shutdown |
| 121 | sqlite3_config $mode |
| 122 | } SQLITE_OK |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 123 | |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 124 | do_test mutex1.2.$mode.2 { |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 125 | sqlite3_initialize |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 126 | clear_mutex_counters |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 127 | sqlite3 db test.db -nomutex 0 -fullmutex 0 |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 128 | catchsql { CREATE TABLE abc(a, b, c) } |
| 129 | db eval { |
| 130 | INSERT INTO abc VALUES(1, 2, 3); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 131 | } |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 132 | } {} |
drh | 986d3b9 | 2011-01-18 16:13:27 +0000 | [diff] [blame] | 133 | ifcapable !memorymanage { |
| 134 | regsub { static_lru} $mutexes {} mutexes |
| 135 | } |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 136 | if {$mode ne "singlethread"} { |
| 137 | do_test mutex1.2.$mode.3 { |
| 138 | # |
| 139 | # NOTE: Make sure all the app and vfs mutexes get used. |
| 140 | # |
| 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 |
| 153 | } {} |
| 154 | } |
| 155 | do_test mutex1.2.$mode.4 { |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 156 | mutex_counters counters |
mistachkin | 28ae577 | 2015-07-03 23:11:36 +0000 | [diff] [blame] | 157 | |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 158 | set res [list] |
| 159 | foreach {key value} [array get counters] { |
| 160 | if {$key ne "total" && $value > 0} { |
| 161 | lappend res $key |
| 162 | } |
| 163 | } |
| 164 | lsort $res |
drh | c8d7567 | 2008-07-08 02:12:37 +0000 | [diff] [blame] | 165 | } [lsort $mutexes] |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 166 | } |
danielk1977 | a3f0659 | 2009-04-23 14:58:39 +0000 | [diff] [blame] | 167 | sqlite3_enable_shared_cache $enable_shared_cache |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 168 | |
danielk1977 | a3f0659 | 2009-04-23 14:58:39 +0000 | [diff] [blame] | 169 | # Open and use a connection in "nomutex" mode. Test that no recursive |
| 170 | # mutexes are obtained. |
drh | 85e9e22 | 2008-07-15 00:27:34 +0000 | [diff] [blame] | 171 | do_test mutex1.3.1 { |
| 172 | catch {db close} |
| 173 | clear_mutex_counters |
| 174 | sqlite3 db test.db -nomutex 1 |
| 175 | execsql { SELECT * FROM abc } |
| 176 | } {1 2 3 1 2 3 1 2 3} |
| 177 | do_test mutex1.3.2 { |
| 178 | mutex_counters counters |
| 179 | set counters(recursive) |
| 180 | } {0} |
| 181 | } |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 182 | |
danielk1977 | 5f6d026 | 2008-11-04 14:55:47 +0000 | [diff] [blame] | 183 | # Test the sqlite3_db_mutex() function. |
| 184 | # |
| 185 | do_test mutex1.4.1 { |
| 186 | catch {db close} |
| 187 | sqlite3 db test.db |
| 188 | enter_db_mutex db |
| 189 | db eval {SELECT 1, 2, 3} |
| 190 | } {1 2 3} |
| 191 | do_test mutex1.4.2 { |
| 192 | leave_db_mutex db |
| 193 | db eval {SELECT 1, 2, 3} |
| 194 | } {1 2 3} |
| 195 | do_test mutex1.4.3 { |
| 196 | catch {db close} |
| 197 | sqlite3 db test.db -nomutex 1 |
| 198 | enter_db_mutex db |
| 199 | db eval {SELECT 1, 2, 3} |
| 200 | } {1 2 3} |
| 201 | do_test mutex1.4.4 { |
| 202 | leave_db_mutex db |
| 203 | db eval {SELECT 1, 2, 3} |
| 204 | } {1 2 3} |
| 205 | |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 206 | do_test mutex1-X { |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 207 | catch {db close} |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 208 | sqlite3_shutdown |
| 209 | clear_mutex_counters |
| 210 | install_mutex_counters 0 |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 211 | sqlite3_initialize |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 212 | } {SQLITE_OK} |
| 213 | |
drh | 55b0cf0 | 2008-06-19 17:54:33 +0000 | [diff] [blame] | 214 | autoinstall_test_functions |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 215 | finish_test |