Repository: incubator-mynewt-larva Updated Branches: refs/heads/master 95ec4cafe -> 0c08cbb69
Addition of task packet queue utility Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/0c08cbb6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/0c08cbb6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/0c08cbb6 Branch: refs/heads/master Commit: 0c08cbb6925454397070bf86d900a945a44e6602 Parents: 95ec4ca Author: wes3 <w...@micosa.io> Authored: Thu Nov 19 15:08:47 2015 -0800 Committer: wes3 <w...@micosa.io> Committed: Thu Nov 19 15:08:54 2015 -0800 ---------------------------------------------------------------------- libs/util/include/util/tpq.h | 64 +++++++++++++++++++++++++++++ libs/util/src/tpq.c | 85 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/0c08cbb6/libs/util/include/util/tpq.h ---------------------------------------------------------------------- diff --git a/libs/util/include/util/tpq.h b/libs/util/include/util/tpq.h new file mode 100644 index 0000000..e0a536b --- /dev/null +++ b/libs/util/include/util/tpq.h @@ -0,0 +1,64 @@ +/** + * Copyright (c) 2015 Runtime Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __UTIL_TPQ_H__ +#define __UTIL_TPQ_H__ + +#include <os/queue.h> +#include <os/os_eventq.h> + +/* A task packet queue element */ +struct tpq_elem { + STAILQ_ENTRY(tpq_elem) tpq_next; +}; + +/* The task packet queue object */ +struct tpq +{ + STAILQ_HEAD(, tpq_elem) tpq_head; + struct os_event tpq_ev; +}; + +/** + * Put an element on a task packet queue and post an event to an event queue. + * + * @param evq Pointer to event queue + * @param tpq Pointer to task packet queue + * @param elem Pointer to element to enqueue + */ +void tpq_put(struct os_eventq *evq, struct tpq *tpq, struct tpq_elem *elem); + +/** + * Retrieve an element from a task packet queue. This removes the element at + * the head of the queue. + * + * @param head + * + * @return struct tpq_elem* + */ +struct tpq_elem *tpq_get(struct tpq *tpq); + +/** + * Initialize a task packet queue + * + * @param tpq Pointer to task packet queue + * @param ev_type Type of event. + * @param ev_arg Argument of event + * + * @return int + */ +void tpq_init(struct tpq *tpq, uint8_t ev_type, void *ev_arg); + +#endif /* __UTIL_TPQ_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/0c08cbb6/libs/util/src/tpq.c ---------------------------------------------------------------------- diff --git a/libs/util/src/tpq.c b/libs/util/src/tpq.c new file mode 100644 index 0000000..72133c0 --- /dev/null +++ b/libs/util/src/tpq.c @@ -0,0 +1,85 @@ +/** + * Copyright (c) 2015 Runtime Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "os/os.h" +#include "util/tpq.h" + +/** + * Put an element on a task packet queue and post an event to an event queue. + * + * @param evq Pointer to event queue + * @param tpq Pointer to task packet queue + * @param elem Pointer to element to enqueue + */ +void +tpq_put(struct os_eventq *evq, struct tpq *tpq, struct tpq_elem *elem) +{ + os_sr_t sr; + + OS_ENTER_CRITICAL(sr); + STAILQ_INSERT_TAIL(&tpq->tpq_head, elem, tpq_next); + OS_EXIT_CRITICAL(sr); + os_eventq_put(evq, &tpq->tpq_ev); +} + +/** + * Retrieve an element from a task packet queue. This removes the element at + * the head of the queue. + * + * @param head + * + * @return struct tpq_elem* + */ +struct tpq_elem * +tpq_get(struct tpq *tpq) +{ + os_sr_t sr; + struct tpq_elem *elem; + + OS_ENTER_CRITICAL(sr); + elem = STAILQ_FIRST(&tpq->tpq_head); + if (elem) { + STAILQ_REMOVE_HEAD(&tpq->tpq_head, tpq_next); + } + OS_EXIT_CRITICAL(sr); + + return elem; +} + +/** + * Initialize a task packet queue + * + * @param tpq Pointer to task packet queue + * @param ev_type Type of event. + * @param ev_arg Argument of event + * + * @return int + */ +void +tpq_init(struct tpq *tpq, uint8_t ev_type, void *ev_arg) +{ + struct os_event *ev; + + /* Initialize the task packet queue */ + STAILQ_INIT(&tpq->tpq_head); + + /* Initial task packet queue event */ + ev = &tpq->tpq_ev; + ev->ev_arg = ev_arg; + ev->ev_type = ev_type; + ev->ev_queued = 0; + STAILQ_NEXT(ev, ev_next) = NULL; +} +