2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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
|
2009-07-29 12:07:54 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#ifndef __ZMQ_YPIPE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_YPIPE_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#include "atomic_ptr.hpp"
|
|
|
|
#include "yqueue.hpp"
|
2013-08-17 23:08:07 +03:00
|
|
|
#include "ypipe_base.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
// 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.
|
2010-05-19 06:31:57 +02:00
|
|
|
// N is granularity of the pipe, i.e. how many items are needed to
|
2010-05-04 10:22:16 +02:00
|
|
|
// perform next memory allocation.
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2019-12-24 10:39:26 +01:00
|
|
|
template <typename T, int N> class ypipe_t ZMQ_FINAL : public ypipe_base_t<T>
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
|
|
|
public:
|
2010-05-04 10:22:16 +02:00
|
|
|
// Initialises the pipe.
|
2020-02-04 11:57:58 +01:00
|
|
|
ypipe_t ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
// Insert terminator element into the queue.
|
2018-05-27 11:10:39 +02:00
|
|
|
_queue.push ();
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-05-04 10:22:16 +02:00
|
|
|
// Let all the pointers to point to the terminator.
|
2009-07-29 12:07:54 +02:00
|
|
|
// (unless pipe is dead, in which case c is set to NULL).
|
2018-05-27 11:10:39 +02:00
|
|
|
_r = _w = _f = &_queue.back ();
|
|
|
|
_c.set (&_queue.back ());
|
2018-02-01 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
2019-05-18 17:02:06 +01:00
|
|
|
// Following function (write) deliberately copies uninitialised data
|
|
|
|
// when used with zmq_msg. Initialising the VSM body for
|
|
|
|
// non-VSM messages won't be good for performance.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#ifdef ZMQ_HAVE_OPENVMS
|
|
|
|
#pragma message save
|
|
|
|
#pragma message disable(UNINIT)
|
|
|
|
#endif
|
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
// 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.
|
2020-02-04 11:57:58 +01:00
|
|
|
void write (const T &value_, bool incomplete_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
// Place the value to the queue, add new terminator element.
|
2018-05-27 11:10:39 +02:00
|
|
|
_queue.back () = value_;
|
|
|
|
_queue.push ();
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
// Move the "flush up to here" poiter.
|
|
|
|
if (!incomplete_)
|
2018-05-27 11:10:39 +02:00
|
|
|
_f = &_queue.back ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ZMQ_HAVE_OPENVMS
|
|
|
|
#pragma message restore
|
|
|
|
#endif
|
|
|
|
|
2015-10-09 10:52:36 +08:00
|
|
|
// Pop an incomplete item from the pipe. Returns true if such
|
2010-05-19 06:31:57 +02:00
|
|
|
// item exists, false otherwise.
|
2020-02-04 11:57:58 +01:00
|
|
|
bool unwrite (T *value_)
|
2010-03-09 08:43:20 +01:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_f == &_queue.back ())
|
2010-03-09 08:43:20 +01:00
|
|
|
return false;
|
2018-05-27 11:10:39 +02:00
|
|
|
_queue.unpush ();
|
|
|
|
*value_ = _queue.back ();
|
2010-03-09 08:43:20 +01:00
|
|
|
return true;
|
2018-02-01 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
// 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.
|
2020-02-04 11:57:58 +01:00
|
|
|
bool flush ()
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2009-07-29 12:07:54 +02:00
|
|
|
// If there are no un-flushed items, do nothing.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_w == _f)
|
2010-03-09 08:43:20 +01:00
|
|
|
return true;
|
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
// Try to set 'c' to 'f'.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_c.cas (_w, _f) != _w) {
|
2009-07-29 12:07:54 +02:00
|
|
|
// 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.
|
2018-05-27 11:10:39 +02:00
|
|
|
_c.set (_f);
|
|
|
|
_w = _f;
|
2009-07-29 12:07:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader is alive. Nothing special to do now. Just move
|
2010-05-04 10:22:16 +02:00
|
|
|
// the 'first un-flushed item' pointer to 'f'.
|
2018-05-27 11:10:39 +02:00
|
|
|
_w = _f;
|
2009-09-30 10:08:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether item is available for reading.
|
2020-02-04 11:57:58 +01:00
|
|
|
bool check_read ()
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2009-09-30 10:08:35 +02:00
|
|
|
// Was the value prefetched already? If so, return.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (&_queue.front () != _r && _r)
|
2009-07-29 12:07:54 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// There's no prefetched value, so let us prefetch more values.
|
2010-05-04 10:22:16 +02:00
|
|
|
// 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).
|
2018-05-27 11:10:39 +02:00
|
|
|
_r = _c.cas (&_queue.front (), NULL);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2010-05-04 10:22:16 +02:00
|
|
|
// If there are no elements prefetched, exit.
|
|
|
|
// During pipe's lifetime r should never be NULL, however,
|
2010-05-19 06:31:57 +02:00
|
|
|
// it can happen during pipe shutdown when items
|
2010-05-04 10:22:16 +02:00
|
|
|
// are being deallocated.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (&_queue.front () == _r || !_r)
|
2010-05-04 10:22:16 +02:00
|
|
|
return false;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2009-09-30 10:08:35 +02:00
|
|
|
// There was at least one value prefetched.
|
2009-07-29 12:07:54 +02:00
|
|
|
return true;
|
2018-02-01 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
// Reads an item from the pipe. Returns false if there is no value.
|
2009-09-30 10:08:35 +02:00
|
|
|
// available.
|
2020-02-04 11:57:58 +01:00
|
|
|
bool read (T *value_)
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2009-09-30 10:08:35 +02:00
|
|
|
// Try to prefetch a value.
|
|
|
|
if (!check_read ())
|
|
|
|
return false;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// There was at least one value prefetched.
|
|
|
|
// Return it to the caller.
|
2018-05-27 11:10:39 +02:00
|
|
|
*value_ = _queue.front ();
|
|
|
|
_queue.pop ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return true;
|
2018-02-01 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
2010-07-14 18:31:17 +02:00
|
|
|
// 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.
|
2020-02-04 11:57:58 +01:00
|
|
|
bool probe (bool (*fn_) (const T &))
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2019-12-25 13:51:21 +01:00
|
|
|
const bool rc = check_read ();
|
2014-06-25 12:47:39 +02:00
|
|
|
zmq_assert (rc);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
return (*fn_) (_queue.front ());
|
2018-02-01 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
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.
|
2018-05-27 11:10:39 +02:00
|
|
|
yqueue_t<T, N> _queue;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
// Points to the first un-flushed item. This variable is used
|
|
|
|
// exclusively by writer thread.
|
2018-05-27 11:10:39 +02:00
|
|
|
T *_w;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
// Points to the first un-prefetched item. This variable is used
|
|
|
|
// exclusively by reader thread.
|
2018-05-27 11:10:39 +02:00
|
|
|
T *_r;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2010-05-19 06:31:57 +02:00
|
|
|
// Points to the first item to be flushed in the future.
|
2018-05-27 11:10:39 +02:00
|
|
|
T *_f;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// The single point of contention between writer and reader thread.
|
|
|
|
// Points past the last flushed item. If it is NULL,
|
2009-07-29 12:07:54 +02:00
|
|
|
// reader is asleep. This pointer should be always accessed using
|
|
|
|
// atomic operations.
|
2018-05-27 11:10:39 +02:00
|
|
|
atomic_ptr_t<T> _c;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2019-12-08 19:22:04 +01:00
|
|
|
ZMQ_NON_COPYABLE_NOR_MOVABLE (ypipe_t)
|
2018-02-01 11:46:09 +01:00
|
|
|
};
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|