2007-09-10 07:35:47 +00: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 14:48:07 +00:00
# $Id: thread_common.tcl,v 1.5 2009/03/26 14:48:07 danielk1977 Exp $
2007-09-10 07:35:47 +00:00
2009-03-26 14:48:07 +00:00
if { [ info exists : : thread_procs] } {
return 0
2007-09-10 07:35:47 +00: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 10:53:01 +00:00
while { $rc eq " S Q L I T E _ L O C K E D "
|| $rc eq " S Q L I T E _ B U S Y "
|| $rc eq " S Q L I T E _ S C H E M A " } {
2007-09-10 07:35:47 +00:00
set res [ list ]
2007-09-10 10:53:01 +00:00
2009-01-19 17:40:12 +00:00
enter_db_mutex $::DB
2007-09-10 10:53:01 +00: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 " S Q L I T E _ R O W " } {
for { set i 0 } { $i < [ sqlite3_column_count $::STMT ] } { incr i} {
lappend res [ sqlite3_column_text $::STMT 0 ]
}
2007-09-10 07:35:47 +00:00
}
2007-09-10 10:53:01 +00:00
set rc [ sqlite3_finalize $::STMT ]
} else {
2009-03-17 15:39:31 +00:00
if { [ lindex $msg 0 ] == " ( 6 ) " } {
2007-09-10 10:53:01 +00: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 07:35:47 +00:00
}
2009-01-19 17:40:12 +00:00
if { $rc ne " S Q L I T E _ O K " } {
set errtxt " $ r c - [ s q l i t e 3 _ e r r m s g $ : : D B ] ( d e b u g 1 ) "
}
leave_db_mutex $::DB
2007-09-10 07:35:47 +00:00
2007-09-10 10:53:01 +00:00
if { $rc eq " S Q L I T E _ L O C K E D " || $rc eq " S Q L I T E _ B U S Y " } {
2009-01-19 17:40:12 +00: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 07:35:47 +00:00
}
}
if { $rc ne " S Q L I T E _ O K " } {
2009-01-19 17:40:12 +00:00
error $errtxt
2007-09-10 07:35:47 +00:00
}
set res
}
proc do_test { name script result} {
set res [ eval $script ]
if { $res ne $result } {
error " $ n a m e f a i l e d : e x p e c t e d \" $ r e s u l t \" g o t \" $ r e s \" "
}
}
}
proc thread_spawn { varname args} {
2010-04-13 11:35:01 +00:00
sqlthread spawn $varname [ join $args { ; } ]
2007-09-10 07:35:47 +00:00
}
2009-03-26 14:48:07 +00:00
# Return true if this build can run the multi-threaded tests.
#
proc run_thread_tests { { print_warning 0 } } {
ifcapable ! mutex {
set zProblem " S Q L i t e b u i l d i s n o t t h r e a d s a f e "
}
2011-04-07 10:09:00 +00:00
ifcapable mutex_noop {
set zProblem " S Q L i t e b u i l d u s e s S Q L I T E _ M U T E X _ N O O P "
}
2009-03-26 14:48:07 +00:00
if { [ info commands sqlthread] eq " " } {
set zProblem " S Q L i t e b u i l d i s n o t t h r e a d s a f e "
}
if { ! [ info exists : : tcl_platform( threaded ) ] } {
set zProblem " L i n k e d a g a i n s t a n o n - t h r e a d s a f e T c l b u i l d "
}
if { [ info exists zProblem] } {
2010-07-06 11:26:15 +00:00
puts " W A R N I N G : M u l t i - t h r e a d e d t e s t s s k i p p e d : $ z P r o b l e m "
2009-03-26 14:48:07 +00:00
return 0
}
2010-07-06 11:26:15 +00:00
set : : run_thread_tests_called 1
2009-03-26 14:48:07 +00:00
return 1 ;
}
2007-09-10 07:35:47 +00:00
return 0
2009-03-26 14:48:07 +00:00