Motr  M0
queue.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #pragma once
24 
25 #ifndef __MOTR_BE_QUEUE_H__
26 #define __MOTR_BE_QUEUE_H__
27 
113 #include "lib/types.h" /* m0_bcount_t */
114 #include "lib/tlist.h" /* m0_tl */
115 #include "lib/mutex.h" /* m0_mutex */
116 
117 #include "be/tx_credit.h" /* m0_be_tx_credit */
118 
119 
120 struct m0_be_op;
121 struct be_queue_wait_op;
122 struct m0_buf;
123 
125  uint64_t bqc_q_size_max;
129 };
130 
131 struct m0_be_queue {
134 
136  struct m0_tl bq_q;
138 
149  char *bq_qitems;
150 
152  struct m0_tl bq_op_put;
154  struct m0_tl bq_op_get;
156 
161 
163 
164  uint64_t bq_enqueued;
165  uint64_t bq_dequeued;
166 };
167 
168 #define BEQ_F "(queue=%p bq_enqueued=%" PRIu64 " bq_dequeued=%" PRIu64 " " \
169  "bq_the_end=%d)"
170 #define BEQ_P(bq) (bq), (bq)->bq_enqueued, (bq)->bq_dequeued, \
171  !!((bq)->bq_the_end)
172 
173 M0_INTERNAL int m0_be_queue_init(struct m0_be_queue *bq,
174  struct m0_be_queue_cfg *cfg);
175 M0_INTERNAL void m0_be_queue_fini(struct m0_be_queue *bq);
176 
177 M0_INTERNAL void m0_be_queue_lock(struct m0_be_queue *bq);
178 M0_INTERNAL void m0_be_queue_unlock(struct m0_be_queue *bq);
179 
180 /*
181  * Put a buffer to the queue.
182  *
183  * @param data the data from this buffer is copied to the queue internal data
184  * structures
185  *
186  * @pre data->b_nob == bq->bq_cfg.bqc_item_length
187  *
188  * - if the queue is full, then the operation would block until a space for at
189  * least one item becomes available in the queue;
190  * - different producers waiting on m0_be_queue_put() would be awaken in the
191  * same order they were put to sleep.
192  */
193 M0_INTERNAL void m0_be_queue_put(struct m0_be_queue *bq,
194  struct m0_be_op *op,
195  const struct m0_buf *data);
196 
197 /*
198  * Nothing is going to be put to the queue after this call.
199  * This function could be called only once.
200  */
201 M0_INTERNAL void m0_be_queue_end(struct m0_be_queue *bq);
202 
203 /*
204  * Get a buffer from the queue.
205  *
206  * @param data The data is copied from queue internal data structures
207  * to the buffer this parameter points to.
208  * @param successful true if the data is returned, false if the queue is
209  * empty and m0_be_queue_end() has been called before this
210  * call.
211  *
212  * @pre data->b_nob == bq->bq_cfg.bqc_item_length
213  *
214  * - if the queue is empty, then the operation would block until at least one
215  * item becomes available in the queue;
216  * - different consumers waiting on m0_be_queue_get() would be awaken in the
217  * same order they were put to sleep.
218  */
219 M0_INTERNAL void m0_be_queue_get(struct m0_be_queue *bq,
220  struct m0_be_op *op,
221  struct m0_buf *data,
222  bool *successful);
223 
224 /*
225  * Peek at the queue.
226  *
227  * @param data the buffer that is in the queue internal data structures is
228  * copied to the memory data parameter points to.
229  * @return true if the data was copied, false if the queue is empty.
230  *
231  * @pre data->b_nob == bq->bq_cfg.bqc_item_length
232  */
233 M0_INTERNAL bool m0_be_queue_peek(struct m0_be_queue *bq,
234  struct m0_buf *data);
235 
236 #define M0_BE_QUEUE_PUT(bq, op, ptr) \
237  m0_be_queue_put(bq, op, &M0_BUF_INIT_PTR(ptr))
238 #define M0_BE_QUEUE_GET(bq, op, ptr, successful) \
239  m0_be_queue_get(bq, op, &M0_BUF_INIT_PTR(ptr), successful)
240 #define M0_BE_QUEUE_PEEK(bq, ptr) \
241  m0_be_queue_peek(bq, &M0_BUF_INIT_PTR(ptr))
242 
244 #endif /* __MOTR_BE_QUEUE_H__ */
245 
246 /*
247  * Local variables:
248  * c-indentation-style: "K&R"
249  * c-basic-offset: 8
250  * tab-width: 8
251  * fill-column: 80
252  * scroll-step: 1
253  * End:
254  */
255 /*
256  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
257  */
uint64_t bqc_consumers_nr_max
Definition: queue.h:127
struct m0_tl bq_op_get
Definition: queue.h:154
uint64_t bq_dequeued
Definition: queue.h:165
struct m0_bufvec data
Definition: di.c:40
uint64_t m0_bcount_t
Definition: types.h:77
struct m0_tl bq_q
Definition: queue.h:136
bool bq_the_end
Definition: queue.h:162
op
Definition: libdemo.c:64
struct m0_mutex bq_lock
Definition: queue.h:133
Definition: buf.h:37
M0_INTERNAL int m0_be_queue_init(struct m0_be_queue *bq, struct m0_be_queue_cfg *cfg)
Definition: queue.c:94
M0_INTERNAL void m0_be_queue_unlock(struct m0_be_queue *bq)
Definition: stubs.c:325
uint64_t bqc_q_size_max
Definition: queue.h:125
M0_INTERNAL void m0_be_queue_fini(struct m0_be_queue *bq)
Definition: queue.c:148
M0_INTERNAL void m0_be_queue_get(struct m0_be_queue *bq, struct m0_be_op *op, struct m0_buf *data, bool *successful)
Definition: queue.c:392
M0_INTERNAL bool m0_be_queue_peek(struct m0_be_queue *bq, struct m0_buf *data)
Definition: queue.c:422
struct be_queue_wait_op * bq_ops_put
Definition: queue.h:160
Definition: tlist.h:251
M0_INTERNAL void m0_be_queue_put(struct m0_be_queue *bq, struct m0_be_op *op, const struct m0_buf *data)
Definition: stubs.c:315
struct m0_tl bq_op_put_unused
Definition: queue.h:153
struct m0_be_queue_cfg bq_cfg
Definition: queue.h:132
struct m0_tl bq_q_unused
Definition: queue.h:137
struct be_queue_wait_op * bq_ops_get
Definition: queue.h:158
char * bq_qitems
Definition: queue.h:149
m0_bcount_t bqc_item_length
Definition: queue.h:128
M0_INTERNAL void m0_be_queue_lock(struct m0_be_queue *bq)
Definition: stubs.c:321
Definition: op.h:74
uint64_t bqc_producers_nr_max
Definition: queue.h:126
M0_INTERNAL void m0_be_queue_end(struct m0_be_queue *bq)
Definition: queue.c:377
struct m0_tl bq_op_get_unused
Definition: queue.h:155
Definition: mutex.h:47
struct m0_tl bq_op_put
Definition: queue.h:152
uint64_t bq_enqueued
Definition: queue.h:164