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 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame^] | 12 | # $Id: mutex1.test,v 1.5 2008/06/26 08:29:35 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 |
drh | 55b0cf0 | 2008-06-19 17:54:33 +0000 | [diff] [blame] | 16 | sqlite3_reset_auto_extension |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 17 | |
| 18 | proc mutex_counters {varname} { |
| 19 | upvar $varname var |
| 20 | set var(total) 0 |
| 21 | foreach {name value} [read_mutex_counters] { |
| 22 | set var($name) $value |
| 23 | incr var(total) $value |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | #------------------------------------------------------------------------- |
| 28 | # Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if |
| 29 | # is called at the wrong time. And that the first time sqlite3_initialize |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame^] | 30 | # is called it obtains the 'static_master' mutex 3 times and a recursive |
| 31 | # mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops |
| 32 | # that do not require any mutexes. |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 33 | # |
| 34 | do_test mutex1-1.0 { |
| 35 | install_mutex_counters 1 |
| 36 | } {SQLITE_MISUSE} |
| 37 | |
| 38 | do_test mutex1-1.1 { |
| 39 | db close |
| 40 | install_mutex_counters 1 |
| 41 | } {SQLITE_MISUSE} |
| 42 | |
| 43 | do_test mutex1-1.2 { |
| 44 | sqlite3_shutdown |
| 45 | install_mutex_counters 1 |
| 46 | } {SQLITE_OK} |
| 47 | |
| 48 | do_test mutex1-1.3 { |
| 49 | install_mutex_counters 0 |
| 50 | } {SQLITE_OK} |
| 51 | |
| 52 | do_test mutex1-1.4 { |
| 53 | install_mutex_counters 1 |
| 54 | } {SQLITE_OK} |
| 55 | |
| 56 | do_test mutex1-1.5 { |
| 57 | mutex_counters counters |
| 58 | set counters(total) |
| 59 | } {0} |
| 60 | |
| 61 | do_test mutex1-1.6 { |
| 62 | sqlite3_initialize |
| 63 | } {SQLITE_OK} |
| 64 | |
| 65 | do_test mutex1-1.7 { |
| 66 | mutex_counters counters |
| 67 | list $counters(total) $counters(static_master) |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame^] | 68 | } {6 3} |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 69 | |
| 70 | do_test mutex1-1.8 { |
| 71 | clear_mutex_counters |
| 72 | sqlite3_initialize |
| 73 | } {SQLITE_OK} |
| 74 | |
| 75 | do_test mutex1-1.9 { |
| 76 | mutex_counters counters |
| 77 | list $counters(total) $counters(static_master) |
| 78 | } {0 0} |
| 79 | |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 80 | #------------------------------------------------------------------------- |
| 81 | # Tests mutex1-2.* test the three thread-safety related modes that |
| 82 | # can be selected using sqlite3_config: |
| 83 | # |
| 84 | # * Serialized mode, |
| 85 | # * Multi-threaded mode, |
| 86 | # * Single-threaded mode. |
| 87 | # |
| 88 | |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 89 | ifcapable threadsafe { |
| 90 | foreach {mode mutexes} { |
| 91 | singlethread {} |
| 92 | multithread {fast static_master static_mem static_prng} |
| 93 | serialized {fast recursive static_master static_mem static_prng} |
| 94 | } { |
| 95 | do_test mutex1.2.$mode.1 { |
| 96 | catch {db close} |
| 97 | sqlite3_shutdown |
| 98 | sqlite3_config $mode |
| 99 | } SQLITE_OK |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame^] | 100 | |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 101 | do_test mutex1.2.$mode.2 { |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame^] | 102 | sqlite3_initialize |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 103 | clear_mutex_counters |
| 104 | sqlite3 db test.db |
| 105 | catchsql { CREATE TABLE abc(a, b, c) } |
| 106 | db eval { |
| 107 | INSERT INTO abc VALUES(1, 2, 3); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 108 | } |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 109 | } {} |
| 110 | |
| 111 | do_test mutex1.2.$mode.3 { |
| 112 | mutex_counters counters |
| 113 | |
| 114 | set res [list] |
| 115 | foreach {key value} [array get counters] { |
| 116 | if {$key ne "total" && $value > 0} { |
| 117 | lappend res $key |
| 118 | } |
| 119 | } |
| 120 | lsort $res |
| 121 | } $mutexes |
| 122 | } |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 123 | } |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 124 | |
| 125 | do_test mutex1-X { |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 126 | catch {db close} |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 127 | sqlite3_shutdown |
| 128 | clear_mutex_counters |
| 129 | install_mutex_counters 0 |
danielk1977 | 01bf299 | 2008-06-18 17:59:03 +0000 | [diff] [blame] | 130 | sqlite3_initialize |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 131 | } {SQLITE_OK} |
| 132 | |
drh | 55b0cf0 | 2008-06-19 17:54:33 +0000 | [diff] [blame] | 133 | autoinstall_test_functions |
danielk1977 | 1a9ed0b | 2008-06-18 09:45:56 +0000 | [diff] [blame] | 134 | finish_test |