Motr  M0
bev_cqueue.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 
609 static bool bev_cqueue_invariant(const struct nlx_core_bev_cqueue *q)
610 {
611  return q != NULL && q->cbcq_consumer != 0 &&
612  q->cbcq_nr >= M0_NET_LNET_BEVQ_MIN_SIZE &&
613  m0_atomic64_get(&q->cbcq_count) < q->cbcq_nr &&
614  !nlx_core_kmem_loc_is_empty(&q->cbcq_producer_loc);
615 }
616 
629  struct nlx_core_bev_link *ql)
630 {
631  struct nlx_core_bev_link *consumer =
632  (struct nlx_core_bev_link *) (q->cbcq_consumer);
633  M0_PRE(q->cbcq_nr > 0 && consumer != NULL);
636 
637  ql->cbl_c_next = consumer->cbl_c_next;
638  ql->cbl_p_next_loc = consumer->cbl_p_next_loc;
639  consumer->cbl_c_next = (nlx_core_opaque_ptr_t) ql;
640  consumer->cbl_p_next_loc = ql->cbl_p_self_loc;
641  q->cbcq_consumer = (nlx_core_opaque_ptr_t) ql;
642  q->cbcq_nr++;
643 
645 }
646 
659  struct nlx_core_bev_link *ql1,
660  struct nlx_core_bev_link *ql2)
661 {
662  M0_PRE(q != NULL && q->cbcq_nr == 0 && ql1 != NULL && ql2 != NULL);
664  /* special case: add first element to the circular queue */
665  ql1->cbl_c_self = (nlx_core_opaque_ptr_t) ql1;
666  ql1->cbl_c_next = (nlx_core_opaque_ptr_t) ql1;
667  ql1->cbl_p_next_loc = ql1->cbl_p_self_loc;
668  q->cbcq_consumer = (nlx_core_opaque_ptr_t) ql1;
669  q->cbcq_producer_loc = ql1->cbl_p_self_loc;
670  q->cbcq_nr++;
671 
672  bev_cqueue_add(q, ql2);
673  m0_atomic64_set(&q->cbcq_count, 0);
675 }
676 
683  void (*free_cb)(struct nlx_core_bev_link *))
684 {
685  struct nlx_core_bev_link *ql;
686  struct nlx_core_bev_link *nql = NULL;
687 
689  M0_PRE(free_cb != NULL);
690  for (ql = (struct nlx_core_bev_link *) q->cbcq_consumer;
691  q->cbcq_nr > 0; ql = nql, --q->cbcq_nr) {
692  nql = (struct nlx_core_bev_link *) ql->cbl_c_next;
693  free_cb(ql);
694  }
695 
696  q->cbcq_consumer = 0;
697 }
698 
702 static bool bev_cqueue_is_empty(const struct nlx_core_bev_cqueue *q)
703 {
705  return m0_atomic64_get(&q->cbcq_count) == 0;
706 }
707 
711 static size_t bev_cqueue_size(const struct nlx_core_bev_cqueue *q)
712 {
713  return q->cbcq_nr;
714 }
715 
723 {
724  struct nlx_core_bev_link *link;
725 
726  if (bev_cqueue_is_empty(q)) /* also checks invariant */
727  return NULL;
728  link = (struct nlx_core_bev_link *) q->cbcq_consumer;
729  M0_ASSERT(link->cbl_c_next != 0);
730  q->cbcq_consumer = (nlx_core_opaque_ptr_t) link->cbl_c_next;
731  m0_atomic64_dec(&q->cbcq_count);
732  return (struct nlx_core_bev_link *) (q->cbcq_consumer);
733 }
734 
739 /*
740  * Local variables:
741  * c-indentation-style: "K&R"
742  * c-basic-offset: 8
743  * tab-width: 8
744  * fill-column: 80
745  * scroll-step: 1
746  * End:
747  */
static void bev_cqueue_add(struct nlx_core_bev_cqueue *q, struct nlx_core_bev_link *ql)
Definition: bev_cqueue.c:628
static struct nlx_core_bev_link * bev_cqueue_get(struct nlx_core_bev_cqueue *q)
Definition: bev_cqueue.c:722
#define M0_PRE(cond)
static struct m0_semaphore q
Definition: rwlock.c:55
#define NULL
Definition: misc.h:38
static size_t bev_cqueue_size(const struct nlx_core_bev_cqueue *q)
Definition: bev_cqueue.c:711
static void bev_cqueue_init(struct nlx_core_bev_cqueue *q, struct nlx_core_bev_link *ql1, struct nlx_core_bev_link *ql2)
Definition: bev_cqueue.c:658
static bool bev_cqueue_invariant(const struct nlx_core_bev_cqueue *q)
Definition: bev_cqueue.c:609
uint64_t nlx_core_opaque_ptr_t
#define M0_ASSERT(cond)
static void m0_atomic64_dec(struct m0_atomic64 *a)
#define M0_POST(cond)
static bool bev_cqueue_is_empty(const struct nlx_core_bev_cqueue *q)
Definition: bev_cqueue.c:702
static int64_t m0_atomic64_get(const struct m0_atomic64 *a)
static void bev_cqueue_fini(struct nlx_core_bev_cqueue *q, void(*free_cb)(struct nlx_core_bev_link *))
Definition: bev_cqueue.c:682
static bool nlx_core_kmem_loc_invariant(const struct nlx_core_kmem_loc *loc)
Definition: lnet_pvt.h:81
static bool nlx_core_kmem_loc_is_empty(const struct nlx_core_kmem_loc *loc)
Definition: lnet_pvt.h:91
static void m0_atomic64_set(struct m0_atomic64 *a, int64_t num)