summaryrefslogtreecommitdiff
path: root/boost/thread/concurrent_queues/sync_queue.hpp
blob: 1dbbef05dd333f1c531162a9ea4dacf638858983 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifndef BOOST_THREAD_CONCURRENT_QUEUES_SYNC_QUEUE_HPP
#define BOOST_THREAD_CONCURRENT_QUEUES_SYNC_QUEUE_HPP

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Vicente J. Botet Escriba 2013-2014. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/thread for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <iostream>

#include <boost/thread/detail/config.hpp>
#include <boost/thread/concurrent_queues/detail/sync_queue_base.hpp>
#include <boost/thread/concurrent_queues/queue_op_status.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/csbl/devector.hpp>
#include <boost/thread/detail/move.hpp>
#include <boost/thread/mutex.hpp>

#include <boost/throw_exception.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>

#include <boost/config/abi_prefix.hpp>

namespace boost
{
namespace concurrent
{
  template <class ValueType, class Container = csbl::devector<ValueType> >
  class sync_queue
    : public detail::sync_queue_base<ValueType, Container >
  {
    typedef detail::sync_queue_base<ValueType, Container >  super;

  public:
    typedef ValueType value_type;
    //typedef typename super::value_type value_type; // fixme
    typedef typename super::underlying_queue_type underlying_queue_type;
    typedef typename super::size_type size_type;
    typedef typename super::op_status op_status;

    // Constructors/Assignment/Destructors
    BOOST_THREAD_NO_COPYABLE(sync_queue)
    inline sync_queue();
    //template <class Range>
    //inline explicit sync_queue(Range range);
    inline ~sync_queue();

    // Modifiers

    inline void push(const value_type& x);
    inline queue_op_status try_push(const value_type& x);
    inline queue_op_status nonblocking_push(const value_type& x);
    inline queue_op_status wait_push(const value_type& x);
    inline void push(BOOST_THREAD_RV_REF(value_type) x);
    inline queue_op_status try_push(BOOST_THREAD_RV_REF(value_type) x);
    inline queue_op_status nonblocking_push(BOOST_THREAD_RV_REF(value_type) x);
    inline queue_op_status wait_push(BOOST_THREAD_RV_REF(value_type) x);

    // Observers/Modifiers
    inline void pull(value_type&);
    // enable_if is_nothrow_copy_movable<value_type>
    inline value_type pull();

    inline queue_op_status try_pull(value_type&);
    inline queue_op_status nonblocking_pull(value_type&);
    inline queue_op_status wait_pull(ValueType& elem);

  private:

    inline queue_op_status try_pull(value_type& x, unique_lock<mutex>& lk);
    inline queue_op_status wait_pull(value_type& x, unique_lock<mutex>& lk);
    inline queue_op_status try_push(const value_type& x, unique_lock<mutex>& lk);
    inline queue_op_status wait_push(const value_type& x, unique_lock<mutex>& lk);
    inline queue_op_status try_push(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);
    inline queue_op_status wait_push(BOOST_THREAD_RV_REF(value_type) x, unique_lock<mutex>& lk);

    inline void pull(value_type& elem, unique_lock<mutex>& )
    {
      elem = boost::move(super::data_.front());
      super::data_.pop_front();
    }
    inline value_type pull(unique_lock<mutex>& )
    {
      value_type e = boost::move(super::data_.front());
      super::data_.pop_front();
      return boost::move(e);
    }

    inline void push(const value_type& elem, unique_lock<mutex>& lk)
    {
      super::data_.push_back(elem);
      super::notify_not_empty_if_needed(lk);
    }

    inline void push(BOOST_THREAD_RV_REF(value_type) elem, unique_lock<mutex>& lk)
    {
      super::data_.push_back(boost::move(elem));
      super::notify_not_empty_if_needed(lk);
    }
  };

  template <class ValueType, class Container>
  sync_queue<ValueType, Container>::sync_queue() :
    super()
  {
  }

//  template <class ValueType, class Container>
//  template <class Range>
//  explicit sync_queue<ValueType, Container>::sync_queue(Range range) :
//    data_(), closed_(false)
//  {
//    try
//    {
//      typedef typename Range::iterator iterator_t;
//      iterator_t first = boost::begin(range);
//      iterator_t end = boost::end(range);
//      for (iterator_t cur = first; cur != end; ++cur)
//      {
//        data_.push(boost::move(*cur));;
//      }
//      notify_not_empty_if_needed(lk);
//    }
//    catch (...)
//    {
//      delete[] data_;
//    }
//  }

