-
Notifications
You must be signed in to change notification settings - Fork 58
/
context.jl
120 lines (105 loc) · 3.13 KB
/
context.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
## Contexts ##
# the low-level context constructor
function _ctx_new()
p = lib.zmq_ctx_new()
if p == C_NULL
throw(StateError(jl_zmq_error_str()))
end
return p
end
mutable struct Context
data::Ptr{Cvoid}
# need to keep a list of weakrefs to sockets for this Context in order to
# close them before finalizing (otherwise zmq_ctx_term will hang)
sockets::Vector{WeakRef}
function Context()
zctx = new(_ctx_new(), WeakRef[])
finalizer(close, zctx)
return zctx
end
function Context(::UndefInitializer)
zctx = new(C_NULL, WeakRef[])
finalizer(close, zctx)
return zctx
end
end
function Context(f::Function, args...)
ctx = Context(args...)
try
f(ctx)
finally
close(ctx)
end
end
Base.unsafe_convert(::Type{Ptr{Cvoid}}, c::Context) = getfield(c, :data)
# define a global context that is initialized lazily
# and is used by default in Socket constructors, to
# save 99% of users from the low-level need to set up
# a context
const _context = Context(undef)
"""
context()
Return the default ZMQ context (of type `Context`), initializing
it if this has not been done already. (This context is automatically
closed when Julia exits.)
"""
function context()
if !isopen(_context)
setfield!(_context, :data, _ctx_new())
end
return _context
end
@deprecate Context(n::Integer) Context()
Base.isopen(ctx::Context) = getfield(ctx, :data) != C_NULL
function Base.close(ctx::Context)
if isopen(ctx) # don't close twice!
for w in getfield(ctx, :sockets)
s = w.value
if s isa Socket && isopen(s)
s.linger = 0 # allow socket to shut down immediately
close(s)
end
end
empty!(getfield(ctx, :sockets))
rc = lib.zmq_ctx_term(ctx)
setfield!(ctx, :data, C_NULL)
if rc != 0
throw(StateError(jl_zmq_error_str()))
end
end
end
@deprecate term(ctx::Context) close(ctx)
function _get(ctx::Context, option::Integer)
val = lib.zmq_ctx_get(ctx, option)
if val < 0
throw(StateError(jl_zmq_error_str()))
end
return val
end
function _set(ctx::Context, option::Integer, value::Integer)
rc = lib.zmq_ctx_set(ctx, option, value)
if rc != 0
throw(StateError(jl_zmq_error_str()))
end
end
const ctxopts = (:io_threads, :max_sockets, :ipv6)
Base.propertynames(::Context) = ctxopts
@eval function Base.getproperty(value::Context, name::Symbol)
$(propexpression(ctxopts) do p
:(_get(value, $(Symbol(uppercase(String(p))))))
end)
end
@eval function Base.setproperty!(value::Context, name::Symbol, x::Integer)
$(propexpression(ctxopts) do p
:(_set(value, $(Symbol(uppercase(String(p)))), x))
end)
return x
end
function Base.get(ctx::Context, option::Integer)
Base.depwarn("get(ctx, option) is deprecated; use ctx.option instead", :get)
return _get(ctx, option)
end
function set(ctx::Context, option::Integer, value::Integer)
Base.depwarn("set(ctx, option, val) is deprecated; use ctx.option = val instead", :set)
return _set(ctx, option, value)
end