summaryrefslogtreecommitdiff
path: root/src/safe_object.hpp
blob: efa0d2fd47d6ca1f6c7f1a905d12b6aeb0558c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 <http://www.gnu.org/licenses/>.
*/

#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