-
Notifications
You must be signed in to change notification settings - Fork 13
lib: bm_queue: a dynamic queue implementation #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lemrey
wants to merge
1
commit into
nrfconnect:main
Choose a base branch
from
lemrey:bm_queue-kcompat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
/* | ||
* Copyright (c) 2016, Wind River Systems, Inc. | ||
* Copyright (c) 2025, Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <zephyr/sys/slist.h> | ||
#include <zephyr/sys/sflist.h> | ||
|
||
/** | ||
* @brief Queue. | ||
*/ | ||
struct bm_queue { | ||
sys_sflist_t data_q; | ||
}; | ||
|
||
/** | ||
* @brief Initialize a queue. | ||
* | ||
* This routine initializes a queue, prior to its first use. | ||
* | ||
* @param queue Queue. | ||
*/ | ||
void bm_queue_init(struct bm_queue *queue); | ||
|
||
/** | ||
* @brief Append an element to the end of a queue. | ||
* | ||
* This routine appends a data item to @a queue. A queue data item must be | ||
* aligned on a word boundary, and the first word of the item is reserved | ||
* for internal use. The data is not copied. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param data Data item. | ||
*/ | ||
void bm_queue_append(struct bm_queue *queue, void *data); | ||
|
||
/** | ||
* @brief Allocate an element and append it to the queue. | ||
* | ||
* This routine appends a data item to @a queue. There is an implicit memory | ||
* allocation on the system heap to create an additional temporary bookkeeping data structure, | ||
* which is automatically freed when the item is removed. The data itself is not copied. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param data Data item. | ||
* | ||
* @retval 0 on success | ||
* @retval -ENOMEM if there isn't sufficient RAM in the system heap | ||
*/ | ||
int32_t bm_queue_alloc_append(struct bm_queue *queue, void *data); | ||
|
||
/** | ||
* @brief Prepend an element to the queue. | ||
* | ||
* This routine prepends a data item to @a queue. A queue data item must be | ||
* aligned on a word boundary, and the first word of the item is reserved | ||
* for internal use. The data is not copied. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param data Data item. | ||
*/ | ||
void bm_queue_prepend(struct bm_queue *queue, void *data); | ||
|
||
/** | ||
* @brief Prepend an element to a queue. | ||
* | ||
* This routine prepends a data item to @a queue. There is an implicit memory | ||
* allocation to create an additional temporary bookkeeping data structure from | ||
* the system heap, which is automatically freed when the item is removed. | ||
* The data itself is not copied. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param data Data item. | ||
* | ||
* @retval 0 on success | ||
* @retval -ENOMEM if there isn't sufficient RAM in the system heap. | ||
*/ | ||
int32_t bm_queue_alloc_prepend(struct bm_queue *queue, void *data); | ||
|
||
/** | ||
* @brief Insert an element at a given position in the queue. | ||
* | ||
* This routine inserts a data item to @a queue after previous item. A queue | ||
* data item must be aligned on a word boundary, and the first word of | ||
* the item is reserved for internal use. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param prev Previous element. | ||
* @param data Data item. | ||
*/ | ||
void bm_queue_insert(struct bm_queue *queue, void *prev, void *data); | ||
|
||
/** | ||
* @brief Atomically append a list of elements to a queue. | ||
* | ||
* This routine adds a list of data items to @a queue in one operation. | ||
* The data items must be in a singly-linked list, with the first word | ||
* in each data item pointing to the next data item; the list must be | ||
* NULL-terminated. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param head Pointer to first node in singly-linked list. | ||
* @param tail Pointer to last node in singly-linked list. | ||
* | ||
* @retval 0 on success | ||
* @retval -EINVAL on invalid supplied data | ||
*/ | ||
int bm_queue_append_list(struct bm_queue *queue, void *head, void *tail); | ||
|
||
/** | ||
* @brief Atomically add a list of elements to a queue. | ||
* | ||
* This routine adds a list of data items to @a queue in one operation. | ||
* The data items must be in a singly-linked list implemented using a | ||
* sys_slist_t object. Upon completion, the original list is empty. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param list Pointer to sys_slist_t object. | ||
* | ||
* @retval 0 on success | ||
* @retval -EINVAL on invalid data | ||
*/ | ||
int bm_queue_merge_slist(struct bm_queue *queue, sys_slist_t *list); | ||
|
||
/** | ||
* @brief Get an element from a queue. | ||
* | ||
* This routine removes first data item from @a queue. The first word of the | ||
* data item is reserved for internal use. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* | ||
* @return Address of the data item if successful; NULL otherwise. | ||
*/ | ||
void *bm_queue_get(struct bm_queue *queue); | ||
|
||
/** | ||
* @brief Remove an element from a queue. | ||
* | ||
* This routine removes data item from @a queue. The first word of the | ||
* data item is reserved for internal use. Removing elements from bm_queue | ||
* rely on sys_slist_find_and_remove which is not a constant time operation. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param data Data item. | ||
* | ||
* @return true if data item was removed | ||
*/ | ||
bool bm_queue_remove(struct bm_queue *queue, void *data); | ||
|
||
/** | ||
* @brief Append an element to a queue only if it's not present already. | ||
* | ||
* This routine appends data item to @a queue. The first word of the data | ||
* item is reserved for internal use. Appending elements to bm_queue | ||
* relies on sys_slist_is_node_in_list which is not a constant time operation. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* @param data Data item. | ||
* | ||
* @return true if data item was added, false if not | ||
*/ | ||
bool bm_queue_unique_append(struct bm_queue *queue, void *data); | ||
|
||
/** | ||
* @brief Query a queue to see if it has data available. | ||
* | ||
* @funcprops \isr_ok | ||
* | ||
* @param queue Queue. | ||
* | ||
* @return Non-zero if the queue is empty. | ||
* @return 0 if data is available. | ||
*/ | ||
static inline bool bm_queue_is_empty(struct bm_queue *queue) | ||
{ | ||
return sys_sflist_is_empty(&queue->data_q); | ||
} | ||
|
||
/** | ||
* @brief Peek element at the head of queue. | ||
* | ||
* Return element from the head of queue without removing it. | ||
* | ||
* @param queue Queue. | ||
* | ||
* @return Head element, or NULL if queue is empty. | ||
*/ | ||
void *bm_queue_peek_head(struct bm_queue *queue); | ||
|
||
/** | ||
* @brief Peek element at the tail of queue. | ||
* | ||
* Return element from the tail of queue without removing it. | ||
* | ||
* @param queue Queue. | ||
* | ||
* @return Tail element, or NULL if queue is empty. | ||
*/ | ||
void *bm_queue_peek_tail(struct bm_queue *queue); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor ASA | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
zephyr_library() | ||
zephyr_library_sources(bm_queue.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
menuconfig BM_QUEUE | ||
bool "Queue library" | ||
help | ||
A simple, dynamic queue using singly linked lists. | ||
|
||
if BM_QUEUE | ||
|
||
config BM_QUEUE_K_QUEUE_COMPAT | ||
bool "Export k_queue compatible API" | ||
depends on !MULTITHREADING | ||
help | ||
Export a k_queue compatible API for compatibility with Zephyr subsystems. | ||
|
||
module=BM_QUEUE | ||
module-dep=LOG | ||
module-str=Queue library | ||
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" | ||
|
||
endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the license, since this file is mostly copied from kernel.h and also modified, the modifications must be marked clearly. A link to the the source file in the commit message would also be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is marked
* Copyright (c) 2025, Nordic Semiconductor ASA
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are our modifications also covered by Apache-2.0 license, then, and not the Nordic 5-clause license?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can't relicense the file, it has to remain under the original license