fix8  version 1.4.0
Open Source C++ FIX Framework
ff_wrapper.hpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
20 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
21 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
23 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
24 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
25 
26 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
27 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
28 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
29 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
30 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
31 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
32 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 
34 */
35 //-------------------------------------------------------------------------------------------------
36 #ifndef FIX8_FF_WRAPPER_HPP_
37 #define FIX8_FF_WRAPPER_HPP_
38 
39 //-------------------------------------------------------------------------------------------------
40 namespace FIX8 {
41 
42 //----------------------------------------------------------------------------------------
43 template<typename T>
45 {
46  // we could also use ff::MSqueue
47  ff::uMPMC_Ptr_Queue _queue;
48 
49 public:
50  using value_type = T;
51 
53  using reference = T&;
54 
56  using const_reference = const T&;
57 
58  explicit ff_unbounded_queue() { _queue.init(); }
60 
61  bool try_push(const T& source)
62  { return _queue.push(new (::ff::ff_malloc(sizeof(T))) T(source)); }
63  void push(const T& source) { try_push(source); }
64  bool try_pop(T* &target) { return _queue.pop(reinterpret_cast<void**>(&target)); }
65  bool pop(T* &target)
66  {
67 #if defined FIX8_SLEEP_NO_YIELD
68  const unsigned cnt_rnd(3);
69  unsigned cnt(0);
70 #endif
71  for(;;)
72  {
73  if (try_pop(target))
74  return true;
75 #if defined FIX8_SLEEP_NO_YIELD
76  if ((++cnt %= cnt_rnd) == 0)
77  hypersleep<h_nanoseconds>(FIX8_SLEEP_NO_YIELD);
78  else
79 #endif
80  sched_yield();
81  }
82 
83  return false;
84  }
85  void release(T *source) const { ::ff::ff_free(source); }
86 };
87 
88 //----------------------------------------------------------------------------------------
89 template<typename T> // pointer specialisation - treat the pointer version identically and gobble the indirection
91 {
92  ff::uMPMC_Ptr_Queue _queue;
93 
94 public:
95  using value_type = T;
96 
98  using reference = T&;
99 
101  using const_reference = const T&;
102 
103  explicit ff_unbounded_queue() { _queue.init(); }
105 
106  bool try_push(T *source) { return _queue.push(source); }
107  void push(T *source) { try_push(source); }
108  bool try_pop(T* &target) { return _queue.pop(reinterpret_cast<void**>(&target)); }
109  bool pop(T* &target)
110  {
111 #if defined FIX8_SLEEP_NO_YIELD
112  const unsigned cnt_rnd(3);
113  unsigned cnt(0);
114 #endif
115  for(;;)
116  {
117  if (try_pop(target))
118  return true;
119 #if defined FIX8_SLEEP_NO_YIELD
120  if ((++cnt %= cnt_rnd) == 0)
121  hypersleep<h_nanoseconds>(FIX8_SLEEP_NO_YIELD);
122  else
123 #endif
124  sched_yield();
125  }
126 
127  return false;
128  }
129 };
130 
131 //----------------------------------------------------------------------------------------
132 
133 } // FIX8
134 
135 #endif // FIX8_FF_WRAPPER_HPP_
136 
ff::uMPMC_Ptr_Queue _queue
Definition: ff_wrapper.hpp:47
void push(const T &source)
Definition: ff_wrapper.hpp:63
void release(T *source) const
Definition: ff_wrapper.hpp:85
bool try_push(const T &source)
Definition: ff_wrapper.hpp:61
bool try_pop(T *&target)
Definition: ff_wrapper.hpp:64
ff::uMPMC_Ptr_Queue _queue
Definition: ff_wrapper.hpp:92
const T & const_reference
Const reference type.
Definition: ff_wrapper.hpp:101
T & reference
Reference type.
Definition: ff_wrapper.hpp:98
const T & const_reference
Const reference type.
Definition: ff_wrapper.hpp:56
bool pop(T *&target)
Definition: ff_wrapper.hpp:65
T & reference
Reference type.
Definition: ff_wrapper.hpp:53
int hypersleep< h_nanoseconds >(unsigned amt)
Definition: hypersleep.hpp:149