blob: 4bdf769ad302c7c777de58d7b03a5f7c21061c2d [file] [log] [blame]
danielk19771a9ed0b2008-06-18 09:45:56 +00001# 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#
danielk1977a3f06592009-04-23 14:58:39 +000012# $Id: mutex1.test,v 1.20 2009/04/23 14:58:40 danielk1977 Exp $
danielk19771a9ed0b2008-06-18 09:45:56 +000013
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
danielk1977185eac92008-07-12 15:55:54 +000016
drh18472fa2008-10-07 15:25:48 +000017ifcapable !mutex {
18 finish_test
19 return
20}
danielk1977185eac92008-07-12 15:55:54 +000021if {[info exists tester_do_binarylog]} {
22 finish_test
23 return
24}
25
drh55b0cf02008-06-19 17:54:33 +000026sqlite3_reset_auto_extension
drh5807c4c2009-01-09 14:29:35 +000027clear_mutex_counters
danielk19771a9ed0b2008-06-18 09:45:56 +000028
29proc 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
danielk197771bc31c2008-06-26 08:29:34 +000041# is called it obtains the 'static_master' mutex 3 times and a recursive
42# mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops
43# that do not require any mutexes.
danielk19771a9ed0b2008-06-18 09:45:56 +000044#
45do_test mutex1-1.0 {
46 install_mutex_counters 1
47} {SQLITE_MISUSE}
48
49do_test mutex1-1.1 {
50 db close
51 install_mutex_counters 1
52} {SQLITE_MISUSE}
53
54do_test mutex1-1.2 {
55 sqlite3_shutdown
56 install_mutex_counters 1
57} {SQLITE_OK}
58
59do_test mutex1-1.3 {
60 install_mutex_counters 0
61} {SQLITE_OK}
62
63do_test mutex1-1.4 {
64 install_mutex_counters 1
65} {SQLITE_OK}
66
67do_test mutex1-1.5 {
68 mutex_counters counters
69 set counters(total)
70} {0}
71
72do_test mutex1-1.6 {
73 sqlite3_initialize
74} {SQLITE_OK}
75
76do_test mutex1-1.7 {
77 mutex_counters counters
danielk1977e339d652008-06-28 11:23:00 +000078 # list $counters(total) $counters(static_master)
79 expr {$counters(total)>0}
80} {1}
danielk19771a9ed0b2008-06-18 09:45:56 +000081
82do_test mutex1-1.8 {
83 clear_mutex_counters
84 sqlite3_initialize
85} {SQLITE_OK}
86
87do_test mutex1-1.9 {
88 mutex_counters counters
89 list $counters(total) $counters(static_master)
90} {0 0}
91
danielk197759f8c082008-06-18 17:09:10 +000092#-------------------------------------------------------------------------
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#
danielk1977a3f06592009-04-23 14:58:39 +0000100ifcapable threadsafe&&shared_cache {
101 set enable_shared_cache [sqlite3_enable_shared_cache 1]
danielk197701bf2992008-06-18 17:59:03 +0000102 foreach {mode mutexes} {
103 singlethread {}
dan6d4fb832011-01-26 07:25:32 +0000104 multithread {
105 fast static_lru static_master static_mem static_open static_prng
danb69577d2011-01-26 15:23:22 +0000106 static_pmem
dan6d4fb832011-01-26 07:25:32 +0000107 }
108 serialized {
danb69577d2011-01-26 15:23:22 +0000109 fast recursive static_lru static_master static_mem static_open
110 static_prng static_pmem
dan6d4fb832011-01-26 07:25:32 +0000111 }
danielk197701bf2992008-06-18 17:59:03 +0000112 } {
drhc8d75672008-07-08 02:12:37 +0000113
danielk197701bf2992008-06-18 17:59:03 +0000114 do_test mutex1.2.$mode.1 {
115 catch {db close}
116 sqlite3_shutdown
117 sqlite3_config $mode
118 } SQLITE_OK
danielk197771bc31c2008-06-26 08:29:34 +0000119
danielk197701bf2992008-06-18 17:59:03 +0000120 do_test mutex1.2.$mode.2 {
danielk197771bc31c2008-06-26 08:29:34 +0000121 sqlite3_initialize
danielk197701bf2992008-06-18 17:59:03 +0000122 clear_mutex_counters
drhd9da78a2009-03-24 15:08:09 +0000123 sqlite3 db test.db -nomutex 0 -fullmutex 0
danielk197701bf2992008-06-18 17:59:03 +0000124 catchsql { CREATE TABLE abc(a, b, c) }
125 db eval {
126 INSERT INTO abc VALUES(1, 2, 3);
danielk197759f8c082008-06-18 17:09:10 +0000127 }
danielk197701bf2992008-06-18 17:59:03 +0000128 } {}
drh986d3b92011-01-18 16:13:27 +0000129 ifcapable !memorymanage {
130 regsub { static_lru} $mutexes {} mutexes
131 }
danielk197701bf2992008-06-18 17:59:03 +0000132 do_test mutex1.2.$mode.3 {
133 mutex_counters counters
134
135 set res [list]
136 foreach {key value} [array get counters] {
137 if {$key ne "total" && $value > 0} {
138 lappend res $key
139 }
140 }
141 lsort $res
drhc8d75672008-07-08 02:12:37 +0000142 } [lsort $mutexes]
danielk197701bf2992008-06-18 17:59:03 +0000143 }
danielk1977a3f06592009-04-23 14:58:39 +0000144 sqlite3_enable_shared_cache $enable_shared_cache
danielk19771a9ed0b2008-06-18 09:45:56 +0000145
danielk1977a3f06592009-04-23 14:58:39 +0000146 # Open and use a connection in "nomutex" mode. Test that no recursive
147 # mutexes are obtained.
drh85e9e222008-07-15 00:27:34 +0000148 do_test mutex1.3.1 {
149 catch {db close}
150 clear_mutex_counters
151 sqlite3 db test.db -nomutex 1
152 execsql { SELECT * FROM abc }
153 } {1 2 3 1 2 3 1 2 3}
154 do_test mutex1.3.2 {
155 mutex_counters counters
156 set counters(recursive)
157 } {0}
158}
danielk19779a6284c2008-07-10 17:52:49 +0000159
danielk19775f6d0262008-11-04 14:55:47 +0000160# Test the sqlite3_db_mutex() function.
161#
162do_test mutex1.4.1 {
163 catch {db close}
164 sqlite3 db test.db
165 enter_db_mutex db
166 db eval {SELECT 1, 2, 3}
167} {1 2 3}
168do_test mutex1.4.2 {
169 leave_db_mutex db
170 db eval {SELECT 1, 2, 3}
171} {1 2 3}
172do_test mutex1.4.3 {
173 catch {db close}
174 sqlite3 db test.db -nomutex 1
175 enter_db_mutex db
176 db eval {SELECT 1, 2, 3}
177} {1 2 3}
178do_test mutex1.4.4 {
179 leave_db_mutex db
180 db eval {SELECT 1, 2, 3}
181} {1 2 3}
182
danielk19771a9ed0b2008-06-18 09:45:56 +0000183do_test mutex1-X {
danielk197701bf2992008-06-18 17:59:03 +0000184 catch {db close}
danielk19771a9ed0b2008-06-18 09:45:56 +0000185 sqlite3_shutdown
186 clear_mutex_counters
187 install_mutex_counters 0
danielk197701bf2992008-06-18 17:59:03 +0000188 sqlite3_initialize
danielk19771a9ed0b2008-06-18 09:45:56 +0000189} {SQLITE_OK}
190
drh55b0cf02008-06-19 17:54:33 +0000191autoinstall_test_functions
danielk19771a9ed0b2008-06-18 09:45:56 +0000192finish_test