/home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/fixedSize/mpsc/locking/consumer.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/fixedSize/mpsc/locking/consumer.hpp Source File
HiCR
consumer.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
26#include <hicr/core/definitions.hpp>
29#include <utility>
30
31namespace HiCR::channel::fixedSize::MPSC::locking
32{
33
41{
42 private:
43
48 const std::shared_ptr<HiCR::GlobalMemorySlot> _tokenBuffer;
49
50 /*
51 * Global Memory slot pointing to the consumer's coordination buffer for acquiring a lock and updating
52 */
53 const std::shared_ptr<HiCR::GlobalMemorySlot> _consumerCoordinationBuffer;
54
55 public:
56
71 Consumer(CommunicationManager &communicationManager,
72 std::shared_ptr<GlobalMemorySlot> tokenBuffer,
73 const std::shared_ptr<LocalMemorySlot> &internalCoordinationBuffer,
74 std::shared_ptr<GlobalMemorySlot> consumerCoordinationBuffer,
75 const size_t tokenSize,
76 const size_t capacity)
77 : channel::fixedSize::Base(communicationManager, internalCoordinationBuffer, tokenSize, capacity),
78 _tokenBuffer(std::move(tokenBuffer)),
79 _consumerCoordinationBuffer(std::move(consumerCoordinationBuffer))
80 {}
81 ~Consumer() = default;
82
106 __INLINE__ ssize_t peek(const size_t pos = 0)
107 {
108 // Check if the requested position exceeds the capacity of the channel
109 if (pos >= getCircularBuffer()->getCapacity())
110 HICR_THROW_LOGIC("Attempting to peek for a token with position %lu (token number %lu when starting from zero), which is beyond than the channel capacity (%lu)",
111 pos,
112 pos + 1,
113 getCircularBuffer()->getCapacity());
114
115 // Value to return, initially set as -1 as default (not able to find the requested value)
116 ssize_t bufferPos = -1;
117
118 // Obtaining coordination buffer slot lock
119 if (getCommunicationManager()->acquireGlobalLock(_consumerCoordinationBuffer) == false) return bufferPos;
120
122 // Calculating current channel depth
123 const auto curDepth = getDepth();
124
125 // Calculating buffer position, if there are enough tokens in the buffer to satisfy the request
126 if (pos < curDepth) bufferPos = (ssize_t)((getCircularBuffer()->getTailPosition() + pos) % getCircularBuffer()->getCapacity());
127
128 // Releasing coordination buffer slot lock
129 getCommunicationManager()->releaseGlobalLock(_consumerCoordinationBuffer);
130
131 // Succeeded in pushing the token(s)
132 return bufferPos;
133 }
134
149 __INLINE__ bool pop(const size_t n = 1)
150 {
151 if (n > getCircularBuffer()->getCapacity())
152 HICR_THROW_LOGIC("Attempting to pop %lu tokens, which is larger than the channel capacity (%lu)", n, getCircularBuffer()->getCapacity());
153
154 // Flag to indicate whether the operaton was successful
155 bool successFlag = false;
156
157 // Obtaining coordination buffer slot lock
158 if (getCommunicationManager()->acquireGlobalLock(_consumerCoordinationBuffer) == false) return successFlag;
159
160 // If the exchange buffer does not have n tokens pushed, reject operation, otherwise succeed
161 if (n <= getDepth())
162 {
163 // Advancing tail (removes elements from the circular buffer)
164 getCircularBuffer()->advanceTail(n);
165
166 // Setting success flag
167 successFlag = true;
168 }
169
170 // Releasing coordination buffer slot lock
171 getCommunicationManager()->releaseGlobalLock(_consumerCoordinationBuffer);
172
173 // Operation was successful
174 return successFlag;
175 }
176
183 [[nodiscard]] __INLINE__ std::shared_ptr<GlobalMemorySlot> getTokenBuffer() const { return _tokenBuffer; }
184};
185
186} // namespace HiCR::channel::fixedSize::MPSC::locking
Definition communicationManager.hpp:54
virtual __INLINE__ void flushReceived()
Definition communicationManager.hpp:492
__INLINE__ void releaseGlobalLock(const std::shared_ptr< GlobalMemorySlot > &memorySlot)
Definition communicationManager.hpp:462
__INLINE__ CommunicationManager * getCommunicationManager() const
Definition base.hpp:217
__INLINE__ size_t getDepth() const noexcept
Definition base.hpp:141
__INLINE__ auto getCircularBuffer() const noexcept
Definition base.hpp:167
Definition base.hpp:42
__INLINE__ ssize_t peek(const size_t pos=0)
Definition consumer.hpp:106
Consumer(CommunicationManager &communicationManager, std::shared_ptr< GlobalMemorySlot > tokenBuffer, const std::shared_ptr< LocalMemorySlot > &internalCoordinationBuffer, std::shared_ptr< GlobalMemorySlot > consumerCoordinationBuffer, const size_t tokenSize, const size_t capacity)
Definition consumer.hpp:71
__INLINE__ bool pop(const size_t n=1)
Definition consumer.hpp:149
__INLINE__ std::shared_ptr< GlobalMemorySlot > getTokenBuffer() const
Definition consumer.hpp:183
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67
Provides base functionality for a fixed-size MPSC channel over HiCR.