2007-09-10 11:35:47 +04:00
|
|
|
# 2007 September 10
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
2009-03-26 17:48:07 +03:00
|
|
|
# $Id: thread_common.tcl,v 1.5 2009/03/26 14:48:07 danielk1977 Exp $
|
2007-09-10 11:35:47 +04:00
|
|
|
|
2009-03-26 17:48:07 +03:00
|
|
|
if {[info exists ::thread_procs]} {
|
|
|
|
return 0
|
2007-09-10 11:35:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# The following script is sourced by every thread spawned using
|
|
|
|
# [sqlthread spawn]:
|
|
|
|
set thread_procs {
|
|
|
|
|
|
|
|
# Execute the supplied SQL using database handle $::DB.
|
|
|
|
#
|
|
|
|
proc execsql {sql} {
|
|
|
|
|
|
|
|
set rc SQLITE_LOCKED
|
2007-09-10 14:53:01 +04:00
|
|
|
while {$rc eq "SQLITE_LOCKED"
|
|
|
|
|| $rc eq "SQLITE_BUSY"
|
|
|
|
|| $rc eq "SQLITE_SCHEMA"} {
|
2007-09-10 11:35:47 +04:00
|
|
|
set res [list]
|
2007-09-10 14:53:01 +04:00
|
|
|
|
2009-01-19 20:40:12 +03:00
|
|
|
enter_db_mutex $::DB
|
2007-09-10 14:53:01 +04:00
|
|
|
set err [catch {
|
|
|
|
set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 dummy_tail]
|
|
|
|
} msg]
|
|
|
|
|
|
|
|
if {$err == 0} {
|
|
|
|
while {[set rc [sqlite3_step $::STMT]] eq "SQLITE_ROW"} {
|
|
|
|
for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
|
|
|
|
lappend res [sqlite3_column_text $::STMT 0]
|
|
|
|
}
|
2007-09-10 11:35:47 +04:00
|
|
|
}
|
2007-09-10 14:53:01 +04:00
|
|
|
set rc [sqlite3_finalize $::STMT]
|
|
|
|
} else {
|
2009-03-17 18:39:31 +03:00
|
|
|
if {[lindex $msg 0]=="(6)"} {
|
2007-09-10 14:53:01 +04:00
|
|
|
set rc SQLITE_LOCKED
|
|
|
|
} else {
|
|
|
|
set rc SQLITE_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[string first locked [sqlite3_errmsg $::DB]]>=0} {
|
|
|
|
set rc SQLITE_LOCKED
|
2007-09-10 11:35:47 +04:00
|
|
|
}
|
2009-01-19 20:40:12 +03:00
|
|
|
if {$rc ne "SQLITE_OK"} {
|
|
|
|
set errtxt "$rc - [sqlite3_errmsg $::DB] (debug1)"
|
|
|
|
}
|
|
|
|
leave_db_mutex $::DB
|
2007-09-10 11:35:47 +04:00
|
|
|
|
2007-09-10 14:53:01 +04:00
|
|
|
if {$rc eq "SQLITE_LOCKED" || $rc eq "SQLITE_BUSY"} {
|
2009-01-19 20:40:12 +03:00
|
|
|
#sqlthread parent "puts \"thread [sqlthread id] is busy. rc=$rc\""
|
|
|
|
after 200
|
|
|
|
} else {
|
|
|
|
#sqlthread parent "puts \"thread [sqlthread id] ran $sql\""
|
2007-09-10 11:35:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$rc ne "SQLITE_OK"} {
|
2009-01-19 20:40:12 +03:00
|
|
|
error $errtxt
|
2007-09-10 11:35:47 +04:00
|
|
|
}
|
|
|
|
set res
|
|
|
|
}
|
|
|
|
|
|
|
|
proc do_test {name script result} {
|
|
|
|
set res [eval $script]
|
|
|
|
if {$res ne $result} {
|
|
|
|
error "$name failed: expected \"$result\" got \"$res\""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc thread_spawn {varname args} {
|
2010-04-13 15:35:01 +04:00
|
|
|
sqlthread spawn $varname [join $args {;}]
|
2007-09-10 11:35:47 +04:00
|
|
|
}
|
|
|
|
|
2009-03-26 17:48:07 +03:00
|
|
|
# Return true if this build can run the multi-threaded tests.
|
|
|
|
#
|
|
|
|
proc run_thread_tests {{print_warning 0}} {
|
|
|
|
ifcapable !mutex {
|
|
|
|
set zProblem "SQLite build is not threadsafe"
|
|
|
|
}
|
2011-04-07 14:09:00 +04:00
|
|
|
ifcapable mutex_noop {
|
|
|
|
set zProblem "SQLite build uses SQLITE_MUTEX_NOOP"
|
|
|
|
}
|
2009-03-26 17:48:07 +03:00
|
|
|
if {[info commands sqlthread] eq ""} {
|
|
|
|
set zProblem "SQLite build is not threadsafe"
|
|
|
|
}
|
|
|
|
if {![info exists ::tcl_platform(threaded)]} {
|
|
|
|
set zProblem "Linked against a non-threadsafe Tcl build"
|
|
|
|
}
|
|
|
|
if {[info exists zProblem]} {
|
2010-07-06 15:26:15 +04:00
|
|
|
puts "WARNING: Multi-threaded tests skipped: $zProblem"
|
2009-03-26 17:48:07 +03:00
|
|
|
return 0
|
|
|
|
}
|
2010-07-06 15:26:15 +04:00
|
|
|
set ::run_thread_tests_called 1
|
2009-03-26 17:48:07 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-10 11:35:47 +04:00
|
|
|
return 0
|
2009-03-26 17:48:07 +03:00
|
|
|
|