Skip to content

Commit

Permalink
親スレッド終了時に子スレッドも強制終了するように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Mar 16, 2011
1 parent 74c34a5 commit 73aafc0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pofo.asd
Expand Up @@ -3,7 +3,7 @@
(defsystem pofo
:name "pofo"
:author "Takeru Ohta"
:version "0.0.1"
:version "0.0.2"
:description "Port Forwarding"
:serial t
:depends-on (:sb-bsd-sockets)
Expand Down
2 changes: 0 additions & 2 deletions pofo.lisp
@@ -1,6 +1,5 @@
(in-package :pofo)

;; TODO: make-two-way-stream を試してみても良いかも
(defun forward (in out)
(while (listen in)
(write-byte (read-byte in) out))
Expand Down Expand Up @@ -52,4 +51,3 @@
(if thread
(sb-thread:make-thread (lambda () (main)))
(main))))

24 changes: 17 additions & 7 deletions socket.lisp
Expand Up @@ -40,10 +40,20 @@
;; NOTE: multi-thread version
;; TODO: single-thread版も試したい
(defmacro do-accept ((client server) &body body)
`(loop
(let ((,client (sb-bsd-sockets:socket-accept ,server)))
(sb-thread:make-thread
(lambda ()
(unwind-protect
(locally ,@body)
(sb-bsd-sockets:socket-close ,client)))))))
(let ((threads (gensym)))
`(let ((,threads '()))
(unwind-protect
(loop FOR ,client = (sb-bsd-sockets:socket-accept ,server)
DO
(push
(sb-thread:make-thread
(lambda ()
(unwind-protect
(locally ,@body)
(sb-bsd-sockets:socket-close ,client))))
,threads)
;; XXX:
(setf ,threads (delete-if-not #'sb-thread:thread-alive-p ,threads)))
(loop FOR thread IN ,threads
WHEN (sb-thread:thread-alive-p thread)
DO (sb-thread:terminate-thread thread))))))

0 comments on commit 73aafc0

Please sign in to comment.