add ffi/unsafe/global and switch openssl to use it

Continues the move away from using the FFI to access unsafe Racket
functionality.
This commit is contained in:
Matthew Flatt 2017-05-26 17:49:16 -06:00
parent bf83d1126d
commit 87c0ca84a8
6 changed files with 60 additions and 12 deletions

View File

@ -14,6 +14,7 @@
@include-section["custodian.scrbl"] @include-section["custodian.scrbl"]
@include-section["atomic.scrbl"] @include-section["atomic.scrbl"]
@include-section["try-atomic.scrbl"] @include-section["try-atomic.scrbl"]
@include-section["global.scrbl"]
@include-section["objc.scrbl"] @include-section["objc.scrbl"]
@include-section["ns.scrbl"] @include-section["ns.scrbl"]
@include-section["com.scrbl"] @include-section["com.scrbl"]

View File

@ -0,0 +1,31 @@
#lang scribble/doc
@(require "utils.rkt"
(for-label ffi/unsafe/global))
@title{Process-Wide Registration}
@defmodule[ffi/unsafe/global]{The
@racketmodname[ffi/unsafe/global] library provides a utility
registering information that spans all places in the Racket
process.}
@history[#:added "6.9.0.5"]
@defproc[(register-process-global [key bytes?]
[val cpointer?])
cpointer?]{
Gets or sets a value in a process-global table (i.e., shared across
multiple places, if any).
If @racket[val] is @racket[#f], the current mapping for @racket[key]
is reported.
If @racket[val] is not @racket[#f], and no value has been installed
for @racket[key], then the value is installed and @racket[#f] is
returned. If a value has already been installed, then no new value is
installed and the old value is returned. The given @racket[val] must
not refer to garbage-collected memory.
This function is intended for infrequent use with a small number of
keys.}

View File

@ -1,5 +1,6 @@
#lang scribble/doc #lang scribble/doc
@(require "utils.rkt") @(require "utils.rkt"
(for-label ffi/unsafe/custodian))
@title{Custodians} @title{Custodians}
@ -64,7 +65,10 @@ most one custodian.
If @var{m} (or the current custodian if @var{m} is @cpp{NULL}) is shut If @var{m} (or the current custodian if @var{m} is @cpp{NULL}) is shut
down, then @var{f} is called immediately, and the result is down, then @var{f} is called immediately, and the result is
@cpp{NULL}.} @cpp{NULL}.
See also @racket[register-custodian-shutdown] from
@racketmodname[ffi/unsafe/custodian].}
@function[(Scheme_Custodian_Reference* scheme_add_managed_close_on_exit @function[(Scheme_Custodian_Reference* scheme_add_managed_close_on_exit
[Scheme_Custodian* m] [Scheme_Custodian* m]
@ -94,7 +98,10 @@ Checks whether @var{m} is already shut down, and raises an error if
Removes @var{o} from the management of its custodian. The @var{mref} Removes @var{o} from the management of its custodian. The @var{mref}
argument must be a value returned by @cpp{scheme_add_managed} or argument must be a value returned by @cpp{scheme_add_managed} or
@cpp{NULL}.} @cpp{NULL}.
See also @racket[unregister-custodian-shutdown] from
@racketmodname[ffi/unsafe/custodian].}
@function[(void scheme_close_managed @function[(void scheme_close_managed
[Scheme_Custodian* m])]{ [Scheme_Custodian* m])]{

View File

@ -1,5 +1,6 @@
#lang scribble/doc #lang scribble/doc
@(require "utils.rkt") @(require "utils.rkt"
(for-label ffi/unsafe/global))
@title{Miscellaneous Utilities} @title{Miscellaneous Utilities}
@ -392,7 +393,10 @@ value is returned. The given @var{val} must not refer to garbage-collected
memory. memory.
This function is intended for infrequent use with a small number of This function is intended for infrequent use with a small number of
keys.} keys.
See also @racket[register-process-global] from
@racketmodname[ffi/unsafe/global].}
@function[(void* scheme_jit_find_code_end @function[(void* scheme_jit_find_code_end
[void* p])]{ [void* p])]{

View File

@ -0,0 +1,8 @@
#lang racket/base
(require (only-in '#%unsafe
unsafe-register-process-global))
(provide register-process-global)
(define (register-process-global bstr val)
(unsafe-register-process-global bstr val))

View File

@ -28,6 +28,7 @@ TO DO:
ffi/unsafe/define ffi/unsafe/define
ffi/unsafe/atomic ffi/unsafe/atomic
ffi/unsafe/alloc ffi/unsafe/alloc
ffi/unsafe/global
ffi/file ffi/file
ffi/unsafe/custodian ffi/unsafe/custodian
racket/list racket/list
@ -1687,10 +1688,6 @@ TO DO:
(define ssl-available? (and libssl #t)) (define ssl-available? (and libssl #t))
(define scheme_register_process_global
(and ssl-available?
(get-ffi-obj 'scheme_register_process_global #f (_fun _string _pointer -> _pointer))))
(when ssl-available? (when ssl-available?
;; Make sure only one place tries to initialize OpenSSL, ;; Make sure only one place tries to initialize OpenSSL,
;; and wait in case some other place is currently initializing ;; and wait in case some other place is currently initializing
@ -1698,18 +1695,18 @@ TO DO:
(begin (begin
(start-atomic) (start-atomic)
(let* ([done (cast 1 _scheme _pointer)] (let* ([done (cast 1 _scheme _pointer)]
[v (scheme_register_process_global "OpenSSL-support-initializing" done)]) [v (register-process-global #"OpenSSL-support-initializing" done)])
(if v (if v
;; Some other place is initializing: ;; Some other place is initializing:
(begin (begin
(end-atomic) (end-atomic)
(let loop () (let loop ()
(unless (scheme_register_process_global "OpenSSL-support-initialized" #f) (unless (register-process-global #"OpenSSL-support-initialized" #f)
(sleep 0.01) ;; busy wait! --- this should be rare (sleep 0.01) ;; busy wait! --- this should be rare
(loop)))) (loop))))
;; This place must initialize: ;; This place must initialize:
(begin (begin
(SSL_library_init) (SSL_library_init)
(SSL_load_error_strings) (SSL_load_error_strings)
(scheme_register_process_global "OpenSSL-support-initialized" done) (register-process-global #"OpenSSL-support-initialized" done)
(end-atomic)))))) (end-atomic))))))