thread_common.tcl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # 2007 September 10
  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. #
  12. # $Id: thread_common.tcl,v 1.5 2009/03/26 14:48:07 danielk1977 Exp $
  13. if {[info exists ::thread_procs]} {
  14. return 0
  15. }
  16. # The following script is sourced by every thread spawned using
  17. # [sqlthread spawn]:
  18. set thread_procs {
  19. # Execute the supplied SQL using database handle $::DB.
  20. #
  21. proc execsql {sql} {
  22. set rc SQLITE_LOCKED
  23. while {$rc eq "SQLITE_LOCKED"
  24. || $rc eq "SQLITE_BUSY"
  25. || $rc eq "SQLITE_SCHEMA"} {
  26. set res [list]
  27. enter_db_mutex $::DB
  28. set err [catch {
  29. set ::STMT [sqlite3_prepare_v2 $::DB $sql -1 dummy_tail]
  30. } msg]
  31. if {$err == 0} {
  32. while {[set rc [sqlite3_step $::STMT]] eq "SQLITE_ROW"} {
  33. for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
  34. lappend res [sqlite3_column_text $::STMT 0]
  35. }
  36. }
  37. set rc [sqlite3_finalize $::STMT]
  38. } else {
  39. if {[lindex $msg 0]=="(6)"} {
  40. set rc SQLITE_LOCKED
  41. } else {
  42. set rc SQLITE_ERROR
  43. }
  44. }
  45. if {[string first locked [sqlite3_errmsg $::DB]]>=0} {
  46. set rc SQLITE_LOCKED
  47. }
  48. if {$rc ne "SQLITE_OK"} {
  49. set errtxt "$rc - [sqlite3_errmsg $::DB] (debug1)"
  50. }
  51. leave_db_mutex $::DB
  52. if {$rc eq "SQLITE_LOCKED" || $rc eq "SQLITE_BUSY"} {
  53. #sqlthread parent "puts \"thread [sqlthread id] is busy. rc=$rc\""
  54. after 200
  55. } else {
  56. #sqlthread parent "puts \"thread [sqlthread id] ran $sql\""
  57. }
  58. }
  59. if {$rc ne "SQLITE_OK"} {
  60. error $errtxt
  61. }
  62. set res
  63. }
  64. proc do_test {name script result} {
  65. set res [eval $script]
  66. if {$res ne $result} {
  67. error "$name failed: expected \"$result\" got \"$res\""
  68. }
  69. }
  70. }
  71. proc thread_spawn {varname args} {
  72. sqlthread spawn $varname [join $args {;}]
  73. }
  74. # Return true if this build can run the multi-threaded tests.
  75. #
  76. proc run_thread_tests {{print_warning 0}} {
  77. ifcapable !mutex {
  78. set zProblem "SQLite build is not threadsafe"
  79. }
  80. ifcapable mutex_noop {
  81. set zProblem "SQLite build uses SQLITE_MUTEX_NOOP"
  82. }
  83. if {[info commands sqlthread] eq ""} {
  84. set zProblem "SQLite build is not threadsafe"
  85. }
  86. if {![info exists ::tcl_platform(threaded)]} {
  87. set zProblem "Linked against a non-threadsafe Tcl build"
  88. }
  89. if {[info exists zProblem]} {
  90. puts "WARNING: Multi-threaded tests skipped: $zProblem"
  91. return 0
  92. }
  93. set ::run_thread_tests_called 1
  94. return 1;
  95. }
  96. return 0