Posted to tcl by aku at Wed Mar 27 20:17:03 GMT 2019view raw

  1. #!/bin/bash
  2. # ######################################################################
  3. ## Synchronization functions (Barrier waiting for a number of subprocesses)
  4. #
  5. ## Example of Usage
  6. #
  7. ## barrier start foo
  8. ## ( ... ; barrier pass foo P1 ) & ;# spawn sub-process 1
  9. ## ( ... ; barrier pass foo P2 ) & ;# spawn sub-process 2
  10. ## ...
  11. ## ( ... ; barrier pass foo Pn ) & ;# spawn sub-process n
  12. ## barrier wait foo P1 P2 ... Pn ;# waits until all of P1 ... Pn have passed.
  13. #
  14.  
  15. function barrier_start () {
  16. code="$1"
  17. rm -f ${code}.*
  18. }
  19.  
  20. function barrier_pass () {
  21. code="$1"
  22. process="$2"
  23. touch ${code}.${process}
  24. }
  25.  
  26. function barrier_wait () {
  27. code="$1"
  28. shift
  29. processes="$@"
  30. while true
  31. do
  32. #debug
  33. #echo ___________/
  34. #ls ${code}.* 2> /dev/null
  35.  
  36. ok=1
  37. for p in $processes
  38. do
  39. if test ! -f ${code}.${p}
  40. then
  41. ok=0
  42. fi
  43. done
  44. if test $ok -eq 1 ; then break ; fi
  45. # Not everything has passed yet. Wait more
  46. sleep 1
  47. done
  48. rm ${code}.*
  49. }
  50.  
  51. cmd="$1";shift
  52. case $cmd in
  53. start) barrier_start "$@" ;;
  54. wait) barrier_wait "$@" ;;
  55. pass) barrier_pass "$@" ;;
  56. *) echo 1>2 "Unknown command $cmd, expected on of start, pass, or wait" ;;
  57. esac
  58.