sprt_queue.c 2.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2018, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>

#include "sprt_queue.h"

void sprt_queue_init(void *queue_base, uint32_t entry_num, uint32_t entry_size)
{
	assert(queue_base != NULL);
	assert(entry_size > 0U);
	assert(entry_num > 0U);

	struct sprt_queue *queue = (struct sprt_queue *)queue_base;

	queue->entry_num = entry_num;
	queue->entry_size = entry_size;
	queue->idx_write = 0U;
	queue->idx_read = 0U;

	memset(queue->data, 0, entry_num * entry_size);
}

int sprt_queue_is_empty(void *queue_base)
{
	assert(queue_base != NULL);

	struct sprt_queue *queue = (struct sprt_queue *)queue_base;

	return (queue->idx_write == queue->idx_read);
}

int sprt_queue_is_full(void *queue_base)
{
	assert(queue_base != NULL);

	struct sprt_queue *queue = (struct sprt_queue *)queue_base;

	uint32_t idx_next_write = (queue->idx_write + 1) % queue->entry_num;

	return (idx_next_write == queue->idx_read);
}

int sprt_queue_push(void *queue_base, const void *entry)
{
	assert(entry != NULL);
	assert(queue_base != NULL);

	if (sprt_queue_is_full(queue_base) != 0) {
		return -ENOMEM;
	}

	struct sprt_queue *queue = (struct sprt_queue *)queue_base;

	uint8_t *dst_entry = &queue->data[queue->entry_size * queue->idx_write];

	memcpy(dst_entry, entry, queue->entry_size);

	/*
	 * Make sure that the message data is visible before increasing the
	 * counter of available messages.
	 */
	__asm__ volatile("dmb st" ::: "memory");

	queue->idx_write = (queue->idx_write + 1) % queue->entry_num;

	__asm__ volatile("dmb st" ::: "memory");

	return 0;
}

int sprt_queue_pop(void *queue_base, void *entry)
{
	assert(entry != NULL);
	assert(queue_base != NULL);

	if (sprt_queue_is_empty(queue_base) != 0) {
		return -ENOENT;
	}

	struct sprt_queue *queue = (struct sprt_queue *)queue_base;

	uint8_t *src_entry = &queue->data[queue->entry_size * queue->idx_read];

	memcpy(entry, src_entry, queue->entry_size);

	/*
	 * Make sure that the message data is visible before increasing the
	 * counter of read messages.
	 */
	__asm__ volatile("dmb st" ::: "memory");

	queue->idx_read = (queue->idx_read + 1) % queue->entry_num;

	__asm__ volatile("dmb st" ::: "memory");

	return 0;
}