|
11 | 11 | #include "EventBroadcaster.h" |
12 | 12 | #include "EventBroadcasterEditor.h" |
13 | 13 |
|
14 | | -std::shared_ptr<void> EventBroadcaster::getZMQContext() { |
15 | | - // Note: C++11 guarantees that initialization of static local variables occurs exactly once, even |
16 | | - // if multiple threads attempt to initialize the same static local variable concurrently. |
| 14 | +EventBroadcaster::ZMQContext* EventBroadcaster::sharedContext = nullptr; |
| 15 | +CriticalSection EventBroadcaster::sharedContextLock{}; |
| 16 | + |
| 17 | +EventBroadcaster::ZMQContext::ZMQContext(const ScopedLock& lock) |
17 | 18 | #ifdef ZEROMQ |
18 | | - static const std::shared_ptr<void> ctx(zmq_ctx_new(), zmq_ctx_destroy); |
19 | | -#else |
20 | | - static const std::shared_ptr<void> ctx; |
| 19 | + : context(zmq_ctx_new()) |
| 20 | +#endif |
| 21 | +{ |
| 22 | + sharedContext = this; |
| 23 | +} |
| 24 | + |
| 25 | +// ZMQContext is a ReferenceCountedObject with a pointer in each instance's |
| 26 | +// socket pointer, so this only happens when the last instance is destroyed. |
| 27 | +EventBroadcaster::ZMQContext::~ZMQContext() |
| 28 | +{ |
| 29 | + ScopedLock lock(sharedContextLock); |
| 30 | + sharedContext = nullptr; |
| 31 | +#ifdef ZEROMQ |
| 32 | + zmq_ctx_destroy(context); |
| 33 | +#endif |
| 34 | +} |
| 35 | + |
| 36 | +void* EventBroadcaster::ZMQContext::createZMQSocket() |
| 37 | +{ |
| 38 | +#ifdef ZEROMQ |
| 39 | + jassert(context != nullptr); |
| 40 | + return zmq_socket(context, ZMQ_PUB); |
21 | 41 | #endif |
22 | | - return ctx; |
| 42 | +} |
| 43 | + |
| 44 | +EventBroadcaster::ZMQSocketPtr::ZMQSocketPtr() |
| 45 | + : std::unique_ptr<void, decltype(&closeZMQSocket)>(nullptr, &closeZMQSocket) |
| 46 | +{ |
| 47 | + ScopedLock lock(sharedContextLock); |
| 48 | + if (sharedContext == nullptr) |
| 49 | + { |
| 50 | + // first one, create the context |
| 51 | + context = new ZMQContext(lock); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + // use already-created context |
| 56 | + context = sharedContext; |
| 57 | + } |
| 58 | + |
| 59 | +#ifdef ZEROMQ |
| 60 | + reset(context->createZMQSocket()); |
| 61 | +#endif |
| 62 | +} |
| 63 | + |
| 64 | +EventBroadcaster::ZMQSocketPtr::~ZMQSocketPtr() |
| 65 | +{ |
| 66 | + // close the socket before the context might get destroyed. |
| 67 | + reset(nullptr); |
23 | 68 | } |
24 | 69 |
|
25 | 70 | int EventBroadcaster::unbindZMQSocket() |
@@ -70,8 +115,6 @@ void EventBroadcaster::reportActualListeningPort(int port) |
70 | 115 |
|
71 | 116 | EventBroadcaster::EventBroadcaster() |
72 | 117 | : GenericProcessor ("Event Broadcaster") |
73 | | - , zmqContext (getZMQContext()) |
74 | | - , zmqSocket (nullptr) |
75 | 118 | , listeningPort (0) |
76 | 119 | { |
77 | 120 | setProcessorType (PROCESSOR_TYPE_SINK); |
@@ -105,7 +148,7 @@ int EventBroadcaster::setListeningPort(int port, bool forceRestart) |
105 | 148 | #ifdef ZEROMQ |
106 | 149 | // unbind current socket (if any) to free up port |
107 | 150 | unbindZMQSocket(); |
108 | | - zmqSocketPtr newSocket(zmq_socket(zmqContext.get(), ZMQ_PUB)); |
| 151 | + ZMQSocketPtr newSocket; |
109 | 152 | auto editor = static_cast<EventBroadcasterEditor*>(getEditor()); |
110 | 153 | int status = 0; |
111 | 154 |
|
|
0 commit comments