summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ctx.cpp9
-rw-r--r--src/ctx.hpp6
-rw-r--r--src/socket_base.cpp9
-rw-r--r--src/socket_base.hpp6
-rw-r--r--src/zmq.cpp32
5 files changed, 46 insertions, 16 deletions
diff --git a/src/ctx.cpp b/src/ctx.cpp
index 9cbb9de..2758729 100644
--- a/src/ctx.cpp
+++ b/src/ctx.cpp
@@ -36,6 +36,7 @@
#endif
zmq::ctx_t::ctx_t (uint32_t io_threads_) :
+ tag (0xbadcafe0),
terminating (false)
{
int rc;
@@ -78,6 +79,11 @@ zmq::ctx_t::ctx_t (uint32_t io_threads_) :
zmq_assert (rc == 0);
}
+bool zmq::ctx_t::check_tag ()
+{
+ return tag == 0xbadcafe0;
+}
+
zmq::ctx_t::~ctx_t ()
{
// Check that there are no remaining sockets.
@@ -99,6 +105,9 @@ zmq::ctx_t::~ctx_t ()
// needed as mailboxes themselves were deallocated with their
// corresponding io_thread/socket objects.
free (slots);
+
+ // Remove the tag, so that the object is considered dead.
+ tag = 0xdeadbeef;
}
int zmq::ctx_t::terminate ()
diff --git a/src/ctx.hpp b/src/ctx.hpp
index c6ea4ce..33d5dad 100644
--- a/src/ctx.hpp
+++ b/src/ctx.hpp
@@ -60,6 +60,9 @@ namespace zmq
// of I/O thread pool to create.
ctx_t (uint32_t io_threads_);
+ // Returns false if object is not a context.
+ bool check_tag ();
+
// This function is called when user invokes zmq_term. If there are
// no more sockets open it'll cause all the infrastructure to be shut
// down. If there are open sockets still, the deallocation happens
@@ -98,6 +101,9 @@ namespace zmq
~ctx_t ();
+ // Used to check whether the object is a context.
+ uint32_t tag;
+
// Sockets belonging to this context. We need the list so that
// we can notify the sockets when zmq_term() is called. The sockets
// will return ETERM then.
diff --git a/src/socket_base.cpp b/src/socket_base.cpp
index b1d1142..c9b5c31 100644
--- a/src/socket_base.cpp
+++ b/src/socket_base.cpp
@@ -61,6 +61,11 @@
#include "xpub.hpp"
#include "xsub.hpp"
+bool zmq::socket_base_t::check_tag ()
+{
+ return tag == 0xbaddecaf;
+}
+
zmq::socket_base_t *zmq::socket_base_t::create (int type_, class ctx_t *parent_,
uint32_t tid_)
{
@@ -110,6 +115,7 @@ zmq::socket_base_t *zmq::socket_base_t::create (int type_, class ctx_t *parent_,
zmq::socket_base_t::socket_base_t (ctx_t *parent_, uint32_t tid_) :
own_t (parent_, tid_),
+ tag (0xbaddecaf),
ctx_terminated (false),
destroyed (false),
last_tsc (0),
@@ -126,6 +132,9 @@ zmq::socket_base_t::~socket_base_t ()
sessions_sync.lock ();
zmq_assert (sessions.empty ());
sessions_sync.unlock ();
+
+ // Mark the socket as dead.
+ tag = 0xdeadbeef;
}
zmq::mailbox_t *zmq::socket_base_t::get_mailbox ()
diff --git a/src/socket_base.hpp b/src/socket_base.hpp
index 15ac83c..333cddd 100644
--- a/src/socket_base.hpp
+++ b/src/socket_base.hpp
@@ -50,6 +50,9 @@ namespace zmq
public:
+ // Returns false if object is not a socket.
+ bool check_tag ();
+
// Create a socket of a specified type.
static socket_base_t *create (int type_, class ctx_t *parent_,
uint32_t tid_);
@@ -136,6 +139,9 @@ namespace zmq
private:
+ // Used to check whether the object is a socket.
+ uint32_t tag;
+
// If true, associated context was already terminated.
bool ctx_terminated;
diff --git a/src/zmq.cpp b/src/zmq.cpp
index c65e166..0f536ac 100644
--- a/src/zmq.cpp
+++ b/src/zmq.cpp
@@ -261,7 +261,7 @@ void *zmq_init (int io_threads_)
int zmq_term (void *ctx_)
{
- if (!ctx_) {
+ if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
errno = EFAULT;
return -1;
}
@@ -287,7 +287,7 @@ int zmq_term (void *ctx_)
void *zmq_socket (void *ctx_, int type_)
{
- if (!ctx_) {
+ if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
errno = EFAULT;
return NULL;
}
@@ -296,8 +296,8 @@ void *zmq_socket (void *ctx_, int type_)
int zmq_close (void *s_)
{
- if (!s_) {
- errno = EFAULT;
+ if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
+ errno = ENOTSOCK;
return -1;
}
((zmq::socket_base_t*) s_)->close ();
@@ -307,8 +307,8 @@ int zmq_close (void *s_)
int zmq_setsockopt (void *s_, int option_, const void *optval_,
size_t optvallen_)
{
- if (!s_) {
- errno = EFAULT;
+ if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
+ errno = ENOTSOCK;
return -1;
}
return (((zmq::socket_base_t*) s_)->setsockopt (option_, optval_,
@@ -317,8 +317,8 @@ int zmq_setsockopt (void *s_, int option_, const void *optval_,
int zmq_getsockopt (void *s_, int option_, void *optval_, size_t *optvallen_)
{
- if (!s_) {
- errno = EFAULT;
+ if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
+ errno = ENOTSOCK;
return -1;
}
return (((zmq::socket_base_t*) s_)->getsockopt (option_, optval_,
@@ -327,8 +327,8 @@ int zmq_getsockopt (void *s_, int option_, void *optval_, size_t *optvallen_)
int zmq_bind (void *s_, const char *addr_)
{
- if (!s_) {
- errno = EFAULT;
+ if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
+ errno = ENOTSOCK;
return -1;
}
return (((zmq::socket_base_t*) s_)->bind (addr_));
@@ -336,8 +336,8 @@ int zmq_bind (void *s_, const char *addr_)
int zmq_connect (void *s_, const char *addr_)
{
- if (!s_) {
- errno = EFAULT;
+ if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
+ errno = ENOTSOCK;
return -1;
}
return (((zmq::socket_base_t*) s_)->connect (addr_));
@@ -393,8 +393,8 @@ int zmq_recv (void *s_, void *buf_, size_t len_, int flags_)
int zmq_sendmsg (void *s_, zmq_msg_t *msg_, int flags_)
{
- if (!s_) {
- errno = EFAULT;
+ if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
+ errno = ENOTSOCK;
return -1;
}
int sz = (int) zmq_msg_size (msg_);
@@ -406,8 +406,8 @@ int zmq_sendmsg (void *s_, zmq_msg_t *msg_, int flags_)
int zmq_recvmsg (void *s_, zmq_msg_t *msg_, int flags_)
{
- if (!s_) {
- errno = EFAULT;
+ if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
+ errno = ENOTSOCK;
return -1;
}
int rc = (((zmq::socket_base_t*) s_)->recv (msg_, flags_));