Skip to content

Instantly share code, notes, and snippets.

@endobson
Created September 30, 2017 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endobson/6efdca16885162b51b76ab385e9afa5b to your computer and use it in GitHub Desktop.
Save endobson/6efdca16885162b51b76ab385e9afa5b to your computer and use it in GitHub Desktop.
Finalizers don't run reliably from other places
#lang racket/base
(require
racket/place
ffi/unsafe
ffi/unsafe/alloc)
(define (other-place)
(place p
(define (f) (malloc 'raw (* 1024 1024 1024)))
(define port (current-output-port))
(define (dealloc x)
(displayln 'dealloc-orig port)
(place-channel-put p 'dealloc)
(free x))
(let ([v (((allocator dealloc) f))])
(memset v 0 (* 1024 1024 1024))
(place-channel-put p 'ready)
(displayln (ptr-ref v _int))
(sync p) ;; 'msg
(place-channel-put p 'msg-response))
(collect-garbage)
;; This sleep determines if the finalizer thread gets time to run
;(sleep 1)
(displayln 'exit)))
(define (run)
(define p (other-place))
(displayln (sync p))
(displayln (place-channel-put/get p 'msg))
(displayln (sync p)))
(module+ main
(run))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment