rocPRIM
double_buffer.hpp
1 // Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef ROCPRIM_TYPES_DOUBLE_BUFFER_HPP_
22 #define ROCPRIM_TYPES_DOUBLE_BUFFER_HPP_
23 
24 #include "../config.hpp"
25 
28 
29 BEGIN_ROCPRIM_NAMESPACE
30 
36 template<class T>
38 {
39  T * buffers[2];
40 
41  unsigned int selector;
42 
43 public:
44 
47  ROCPRIM_HOST_DEVICE inline
49  {
50  selector = 0;
51  buffers[0] = nullptr;
52  buffers[1] = nullptr;
53  }
54 
59  ROCPRIM_HOST_DEVICE inline
61  {
62  selector = 0;
63  buffers[0] = current;
64  buffers[1] = alternate;
65  }
66 
68  ROCPRIM_HOST_DEVICE inline
69  T * current() const
70  {
71  return buffers[selector];
72  }
73 
75  ROCPRIM_HOST_DEVICE inline
76  T * alternate() const
77  {
78  return buffers[selector ^ 1];
79  }
80 
82  ROCPRIM_HOST_DEVICE inline
83  void swap()
84  {
85  selector ^= 1;
86  }
87 };
88 
89 END_ROCPRIM_NAMESPACE
90 
92 // end of group utilsmodule
93 
94 #endif // ROCPRIM_TYPES_DOUBLE_BUFFER_HPP_
This class provides an convenient way to do double buffering.
Definition: double_buffer.hpp:37
ROCPRIM_HOST_DEVICE double_buffer(T *current, T *alternate)
Contructs a double buffer object using the supplied buffer pointers.
Definition: double_buffer.hpp:60
ROCPRIM_HOST_DEVICE double_buffer()
Constructs an empty double buffer object, initializing the buffer pointers to nullptr.
Definition: double_buffer.hpp:48
ROCPRIM_HOST_DEVICE T * current() const
Returns a pointer to the current buffer.
Definition: double_buffer.hpp:69
ROCPRIM_HOST_DEVICE T * alternate() const
Returns a pointer to the alternate buffer.
Definition: double_buffer.hpp:76
ROCPRIM_HOST_DEVICE void swap()
Swaps the current and alternate buffers.
Definition: double_buffer.hpp:83