1 /*
2  * Copyright (c) 2020 Synopsys, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "pktqueue.h"
8 
9 /* Put a packet header in a queue defined in argument */
phdr_desc_enqueue(struct phdr_desc_queue * queue,struct phdr_desc * desc,struct k_mutex * mutex)10 void phdr_desc_enqueue(struct phdr_desc_queue *queue, struct phdr_desc *desc,
11 			struct k_mutex *mutex)
12 {
13 	/* Locking queue */
14 	k_mutex_lock(mutex, K_FOREVER);
15 
16 	if (queue->count == 0) {
17 		queue->head = queue->tail = desc;
18 	} else {
19 		queue->tail->next = desc;
20 		queue->tail = desc;
21 	}
22 	queue->count++;
23 	desc->next = NULL;
24 
25 	/* Unlocking queue */
26 	k_mutex_unlock(mutex);
27 }
28 
29 /* Take a packet header from queue defined in argument */
phdr_desc_dequeue(struct phdr_desc_queue * queue,struct k_mutex * mutex)30 struct phdr_desc *phdr_desc_dequeue(struct phdr_desc_queue *queue,
31 					struct k_mutex *mutex)
32 {
33 	struct phdr_desc *return_ptr = NULL;
34 	/* Locking queue */
35 	k_mutex_lock(mutex, K_FOREVER);
36 	if (queue->count != 0) {
37 		queue->count--;
38 		return_ptr = queue->head;
39 		queue->head = queue->head->next;
40 	}
41 
42 	/* Unlocking queue */
43 	k_mutex_unlock(mutex);
44 
45 	return return_ptr;
46 }
47