mutexscript.tcl
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. # See the file LICENSE for redistribution information.
  2. #
  3. # Copyright (c) 1996, 1997, 1998, 1999, 2000
  4. # Sleepycat Software.  All rights reserved.
  5. #
  6. # $Id: mutexscript.tcl,v 11.12 2000/11/21 22:14:56 dda Exp $
  7. #
  8. # Random mutex tester.
  9. # Usage: mutexscript dir numiters mlocks sleepint degree
  10. # dir: dir in which all the mutexes live.
  11. # numiters: Total number of iterations.
  12. # nmutex: Total number of mutexes.
  13. # sleepint: Maximum sleep interval.
  14. # degree: Maximum number of locks to acquire at once
  15. source ./include.tcl
  16. source $test_path/test.tcl
  17. source $test_path/testutils.tcl
  18. set usage "mutexscript dir numiters nmutex sleepint degree"
  19. # Verify usage
  20. if { $argc != 5 } {
  21. puts stderr "FAIL:[timestamp] Usage: $usage"
  22. exit
  23. }
  24. # Initialize arguments
  25. set dir [lindex $argv 0]
  26. set numiters [ lindex $argv 1 ]
  27. set nmutex [ lindex $argv 2 ]
  28. set sleepint [ lindex $argv 3 ]
  29. set degree [ lindex $argv 4 ]
  30. set locker [pid]
  31. set mypid [sanitized_pid]
  32. # Initialize seed
  33. global rand_init
  34. berkdb srand $rand_init
  35. puts -nonewline "Mutexscript: Beginning execution for $locker:"
  36. puts " $numiters $nmutex $sleepint $degree"
  37. flush stdout
  38. # Open the environment and the mutex
  39. set e [berkdb env -create -mode 0644 -lock -home $dir]
  40. error_check_good evn_open [is_valid_env $e] TRUE
  41. set mutex [$e mutex 0644 $nmutex]
  42. error_check_good mutex_init [is_valid_mutex $mutex $e] TRUE
  43. # Sleep for awhile to make sure that everyone has gotten in
  44. tclsleep 5
  45. for { set iter 0 } { $iter < $numiters } { incr iter } {
  46. set nlocks [berkdb random_int 1 $degree]
  47. # We will always lock objects in ascending order to avoid
  48. # deadlocks.
  49. set lastobj 1
  50. set mlist {}
  51. for { set lnum 0 } { $lnum < $nlocks } { incr lnum } {
  52. # Pick lock parameters
  53. set obj [berkdb random_int $lastobj [expr $nmutex - 1]]
  54. set lastobj [expr $obj + 1]
  55. puts "[timestamp] $locker $lnum: $obj"
  56. # Do get, set its val to own pid, and then add to list
  57. error_check_good mutex_get:$obj [$mutex get $obj] 0
  58. error_check_good mutex_setval:$obj [$mutex setval $obj $mypid] 0
  59. lappend mlist $obj
  60. if {$lastobj >= $nmutex} {
  61. break
  62. }
  63. }
  64. # Pick sleep interval
  65. tclsleep [ berkdb random_int 1 $sleepint ]
  66. # Now release locks
  67. foreach i $mlist {
  68. error_check_good mutex_getval:$i [$mutex getval $i] $mypid
  69. error_check_good mutex_setval:$i 
  70.     [$mutex setval $i [expr 0 - $mypid]] 0
  71. error_check_good mutex_release:$i [$mutex release $i] 0
  72. }
  73. puts "[timestamp] $locker released mutexes"
  74. flush stdout
  75. }
  76. puts "[timestamp] $locker Complete"
  77. flush stdout