Skip to content

Commit 3fba6f4

Browse files
authored
Merge pull request #278 from tne-lab/shared-resource-zmq
Use SharedResourcePointers for ZeroMQ contexts
2 parents e31f6f5 + d0b3baf commit 3fba6f4

4 files changed

Lines changed: 14 additions & 69 deletions

File tree

Source/Plugins/EventBroadcaster/EventBroadcaster.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,16 @@
1111
#include "EventBroadcaster.h"
1212
#include "EventBroadcasterEditor.h"
1313

14-
EventBroadcaster::ZMQContext* EventBroadcaster::sharedContext = nullptr;
15-
CriticalSection EventBroadcaster::sharedContextLock{};
16-
17-
EventBroadcaster::ZMQContext::ZMQContext(const ScopedLock& lock)
14+
EventBroadcaster::ZMQContext::ZMQContext()
1815
#ifdef ZEROMQ
1916
: context(zmq_ctx_new())
2017
#endif
21-
{
22-
sharedContext = this;
23-
}
18+
{}
2419

2520
// ZMQContext is a ReferenceCountedObject with a pointer in each instance's
2621
// socket pointer, so this only happens when the last instance is destroyed.
2722
EventBroadcaster::ZMQContext::~ZMQContext()
2823
{
29-
ScopedLock lock(sharedContextLock);
30-
sharedContext = nullptr;
3124
#ifdef ZEROMQ
3225
zmq_ctx_destroy(context);
3326
#endif
@@ -44,18 +37,6 @@ void* EventBroadcaster::ZMQContext::createZMQSocket()
4437
EventBroadcaster::ZMQSocketPtr::ZMQSocketPtr()
4538
: std::unique_ptr<void, decltype(&closeZMQSocket)>(nullptr, &closeZMQSocket)
4639
{
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-
5940
#ifdef ZEROMQ
6041
reset(context->createZMQSocket());
6142
#endif

Source/Plugins/EventBroadcaster/EventBroadcaster.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ class EventBroadcaster : public GenericProcessor
4444

4545

4646
private:
47-
class ZMQContext : public ReferenceCountedObject
47+
class ZMQContext
4848
{
4949
public:
50-
ZMQContext(const ScopedLock& lock);
51-
~ZMQContext() override;
50+
ZMQContext();
51+
~ZMQContext();
5252
void* createZMQSocket();
5353
private:
5454
void* context;
@@ -62,7 +62,7 @@ class EventBroadcaster : public GenericProcessor
6262
ZMQSocketPtr();
6363
~ZMQSocketPtr();
6464
private:
65-
ReferenceCountedObjectPtr<ZMQContext> context;
65+
SharedResourcePointer<ZMQContext> context;
6666
};
6767

6868
int unbindZMQSocket();
@@ -73,11 +73,6 @@ class EventBroadcaster : public GenericProcessor
7373
// called from getListeningPort() depending on success/failure of ZMQ operations
7474
void reportActualListeningPort(int port);
7575

76-
// share a "dumb" pointer that doesn't take part in reference counting.
77-
// want the context to be terminated by the time the static members are
78-
// destroyed (see: https://github.com/zeromq/libzmq/issues/1708)
79-
static ZMQContext* sharedContext;
80-
static CriticalSection sharedContextLock;
8176
ZMQSocketPtr zmqSocket;
8277
int listeningPort;
8378
};

Source/Plugins/NetworkEvents/NetworkEvents.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ const int MAX_MESSAGE_LENGTH = 64000;
3535
#include <unistd.h>
3636
#endif
3737

38-
NetworkEvents::ZMQContext* NetworkEvents::sharedContext = nullptr;
39-
CriticalSection NetworkEvents::sharedContextLock{};
40-
4138
NetworkEvents::NetworkEvents()
4239
: GenericProcessor ("Network Events")
4340
, Thread ("NetworkThread")
@@ -79,6 +76,7 @@ String NetworkEvents::getCurrPortString() const
7976

8077
void NetworkEvents::restartConnection()
8178
{
79+
requestedPort = boundPort.load();
8280
makeNewSocket = true;
8381
}
8482

@@ -430,21 +428,15 @@ String NetworkEvents::getPortString(uint16 port)
430428

431429
/*** ZMQContext ***/
432430

433-
NetworkEvents::ZMQContext::ZMQContext(const ScopedLock& lock)
431+
NetworkEvents::ZMQContext::ZMQContext()
434432
#ifdef ZEROMQ
435433
: context(zmq_ctx_new())
436434
#endif
437-
{
438-
// sharedContextLock should already be held here
439-
sharedContext = this;
440-
}
435+
{}
441436

442-
// ZMQContext is a ReferenceCountedObject with a pointer in each instance's
443-
// socket pointer, so this only happens when the last instance is destroyed.
437+
// only happens when the last SharedResourcePointer is destroyed.
444438
NetworkEvents::ZMQContext::~ZMQContext()
445439
{
446-
ScopedLock lock(sharedContextLock);
447-
sharedContext = nullptr;
448440
#ifdef ZEROMQ
449441
zmq_ctx_destroy(context);
450442
#endif
@@ -471,20 +463,6 @@ NetworkEvents::Responder::Responder(uint16 port)
471463
, boundPort (0)
472464
, lastErrno (0)
473465
{
474-
{
475-
ScopedLock lock(sharedContextLock);
476-
if (sharedContext == nullptr)
477-
{
478-
// first one, create the context
479-
context = new ZMQContext(lock);
480-
}
481-
else
482-
{
483-
// use already-created context
484-
context = sharedContext;
485-
}
486-
}
487-
488466
#ifdef ZEROMQ
489467
socket = context->createSocket();
490468
if (!socket)

Source/Plugins/NetworkEvents/NetworkEvents.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,12 @@ class NetworkEvents : public GenericProcessor
9090
int64 timestamp;
9191
};
9292

93-
class ZMQContext : public ReferenceCountedObject
93+
class ZMQContext
9494
{
9595
public:
96-
ZMQContext(const ScopedLock& lock);
97-
~ZMQContext() override;
96+
ZMQContext();
97+
~ZMQContext();
9898
void* createSocket();
99-
100-
typedef ReferenceCountedObjectPtr<ZMQContext> Ptr;
101-
10299
private:
103100
void* context;
104101

@@ -134,7 +131,7 @@ class NetworkEvents : public GenericProcessor
134131
int send(StringRef response);
135132

136133
private:
137-
ZMQContext::Ptr context;
134+
SharedResourcePointer<ZMQContext> context;
138135
void* socket;
139136
bool valid;
140137
uint16 boundPort;
@@ -161,12 +158,6 @@ class NetworkEvents : public GenericProcessor
161158
// get a representation of the given port for use on the editor
162159
static String getPortString(uint16 port);
163160

164-
// share a "dumb" pointer that doesn't take part in reference counting.
165-
// want the context to be terminated by the time the static members are
166-
// destroyed (see: https://github.com/zeromq/libzmq/issues/1708)
167-
static ZMQContext* sharedContext;
168-
static CriticalSection sharedContextLock;
169-
170161
std::atomic<bool> makeNewSocket; // port change or restart needed (depending on requestedPort)
171162
std::atomic<uint16> requestedPort; // never set by the thread; 0 means any free port
172163
std::atomic<uint16> boundPort; // only set by the thread; 0 means no connection

0 commit comments

Comments
 (0)