ypipe.hpp 7.63 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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 GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

Martin Sustrik's avatar
Martin Sustrik committed
30 31
#ifndef __ZMQ_YPIPE_HPP_INCLUDED__
#define __ZMQ_YPIPE_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
32 33 34

#include "atomic_ptr.hpp"
#include "yqueue.hpp"
35
#include "ypipe_base.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
36

Martin Sustrik's avatar
Martin Sustrik committed
37
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
38 39 40 41 42 43
{

    //  Lock-free queue implementation.
    //  Only a single thread can read from the pipe at any specific moment.
    //  Only a single thread can write to the pipe at any specific moment.
    //  T is the type of the object in the queue.
44
    //  N is granularity of the pipe, i.e. how many items are needed to
45 46
    //  perform next memory allocation.

47
    template <typename T, int N> class ypipe_t : public ypipe_base_t <T>
Martin Sustrik's avatar
Martin Sustrik committed
48 49 50
    {
    public:

51 52
        //  Initialises the pipe.
        inline ypipe_t ()
Martin Sustrik's avatar
Martin Sustrik committed
53 54 55 56
        {
            //  Insert terminator element into the queue.
            queue.push ();

57
            //  Let all the pointers to point to the terminator.
Martin Sustrik's avatar
Martin Sustrik committed
58
            //  (unless pipe is dead, in which case c is set to NULL).
59
            r = w = f = &queue.back ();
60
            c.set (&queue.back ());
Martin Sustrik's avatar
Martin Sustrik committed
61 62
        }

hitstergtd's avatar
hitstergtd committed
63
        //  The destructor doesn't have to be virtual. It is made virtual
64 65 66 67 68
        //  just to keep ICC and code checking tools from complaining.
        inline virtual ~ypipe_t ()
        {
        }

Martin Sustrik's avatar
Martin Sustrik committed
69
        //  Following function (write) deliberately copies uninitialised data
Martin Sustrik's avatar
Martin Sustrik committed
70
        //  when used with zmq_msg. Initialising the VSM body for
Martin Sustrik's avatar
Martin Sustrik committed
71 72 73 74 75 76 77
        //  non-VSM messages won't be good for performance.

#ifdef ZMQ_HAVE_OPENVMS
#pragma message save
#pragma message disable(UNINIT)
#endif

78 79 80 81 82
        //  Write an item to the pipe.  Don't flush it yet. If incomplete is
        //  set to true the item is assumed to be continued by items
        //  subsequently written to the pipe. Incomplete items are never
        //  flushed down the stream.
        inline void write (const T &value_, bool incomplete_)
Martin Sustrik's avatar
Martin Sustrik committed
83 84 85 86
        {
            //  Place the value to the queue, add new terminator element.
            queue.back () = value_;
            queue.push ();
87 88 89 90

            //  Move the "flush up to here" poiter.
            if (!incomplete_)
                f = &queue.back ();
Martin Sustrik's avatar
Martin Sustrik committed
91 92 93 94 95 96
        }

#ifdef ZMQ_HAVE_OPENVMS
#pragma message restore
#endif

Zhilong.JI's avatar
Zhilong.JI committed
97
        //  Pop an incomplete item from the pipe. Returns true if such
98
        //  item exists, false otherwise.
99 100
        inline bool unwrite (T *value_)
        {
101
            if (f == &queue.back ())
102 103
                return false;
            queue.unpush ();
104
            *value_ = queue.back ();
105 106 107
            return true;
        }

108 109 110
        //  Flush all the completed items into the pipe. Returns false if
        //  the reader thread is sleeping. In that case, caller is obliged to
        //  wake the reader up before using the pipe again.
Martin Sustrik's avatar
Martin Sustrik committed
111 112 113
        inline bool flush ()
        {
            //  If there are no un-flushed items, do nothing.
114
            if (w == f)
Martin Sustrik's avatar
Martin Sustrik committed
115 116
                return true;

117 118
            //  Try to set 'c' to 'f'.
            if (c.cas (w, f) != w) {
Martin Sustrik's avatar
Martin Sustrik committed
119 120 121 122 123 124

                //  Compare-and-swap was unseccessful because 'c' is NULL.
                //  This means that the reader is asleep. Therefore we don't
                //  care about thread-safeness and update c in non-atomic
                //  manner. We'll return false to let the caller know
                //  that reader is sleeping.
125 126
                c.set (f);
                w = f;
Martin Sustrik's avatar
Martin Sustrik committed
127 128 129 130
                return false;
            }

            //  Reader is alive. Nothing special to do now. Just move
131 132
            //  the 'first un-flushed item' pointer to 'f'.
            w = f;
Martin Sustrik's avatar
Martin Sustrik committed
133 134 135
            return true;
        }

136 137
        //  Check whether item is available for reading.
        inline bool check_read ()
Martin Sustrik's avatar
Martin Sustrik committed
138
        {
139
            //  Was the value prefetched already? If so, return.
140
            if (&queue.front () != r && r)
Martin Sustrik's avatar
Martin Sustrik committed
141 142 143
                 return true;

            //  There's no prefetched value, so let us prefetch more values.
144 145 146 147 148 149 150
            //  Prefetching is to simply retrieve the
            //  pointer from c in atomic fashion. If there are no
            //  items to prefetch, set c to NULL (using compare-and-swap).
            r = c.cas (&queue.front (), NULL);

            //  If there are no elements prefetched, exit.
            //  During pipe's lifetime r should never be NULL, however,
151
            //  it can happen during pipe shutdown when items
152 153 154
            //  are being deallocated.
            if (&queue.front () == r || !r)
                return false;
Martin Sustrik's avatar
Martin Sustrik committed
155

156 157 158 159 160 161 162 163 164 165 166 167
            //  There was at least one value prefetched.
            return true;
        }

        //  Reads an item from the pipe. Returns false if there is no value.
        //  available.
        inline bool read (T *value_)
        {
            //  Try to prefetch a value.
            if (!check_read ())
                return false;

Martin Sustrik's avatar
Martin Sustrik committed
168 169
            //  There was at least one value prefetched.
            //  Return it to the caller.
Martin Sustrik's avatar
Martin Sustrik committed
170 171 172 173 174
            *value_ = queue.front ();
            queue.pop ();
            return true;
        }

175 176 177
        //  Applies the function fn to the first elemenent in the pipe
        //  and returns the value returned by the fn.
        //  The pipe mustn't be empty or the function crashes.
178
        inline bool probe (bool (*fn)(const T &))
179
        {
Pieter Hintjens's avatar
Pieter Hintjens committed
180 181
            bool rc = check_read ();
            zmq_assert (rc);
182

Pieter Hintjens's avatar
Pieter Hintjens committed
183
            return (*fn) (queue.front ());
184 185
        }

Martin Sustrik's avatar
Martin Sustrik committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    protected:

        //  Allocation-efficient queue to store pipe items.
        //  Front of the queue points to the first prefetched item, back of
        //  the pipe points to last un-flushed item. Front is used only by
        //  reader thread, while back is used only by writer thread.
        yqueue_t <T, N> queue;

        //  Points to the first un-flushed item. This variable is used
        //  exclusively by writer thread.
        T *w;

        //  Points to the first un-prefetched item. This variable is used
        //  exclusively by reader thread.
        T *r;

202 203 204
        //  Points to the first item to be flushed in the future.
        T *f;

Martin Sustrik's avatar
Martin Sustrik committed
205 206
        //  The single point of contention between writer and reader thread.
        //  Points past the last flushed item. If it is NULL,
Martin Sustrik's avatar
Martin Sustrik committed
207 208 209 210 211 212
        //  reader is asleep. This pointer should be always accessed using
        //  atomic operations.
        atomic_ptr_t <T> c;

        //  Disable copying of ypipe object.
        ypipe_t (const ypipe_t&);
213
        const ypipe_t &operator = (const ypipe_t&);
Martin Sustrik's avatar
Martin Sustrik committed
214 215 216 217 218
    };

}

#endif