blob: ffc9a15e807ccef0c1a7d69fdceabee47b47784a [file] [log] [blame]
VoLTHA753536e2017-11-02 20:15:09 +05301/*
2** Copyright 2017-present Open Networking Foundation
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
17#include "bal_indications_queue.h"
18
19/*
20 * Implements a circular queue whose size can grow upto MAX_QUEUE_SIZE defined.
21 * Once MAX_QUEUE_SIZE is reached, newest nodes start replacing oldest nodes
22 * in the circular queue.
23 * We define two APIs below, one to get the node from the queue, another to
24 * add node to the queue.
25 */
26
27list_node* get_bal_indication_node(void) {
28 /*'bal_ind_queue_head' should be thread safe there could be simultaneous
29 write from BAL and adapter trying to read from the 'bal_ind_queue_head'.
30 So, protect it under a mutex(bal_ind_queue_lock), before using this MACRO*/
31 list_node *node = NULL;
32 if (num_of_nodes == 1) {
33 node = bal_ind_queue_head;
34 bal_ind_queue_head = NULL;
35 bal_ind_queue_tail = NULL;
36 num_of_nodes = 0;
37 }
38 else if(num_of_nodes > 1) {
39 /*Process if there is at least one node in the queue*/
40 node = bal_ind_queue_head;
41 bal_ind_queue_head = bal_ind_queue_head->next;
42 bal_ind_queue_tail->next = bal_ind_queue_head;
43 --num_of_nodes;
44 }
45
46 return node;
47}
48
49void add_bal_indication_node(list_node *node) {
50 /*'bal_ind_queue_head' should be thread safe there could be simultaneous
51 write from BAL and adapter trying to read from the 'bal_ind_queue_head'.
52 So, protect it under a mutex(bal_ind_queue_lock), before using this MACRO*/
53 if (num_of_nodes == 0 &&
54 bal_ind_queue_head == NULL &&
55 bal_ind_queue_tail == NULL ) {
56 /*Case where the queue is empty*/
57 bal_ind_queue_head = node;
58 bal_ind_queue_tail = node;
59 bal_ind_queue_head->next = node;
60 bal_ind_queue_tail->next = node;
61 ++num_of_nodes;
62 }
63 else if (num_of_nodes == 1 &&
64 bal_ind_queue_head != NULL &&
65 bal_ind_queue_tail != NULL ) {
66 bal_ind_queue_head->next = node;
67 bal_ind_queue_tail = node;
68 bal_ind_queue_tail->next = bal_ind_queue_head;
69 ++num_of_nodes;
70 }
71 else if (num_of_nodes > 1 &&
72 num_of_nodes < MAX_QUEUE_SIZE &&
73 bal_ind_queue_head != NULL &&
74 bal_ind_queue_tail != NULL ) {
75 bal_ind_queue_tail->next = node;
76 bal_ind_queue_tail = node;
77 bal_ind_queue_tail->next = bal_ind_queue_head;
78 ++num_of_nodes;
79 }
80 else if (num_of_nodes == MAX_QUEUE_SIZE &&
81 bal_ind_queue_head != NULL &&
82 bal_ind_queue_tail != NULL ) {
83 list_node *head_ref = bal_ind_queue_head;
84 bal_ind_queue_tail->next = node;
85 bal_ind_queue_tail = node;
86 bal_ind_queue_head = bal_ind_queue_head->next;
87 bal_ind_queue_tail->next = bal_ind_queue_head;
88 bal_get_ind__free_mem(head_ref->bal_indication);
89 free(head_ref);
90 }
91}