Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
reset/shift と guard の組み合わせでメモリリークする件の調査
;;
;; pcdemo3_debug.scm
;; 2020-6-4 v1.02
;;
(use data.queue)
(use gauche.partcont)
;; for debug
(define *dbg-level* 3) ; (bitwise setting (e.g. 3 is error+info)
; ; =0:none, =1:error, =2:info)
(define (dbg-print dbg-level . args)
(when (logtest *dbg-level* dbg-level)
(apply format (current-error-port) args)))
(define *err-raise* #f) ; raise error
(define *sleep-time* ; (msec)
(if (>= *dbg-level* 2) 500 0))
;; pcdemo3 + debug
(define queue (make-queue))
;(define queue (make-mtqueue))
(define (wrap-1 thunk)
(lambda ()
(define id (gensym))
;; Add guard here!!
(dbg-print 2 "wrap-1 id=~d guard before~%" id)
(guard (e (else (dbg-print 1 "wrap-1 id=~d guard catched~%" id) (raise e)))
(dbg-print 2 "wrap-1 id=~d reset before~%" id)
(reset
(dbg-print 2 "wrap-1 id=~d thunk before~%" id)
(thunk)
(dbg-print 2 "wrap-1 id=~d thunk after~%" id))
(dbg-print 2 "wrap-1 id=~d reset after~%" id))
(dbg-print 2 "wrap-1 id=~d guard after~%" id)))
(define (wrap-2 thunk)
(lambda ()
(define id (gensym))
;; Add guard here!!
(dbg-print 2 "wrap-2 id=~d guard before~%" id)
(guard (e (else (dbg-print 1 "wrap-2 id=~d guard catched~%" id) (raise e)))
(dbg-print 2 "wrap-2 id=~d reset before~%" id)
(reset
(dbg-print 2 "wrap-2 id=~d thunk before~%" id)
(thunk)
(dbg-print 2 "wrap-2 id=~d thunk after~%" id))
(dbg-print 2 "wrap-2 id=~d reset after~%" id))
(dbg-print 2 "wrap-2 id=~d guard after~%" id)))
(define (yield)
(define id (gensym))
(dbg-print 2 "yield id=~d shift before~%" id)
(shift cont
(enqueue! queue (wrap-2 cont)))
(dbg-print 2 "yield id=~d shift after~%" id))
(define loop-func-1
(lambda ()
(define counter-1 0)
;; while-1
(while #t
(inc! counter-1)
(dbg-print 2 "loop-func-1 while-1 counter-1=~d~%" counter-1)
(when (> *sleep-time* 0)
(sys-nanosleep (* *sleep-time* 1e6)))
(when (= counter-1 100000)
(when *err-raise* (error "xxxxx"))
(set! counter-1 0)
(format #t "~s~%" (gc-stat)))
(yield))))
(define loop-func-2
(lambda ()
(define counter-2 0)
;; while-2
(while #t
(inc! counter-2)
(dbg-print 2 "===== loop-func-2 while-2 counter-2=~d~%" counter-2)
(let1 next (dequeue! queue #f)
(when next
(dbg-print 2 "loop-func-2 next before counter-2=~d~%" counter-2)
(next)
(dbg-print 2 "loop-func-2 next after counter-2=~d~%" counter-2)
)))))
(define (main args)
;(disasm wrap-1)
;(disasm wrap-2)
;(disasm yield)
;(disasm loop-func-1)
;(disasm loop-func-2)
;(exit)
(enqueue! queue (wrap-1 loop-func-1))
(loop-func-2)
0)
> gosh pcdemo3_debug.scm
===== loop-func-2 while-2 counter-2=1
loop-func-2 next before counter-2=1
wrap-1 id=G50 guard before
wrap-1 id=G50 reset before
wrap-1 id=G50 thunk before
loop-func-1 while-1 counter-1=1
yield id=G51 shift before
wrap-1 id=G50 reset after
wrap-1 id=G50 guard after
loop-func-2 next after counter-2=1
===== loop-func-2 while-2 counter-2=2
loop-func-2 next before counter-2=2
wrap-2 id=G52 guard before
wrap-2 id=G52 reset before
wrap-2 id=G52 thunk before
yield id=G51 shift after
loop-func-1 while-1 counter-1=2
yield id=G53 shift before
wrap-2 id=G52 thunk after
wrap-2 id=G52 reset after
wrap-2 id=G52 guard after
loop-func-2 next after counter-2=2
===== loop-func-2 while-2 counter-2=3
loop-func-2 next before counter-2=3
wrap-2 id=G54 guard before
wrap-2 id=G54 reset before
wrap-2 id=G54 thunk before
yield id=G53 shift after
loop-func-1 while-1 counter-1=3
yield id=G55 shift before
wrap-2 id=G54 thunk after
wrap-2 id=G54 reset after
wrap-2 id=G54 guard after
loop-func-2 next after counter-2=3
===== loop-func-2 while-2 counter-2=4
loop-func-2 next before counter-2=4
wrap-2 id=G56 guard before
wrap-2 id=G56 reset before
wrap-2 id=G56 thunk before
yield id=G55 shift after
loop-func-1 while-1 counter-1=4
yield id=G57 shift before
wrap-2 id=G56 thunk after
wrap-2 id=G56 reset after
wrap-2 id=G56 guard after
loop-func-2 next after counter-2=4
===== loop-func-2 while-2 counter-2=5
loop-func-2 next before counter-2=5
wrap-2 id=G58 guard before
wrap-2 id=G58 reset before
wrap-2 id=G58 thunk before
yield id=G57 shift after
loop-func-1 while-1 counter-1=5
yield id=G59 shift before
wrap-2 id=G58 thunk after
wrap-2 id=G58 reset after
wrap-2 id=G58 guard after
loop-func-2 next after counter-2=5
===== loop-func-2 while-2 counter-2=6
loop-func-2 next before counter-2=6
wrap-2 id=G60 guard before
wrap-2 id=G60 reset before
wrap-2 id=G60 thunk before
yield id=G59 shift after
loop-func-1 while-1 counter-1=6
yield id=G61 shift before
wrap-2 id=G60 thunk after
wrap-2 id=G60 reset after
wrap-2 id=G60 guard after
loop-func-2 next after counter-2=6
===== loop-func-2 while-2 counter-2=7
loop-func-2 next before counter-2=7
wrap-2 id=G62 guard before
wrap-2 id=G62 reset before
wrap-2 id=G62 thunk before
yield id=G61 shift after
loop-func-1 while-1 counter-1=7
yield id=G63 shift before
wrap-2 id=G62 thunk after
wrap-2 id=G62 reset after
wrap-2 id=G62 guard after
loop-func-2 next after counter-2=7
===== loop-func-2 while-2 counter-2=8
loop-func-2 next before counter-2=8
wrap-2 id=G64 guard before
wrap-2 id=G64 reset before
wrap-2 id=G64 thunk before
yield id=G63 shift after
loop-func-1 while-1 counter-1=8
yield id=G65 shift before
wrap-2 id=G64 thunk after
wrap-2 id=G64 reset after
wrap-2 id=G64 guard after
loop-func-2 next after counter-2=8
===== loop-func-2 while-2 counter-2=9
loop-func-2 next before counter-2=9
wrap-2 id=G66 guard before
wrap-2 id=G66 reset before
wrap-2 id=G66 thunk before
yield id=G65 shift after
loop-func-1 while-1 counter-1=9
yield id=G67 shift before
wrap-2 id=G66 thunk after
wrap-2 id=G66 reset after
wrap-2 id=G66 guard after
loop-func-2 next after counter-2=9
===== loop-func-2 while-2 counter-2=10
loop-func-2 next before counter-2=10
wrap-2 id=G68 guard before
wrap-2 id=G68 reset before
wrap-2 id=G68 thunk before
yield id=G67 shift after
loop-func-1 while-1 counter-1=10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment