Posted to tcl by pooryorick at Mon Apr 18 14:56:09 GMT 2016view raw

  1. if {$initialThreadReader eq $finalThreadWriter && $finalThreadWriter ne "" && [lsearch -exact $args "|"] != -1} {
  2. set copyThreadChannel $initialThreadReader
  3. set copyThread [thread::create]
  4. lassign [chan pipe] readerPipeReader readerPipeWriter
  5. lassign [chan pipe] writerPipeReader writerPipeWriter
  6. foreach fd [list $readerPipeReader $readerPipeWriter $writerPipeReader $writerPipeWriter] {
  7. fconfigure $fd -translation binary
  8. }
  9. thread::transfer $copyThreadChannel $copyThread
  10. thread::transfer [lindex readerpipe 1] $copyThread
  11. thread::transfer [lindex writerpipe 0] $copyThread
  12. thread::send $copyThread [string map [list @copy@ [
  13. list $copyThreadChannel] @reader@ [
  14. list $writerPipeReader] @writer@ [list $readerPipeWriter]] {
  15.  
  16. proc copyComplete {var args} {
  17. incr ::copyComplete
  18. set sock [set ::$var]
  19. unset ::$var
  20. close $sock
  21. }
  22.  
  23. proc terminate {} {
  24. catch {close @writer@}
  25. catch {close @reader@}
  26. thread::detach @copy@
  27. }
  28.  
  29. fcopy @copy@ @writer@ -command [
  30. list copyComplete @writer@]
  31. fcopy @reader@ @copy -command [
  32. list copyComplete @reader@]
  33. }]
  34. set initialThreadReader $readerPipeReader
  35. set finalThreadWriter $writerPipeWriter
  36. }
  37.