From 4ed70a930202b103e7e80b8dc925e0aaa4622595 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Wed, 29 Jul 2009 12:07:54 +0200 Subject: initial commit --- src/safe_object.hpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/safe_object.hpp (limited to 'src/safe_object.hpp') diff --git a/src/safe_object.hpp b/src/safe_object.hpp new file mode 100644 index 0000000..efa0d2f --- /dev/null +++ b/src/safe_object.hpp @@ -0,0 +1,68 @@ +/* + 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 . +*/ + +#ifndef __ZS_SAFE_OBJECT_HPP_INCLUDED__ +#define __ZS_SAFE_OBJECT_HPP_INCLUDED__ + +#include "object.hpp" +#include "atomic_counter.hpp" + +namespace zs +{ + + // Same as object_t with the exception of termination mechanism. While + // object_t is destroyed immediately on terminate (assuming that the caller + // have ensured that there are no more commands for the object on the + // fly), safe_object_t switches into termination mode and waits for all + // the on-the-fly commands to be delivered before it deallocates itself. + + class safe_object_t : public object_t + { + public: + + safe_object_t (class dispatcher_t *dispatcher_, int thread_slot_); + safe_object_t (object_t *parent_); + + void inc_seqnum (); + void process_command (struct command_t &cmd_); + + protected: + + void terminate (); + bool is_terminating (); + + virtual ~safe_object_t (); + + private: + + // Sequence number of the last command sent to the object and last + // command processed by the object. The former is an atomic counter + // meaning that other threads can increment it safely. + atomic_counter_t sent_seqnum; + uint32_t processed_seqnum; + + bool terminating; + + safe_object_t (const safe_object_t&); + void operator = (const safe_object_t&); + }; + +} + +#endif -- cgit v1.2.3