Skip to content

Commit 3fd6c06

Browse files
Use OncePerProcess when possible (#483)
1 parent 8ccd416 commit 3fd6c06

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed

lib/mtl/buffer.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,18 @@ function MTLBuffer(dev::MTLDevice, bytesize::Integer, ptr::Ptr;
4949
return MTLBuffer(ptr)
5050
end
5151

52-
const _page_size::Ref{Int} = Ref{Int}(0)
53-
function page_size()
54-
if _page_size[] == 0
55-
_page_size[] = Int(ccall(:getpagesize, Cint, ()))
52+
@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
53+
const page_size = OncePerProcess{Int}() do
54+
Int(ccall(:getpagesize, Cint, ()))
55+
end
56+
else
57+
const _page_size::Ref{Int} = Ref{Int}(0)
58+
function page_size()
59+
if _page_size[] == 0
60+
_page_size[] = Int(ccall(:getpagesize, Cint, ()))
61+
end
62+
_page_size[]
5663
end
57-
_page_size[]
5864
end
5965

6066
function can_alloc_nocopy(ptr::Ptr, bytesize::Integer)

src/initialization.jl

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
# Starts at `nothing`. Only false when it's determined
2-
const _functional = Ref{Union{Nothing,Bool}}(false)
1+
@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
2+
const functional = OncePerProcess{Bool}() do
3+
try
4+
dev = device()
5+
return supports_family(dev, MTL.MTLGPUFamilyApple7) &&
6+
supports_family(dev, MTL.MTLGPUFamilyMetal3)
7+
catch
8+
return false
9+
end
10+
end
11+
else
12+
# Becomes `nothing` once it has been determined that the device is on macOS
13+
const _functional = Ref{Union{Nothing,Bool}}(false)
314

4-
function functional()
5-
if isnothing(_functional[])
6-
dev = device()
15+
function functional()
16+
if isnothing(_functional[])
17+
dev = device()
718

8-
_functional[] =
9-
supports_family(dev, MTL.MTLGPUFamilyApple7) &&
10-
supports_family(dev, MTL.MTLGPUFamilyMetal3)
19+
_functional[] =
20+
supports_family(dev, MTL.MTLGPUFamilyApple7) &&
21+
supports_family(dev, MTL.MTLGPUFamilyMetal3)
22+
end
23+
_functional[]
1124
end
12-
_functional[]
1325
end
1426

1527
function __init__()
@@ -51,7 +63,9 @@ function __init__()
5163

5264
# Successful loading of CoreGraphics means there's a
5365
# chance the graphics device is supported
54-
_functional[] = nothing
66+
if @isdefined _functional
67+
_functional[] = nothing # VERSION <= v"1.12.0-DEV.1421"
68+
end
5569
catch err
5670
@error "Failed to load Metal" exception=(err,catch_backtrace())
5771
return

src/version.jl

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,46 @@
1515
parse(VersionNumber, verstr)
1616
end
1717

18-
const _darwin_version = Ref{VersionNumber}()
19-
"""
18+
@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
19+
const darwin_version = OncePerProcess{VersionNumber}() do
20+
_syscall_version("kern.osrelease")
21+
end
22+
const macos_version = OncePerProcess{VersionNumber}() do
23+
_syscall_version("kern.osproductversion")
24+
end
25+
else
26+
const _darwin_version = Ref{VersionNumber}()
27+
function darwin_version()
28+
if !isassigned(_darwin_version)
29+
_darwin_version[] = _syscall_version("kern.osrelease")
30+
end
31+
_darwin_version[]
32+
end
33+
34+
const _macos_version = Ref{VersionNumber}()
35+
function macos_version()
36+
if !isassigned(_macos_version)
37+
_macos_version[] = _syscall_version("kern.osproductversion")
38+
end
39+
_macos_version[]
40+
end
41+
end
42+
43+
@doc """
2044
Metal.darwin_version() -> VersionNumber
2145
2246
Returns the host Darwin kernel version.
2347
2448
See also [`Metal.macos_version`](@ref).
25-
"""
26-
function darwin_version()
27-
if !isassigned(_darwin_version)
28-
_darwin_version[] = _syscall_version("kern.osrelease")
29-
end
30-
_darwin_version[]
31-
end
49+
""" darwin_version
3250

33-
const _macos_version = Ref{VersionNumber}()
34-
"""
51+
@doc """
3552
Metal.macos_version() -> VersionNumber
3653
3754
Returns the host macOS version.
3855
3956
See also [`Metal.darwin_version`](@ref).
40-
"""
41-
function macos_version()
42-
if !isassigned(_macos_version)
43-
_macos_version[] = _syscall_version("kern.osproductversion")
44-
end
45-
_macos_version[]
46-
end
57+
""" macos_version
4758

4859

4960
## support queries

0 commit comments

Comments
 (0)