  template <class ValueType, class Container>
  sync_queue<ValueType, Container>::~sync_queue()
  {
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::try_pull(ValueType& elem, unique_lock<mutex>& lk)
  {
    if (super::empty(lk))
    {
      if (super::closed(lk)) return queue_op_status::closed;
      return queue_op_status::empty;
    }
    pull(elem, lk);
    return queue_op_status::success;
  }
  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::wait_pull(ValueType& elem, unique_lock<mutex>& lk)
  {
    //std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
    if (super::empty(lk))
    {
      //std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
      if (super::closed(lk)) return queue_op_status::closed;
    }
    //std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
    bool has_been_closed = super::wait_until_not_empty_or_closed(lk);
    //std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
    if (has_been_closed) return queue_op_status::closed;
    //std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
    pull(elem, lk);
    //std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
    return queue_op_status::success;
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::try_pull(ValueType& elem)
  {
    unique_lock<mutex> lk(super::mtx_);
    return try_pull(elem, lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::wait_pull(ValueType& elem)
  {
    unique_lock<mutex> lk(super::mtx_);
    return wait_pull(elem, lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::nonblocking_pull(ValueType& elem)
  {
    unique_lock<mutex> lk(super::mtx_, try_to_lock);
    if (!lk.owns_lock())
    {
      return queue_op_status::busy;
    }
    return try_pull(elem, lk);
  }

  template <class ValueType, class Container>
  void sync_queue<ValueType, Container>::pull(ValueType& elem)
  {
      unique_lock<mutex> lk(super::mtx_);
      super::wait_until_not_empty(lk);
      pull(elem, lk);
  }

  // enable if ValueType is nothrow movable
  template <class ValueType, class Container>
  ValueType sync_queue<ValueType, Container>::pull()
  {
      unique_lock<mutex> lk(super::mtx_);
      super::wait_until_not_empty(lk);
      return pull(lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::try_push(const ValueType& elem, unique_lock<mutex>& lk)
  {
    if (super::closed(lk)) return queue_op_status::closed;
    push(elem, lk);
    return queue_op_status::success;
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::try_push(const ValueType& elem)
  {
    unique_lock<mutex> lk(super::mtx_);
    return try_push(elem, lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::wait_push(const ValueType& elem, unique_lock<mutex>& lk)
  {
    if (super::closed(lk)) return queue_op_status::closed;
    push(elem, lk);
    return queue_op_status::success;
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::wait_push(const ValueType& elem)
  {
    unique_lock<mutex> lk(super::mtx_);
    return wait_push(elem, lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::nonblocking_push(const ValueType& elem)
  {
    unique_lock<mutex> lk(super::mtx_, try_to_lock);
    if (!lk.owns_lock()) return queue_op_status::busy;
    return try_push(elem, lk);
  }

  template <class ValueType, class Container>
  void sync_queue<ValueType, Container>::push(const ValueType& elem)
  {
      unique_lock<mutex> lk(super::mtx_);
      super::throw_if_closed(lk);
      push(elem, lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::try_push(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
  {
    if (super::closed(lk)) return queue_op_status::closed;
    push(boost::move(elem), lk);
    return queue_op_status::success;
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::try_push(BOOST_THREAD_RV_REF(ValueType) elem)
  {
    unique_lock<mutex> lk(super::mtx_);
    return try_push(boost::move(elem), lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::wait_push(BOOST_THREAD_RV_REF(ValueType) elem, unique_lock<mutex>& lk)
  {
    if (super::closed(lk)) return queue_op_status::closed;
    push(boost::move(elem), lk);
    return queue_op_status::success;
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::wait_push(BOOST_THREAD_RV_REF(ValueType) elem)
  {
    unique_lock<mutex> lk(super::mtx_);
    return wait_push(boost::move(elem), lk);
  }

  template <class ValueType, class Container>
  queue_op_status sync_queue<ValueType, Container>::nonblocking_push(BOOST_THREAD_RV_REF(ValueType) elem)
  {
    unique_lock<mutex> lk(super::mtx_, try_to_lock);
    if (!lk.owns_lock())
    {
      return queue_op_status::busy;
    }
    return try_push(boost::move(elem), lk);
  }

  template <class ValueType, class Container>
  void sync_queue<ValueType, Container>::push(BOOST_THREAD_RV_REF(ValueType) elem)
  {
      unique_lock<mutex> lk(super::mtx_);
      super::throw_if_closed(lk);
      push(boost::move(elem), lk);
  }

  template <class ValueType, class Container>
  sync_queue<ValueType, Container>& operator<<(sync_queue<ValueType, Container>& sbq, BOOST_THREAD_RV_REF(ValueType) elem)
  {
    sbq.push(boost::move(elem));
    return sbq;
  }

  template <class ValueType, class Container>
  sync_queue<ValueType, Container>& operator<<(sync_queue<ValueType, Container>& sbq, ValueType const&elem)
  {
    sbq.push(elem);
    return sbq;
  }

  template <class ValueType, class Container>
  sync_queue<ValueType, Container>& operator>>(sync_queue<ValueType, Container>& sbq, ValueType &elem)
  {
    sbq.pull(elem);
    return sbq;
  }

}
using concurrent::sync_queue;

}

#include <boost/config/abi_suffix.hpp>

#endif