sl@0: # 2007 September 10 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # $Id: thread_common.tcl,v 1.2 2007/09/10 10:53:02 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: if {[info commands sqlthread] eq ""} { sl@0: puts -nonewline "Skipping thread-safety tests - " sl@0: puts " not running a threadsafe sqlite/tcl build" sl@0: puts -nonewline "Both SQLITE_THREADSAFE and TCL_THREADS must be defined when" sl@0: puts " building testfixture" sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # The following script is sourced by every thread spawned using sl@0: # [sqlthread spawn]: sl@0: set thread_procs { sl@0: sl@0: # Execute the supplied SQL using database handle $::DB. sl@0: # sl@0: proc execsql {sql} { sl@0: sl@0: set rc SQLITE_LOCKED sl@0: while {$rc eq "SQLITE_LOCKED" sl@0: || $rc eq "SQLITE_BUSY" sl@0: || $rc eq "SQLITE_SCHEMA"} { sl@0: set res [list] sl@0: sl@0: set err [catch { sl@0: set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 dummy_tail] sl@0: } msg] sl@0: sl@0: if {$err == 0} { sl@0: while {[set rc [sqlite3_step $::STMT]] eq "SQLITE_ROW"} { sl@0: for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} { sl@0: lappend res [sqlite3_column_text $::STMT 0] sl@0: } sl@0: } sl@0: set rc [sqlite3_finalize $::STMT] sl@0: } else { sl@0: if {[string first (6) $msg]} { sl@0: set rc SQLITE_LOCKED sl@0: } else { sl@0: set rc SQLITE_ERROR sl@0: } sl@0: } sl@0: sl@0: if {[string first locked [sqlite3_errmsg $::DB]]>=0} { sl@0: set rc SQLITE_LOCKED sl@0: } sl@0: sl@0: if {$rc eq "SQLITE_LOCKED" || $rc eq "SQLITE_BUSY"} { sl@0: #puts -nonewline "([sqlthread id] $rc)" sl@0: #flush stdout sl@0: after 20 sl@0: } sl@0: } sl@0: sl@0: if {$rc ne "SQLITE_OK"} { sl@0: error "$rc - [sqlite3_errmsg $::DB]" sl@0: } sl@0: set res sl@0: } sl@0: sl@0: proc do_test {name script result} { sl@0: set res [eval $script] sl@0: if {$res ne $result} { sl@0: error "$name failed: expected \"$result\" got \"$res\"" sl@0: } sl@0: } sl@0: } sl@0: sl@0: proc thread_spawn {varname args} { sl@0: sqlthread spawn $varname [join $args ;] sl@0: } sl@0: sl@0: return 0