summaryrefslogtreecommitdiff
path: root/src/safe_object.cpp
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@fastmq.commkdir>2009-07-29 12:07:54 +0200
committerMartin Sustrik <sustrik@fastmq.commkdir>2009-07-29 12:07:54 +0200
commit4ed70a930202b103e7e80b8dc925e0aaa4622595 (patch)
treeaeed881ce17629f81b7c90f7d675aac8ecf69d4f /src/safe_object.cpp
initial commit
Diffstat (limited to 'src/safe_object.cpp')
-rw-r--r--src/safe_object.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/safe_object.cpp b/src/safe_object.cpp
new file mode 100644
index 0000000..a7c72e6
--- /dev/null
+++ b/src/safe_object.cpp
@@ -0,0 +1,76 @@
+/*
+ Copyright (c) 2007-2009 FastMQ Inc.
+
+ This file is part of 0MQ.
+
+ 0MQ is free software; you can redistribute it and/or modify it under
+ the terms of the Lesser GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ 0MQ is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ Lesser GNU General Public License for more details.
+
+ You should have received a copy of the Lesser GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "safe_object.hpp"
+
+zs::safe_object_t::safe_object_t (class dispatcher_t *dispatcher_,
+ int thread_slot_) :
+ object_t (dispatcher_, thread_slot_),
+ processed_seqnum (0),
+ terminating (false)
+{
+}
+
+zs::safe_object_t::safe_object_t (object_t *parent_) :
+ object_t (parent_),
+ processed_seqnum (0),
+ terminating (false)
+{
+}
+
+void zs::safe_object_t::inc_seqnum ()
+{
+ // This function is called from the sender thread to ensure that this
+ // object will still exist when the command sent to it arrives in the
+ // destination thread.
+ sent_seqnum.add (1);
+}
+
+void zs::safe_object_t::process_command (struct command_t &cmd_)
+{
+ object_t::process_command (cmd_);
+
+ // Adjust sequence number of the last processed command.
+ processed_seqnum++;
+
+ // If we are already in the termination phase and all commands sent to
+ // this object are processed, it's safe to deallocate it.
+ if (terminating && sent_seqnum.get () == processed_seqnum)
+ delete this;
+}
+
+void zs::safe_object_t::terminate ()
+{
+ // Wait till all commands sent to this session are processed.
+ terminating = true;
+
+ // If there's no pending command we can deallocate the session
+ // straight saway.
+ if (sent_seqnum.get () == processed_seqnum)
+ delete this;
+}
+
+bool zs::safe_object_t::is_terminating ()
+{
+ return terminating;
+}
+
+zs::safe_object_t::~safe_object_t ()
+{
+}