Skip to content

Commit ae76d78

Browse files
author
Chongkai Zhu
committed
use register-finalizer-and-custodian-shutdown
1 parent fe2724e commit ae76d78

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

light.ss

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
(require "mathlink.ss"
44
"translation.ss"
55
racket/contract
6-
(only-in ffi/unsafe register-finalizer)
76
ffi/unsafe/custodian)
87

98
(provide/contract (MathKernel
10-
(->* () () #:rest (listof string?) any))
9+
(-> string? ... MathLink?))
1110
(MathEval
1211
(->* ((flat-rec-contract Mexp
1312
number? boolean? symbol? string? void? eof-object?
@@ -164,18 +163,16 @@
164163
"/Applications/Mathematica.app/Contents/MacOS/MathKernel -mathlink"))))
165164
(arg
166165
(let ((link (apply MLOpen arg)))
167-
(set-MathLink-ref! link
168-
(register-custodian-shutdown link MLClose #:at-exit? #t))
169-
(register-finalizer link MathExit)
166+
(register-finalizer-and-custodian-shutdown link MLClose #:at-exit? #t)
170167
(current-mathlink link)
171168
link))))
172169

173170
(define (MathEval exp (link (or (current-mathlink) (MathKernel))))
174171
(call-with-semaphore (MathLink-sema link)
175172
(lambda ()
176-
(unless (MathLink-ref link)
177-
(mathlink-error "MathEval: MathLink is closed"))
178173
(let ((lp (MathLink-lp link)))
174+
(unless lp
175+
(mathlink-error "MathEval: MathLink is closed"))
179176
(MLPutFunction lp #"EvaluatePacket" 1)
180177
(MLPut lp exp)
181178
(MLEndPacket lp)
@@ -186,13 +183,6 @@
186183
(()
187184
(cond ((current-mathlink) => MathExit)))
188185
((link)
189-
(call-with-semaphore (MathLink-sema link)
190-
(lambda ()
191-
(when (eq? link (current-mathlink))
192-
(current-mathlink #f))
193-
(let ((ref (MathLink-ref link)))
194-
(when ref
195-
(set-MathLink-ref! link #f)
196-
(unregister-custodian-shutdown link ref)
197-
(MLClose link)
198-
(set-phantom-bytes! (MathLink-phantom link) 0))))))))
186+
(when (eq? link (current-mathlink))
187+
(current-mathlink #f))
188+
(MLClose link))))

mathlink.ss

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(provide (except-out (all-defined-out)
66
mathlink))
77

8-
(define-struct MathLink (ep lp (ref #:mutable) sema phantom))
8+
(define-struct MathLink (ep (lp #:mutable) sema))
99

1010
(define-struct (exn:fail:mathlink exn:fail) () #:transparent)
1111
(define-syntax-rule (mathlink-error str)
@@ -39,19 +39,22 @@
3939
-> (if lp
4040
(begin (MLNextPacket lp)
4141
(MLNewPacket lp)
42-
(make-MathLink ep lp #t (make-semaphore 1) (make-phantom-bytes 65536)))
42+
(make-MathLink ep lp (make-semaphore 1)))
4343
(mathlink-error "MathKernel: MathLink Open Error"))))))
4444

4545
(define MLClose
46-
(let ((MLClose (get-ffi-obj 'WSClose mathlink
46+
(let ((close (get-ffi-obj 'WSClose mathlink
4747
(_fun _pointer -> _void)))
48-
(MLDeinitialize
48+
(deinitialize
4949
(get-ffi-obj 'WSDeinitialize mathlink
5050
(_fun _pointer -> _void))))
5151
(lambda (link)
52-
(MLPutMessage (MathLink-lp link) 1)
53-
(MLClose (MathLink-lp link))
54-
(MLDeinitialize (MathLink-ep link)))))
52+
(let ((lp (MathLink-lp link)))
53+
(when lp
54+
(MLPutMessage (MathLink-lp link) 1)
55+
(close (MathLink-lp link))
56+
(deinitialize (MathLink-ep link))
57+
(set-MathLink-lp! link #f))))))
5558

5659
(define MLPutFunction
5760
(get-ffi-obj 'WSPutFunction mathlink

0 commit comments

Comments
 (0)