You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/03/15 01:44:05 UTC
[03/14] incubator-mynewt-blinky git commit: update structure to
represent blinky
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_eventq.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_eventq.c b/libs/os/src/os_eventq.c
deleted file mode 100644
index e3fe6e5..0000000
--- a/libs/os/src/os_eventq.c
+++ /dev/null
@@ -1,110 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <string.h>
-
-void
-os_eventq_init(struct os_eventq *evq)
-{
- memset(evq, 0, sizeof(*evq));
- STAILQ_INIT(&evq->evq_list);
-}
-
-void
-os_eventq_put2(struct os_eventq *evq, struct os_event *ev, int isr)
-{
- int resched;
- os_sr_t sr;
-
- OS_ENTER_CRITICAL(sr);
-
- /* Do not queue if already queued */
- if (OS_EVENT_QUEUED(ev)) {
- OS_EXIT_CRITICAL(sr);
- return;
- }
-
- /* Queue the event */
- ev->ev_queued = 1;
- STAILQ_INSERT_TAIL(&evq->evq_list, ev, ev_next);
-
- /* If task waiting on event, wake it up. */
- resched = 0;
- if (evq->evq_task) {
- os_sched_wakeup(evq->evq_task);
- evq->evq_task = NULL;
- resched = 1;
- }
-
- OS_EXIT_CRITICAL(sr);
-
- if (resched) {
- os_sched(NULL, isr);
- }
-}
-
-void
-os_eventq_put(struct os_eventq *evq, struct os_event *ev)
-{
- os_eventq_put2(evq, ev, 0);
-}
-
-struct os_event *
-os_eventq_get(struct os_eventq *evq)
-{
- struct os_event *ev;
- os_sr_t sr;
-
- OS_ENTER_CRITICAL(sr);
-pull_one:
- ev = STAILQ_FIRST(&evq->evq_list);
- if (ev) {
- STAILQ_REMOVE(&evq->evq_list, ev, os_event, ev_next);
- ev->ev_queued = 0;
- } else {
- evq->evq_task = os_sched_get_current_task();
- os_sched_sleep(evq->evq_task, OS_TIMEOUT_NEVER);
- OS_EXIT_CRITICAL(sr);
-
- os_sched(NULL, 0);
-
- OS_ENTER_CRITICAL(sr);
- evq->evq_task = NULL;
- goto pull_one;
- }
- OS_EXIT_CRITICAL(sr);
-
- return (ev);
-}
-
-void
-os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
-{
- os_sr_t sr;
-
- OS_ENTER_CRITICAL(sr);
- if (OS_EVENT_QUEUED(ev)) {
- STAILQ_REMOVE(&evq->evq_list, ev, os_event, ev_next);
- }
- ev->ev_queued = 0;
- OS_EXIT_CRITICAL(sr);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_heap.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_heap.c b/libs/os/src/os_heap.c
deleted file mode 100644
index a94d3ad..0000000
--- a/libs/os/src/os_heap.c
+++ /dev/null
@@ -1,79 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <assert.h>
-#include "os/os_mutex.h"
-#include "os/os_heap.h"
-
-static struct os_mutex os_malloc_mutex;
-
-static void
-os_malloc_lock(void)
-{
- int rc;
-
- if (g_os_started) {
- rc = os_mutex_pend(&os_malloc_mutex, 0xffffffff);
- assert(rc == 0);
- }
-}
-
-static void
-os_malloc_unlock(void)
-{
- int rc;
-
- if (g_os_started) {
- rc = os_mutex_release(&os_malloc_mutex);
- assert(rc == 0);
- }
-}
-
-void *
-os_malloc(size_t size)
-{
- void *ptr;
-
- os_malloc_lock();
- ptr = malloc(size);
- os_malloc_unlock();
-
- return ptr;
-}
-
-void
-os_free(void *mem)
-{
- os_malloc_lock();
- free(mem);
- os_malloc_unlock();
-}
-
-void *
-os_realloc(void *ptr, size_t size)
-{
- void *new_ptr;
-
- os_malloc_lock();
- new_ptr = realloc(ptr, size);
- os_malloc_unlock();
-
- return new_ptr;
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_mbuf.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mbuf.c b/libs/os/src/os_mbuf.c
deleted file mode 100644
index 28abaee..0000000
--- a/libs/os/src/os_mbuf.c
+++ /dev/null
@@ -1,987 +0,0 @@
-/*
- * Software in this file is based heavily on code written in the FreeBSD source
- * code repostiory. While the code is written from scratch, it contains
- * many of the ideas and logic flow in the original source, this is a
- * derivative work, and the following license applies as well:
- *
- * Copyright (c) 1982, 1986, 1988, 1991, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-
-#include "os/os.h"
-
-#include <string.h>
-
-STAILQ_HEAD(, os_mbuf_pool) g_msys_pool_list =
- STAILQ_HEAD_INITIALIZER(g_msys_pool_list);
-
-int
-os_mqueue_init(struct os_mqueue *mq, void *arg)
-{
- struct os_event *ev;
-
- STAILQ_INIT(&mq->mq_head);
-
- ev = &mq->mq_ev;
- memset(ev, 0, sizeof(*ev));
- ev->ev_arg = arg;
- ev->ev_type = OS_EVENT_T_MQUEUE_DATA;
-
- return (0);
-}
-
-
-struct os_mbuf *
-os_mqueue_get(struct os_mqueue *mq)
-{
- struct os_mbuf_pkthdr *mp;
- struct os_mbuf *m;
- os_sr_t sr;
-
- OS_ENTER_CRITICAL(sr);
- mp = STAILQ_FIRST(&mq->mq_head);
- if (mp) {
- STAILQ_REMOVE_HEAD(&mq->mq_head, omp_next);
- }
- OS_EXIT_CRITICAL(sr);
-
- if (mp) {
- m = OS_MBUF_PKTHDR_TO_MBUF(mp);
- } else {
- m = NULL;
- }
-
- return (m);
-}
-
-int
-os_mqueue_put(struct os_mqueue *mq, struct os_eventq *evq, struct os_mbuf *m)
-{
- struct os_mbuf_pkthdr *mp;
- os_sr_t sr;
- int rc;
-
- if (!OS_MBUF_IS_PKTHDR(m)) {
- rc = OS_EINVAL;
- goto err;
- }
-
- mp = OS_MBUF_PKTHDR(m);
-
- OS_ENTER_CRITICAL(sr);
- STAILQ_INSERT_TAIL(&mq->mq_head, mp, omp_next);
- OS_EXIT_CRITICAL(sr);
-
- /* Only post an event to the queue if its specified */
- if (evq) {
- os_eventq_put(evq, &mq->mq_ev);
- }
-
- return (0);
-err:
- return (rc);
-}
-
-int
-os_msys_register(struct os_mbuf_pool *new_pool)
-{
- struct os_mbuf_pool *pool;
-
- pool = NULL;
- STAILQ_FOREACH(pool, &g_msys_pool_list, omp_next) {
- if (new_pool->omp_databuf_len > pool->omp_databuf_len) {
- break;
- }
- }
-
- if (pool) {
- STAILQ_INSERT_AFTER(&g_msys_pool_list, pool, new_pool, omp_next);
- } else {
- STAILQ_INSERT_TAIL(&g_msys_pool_list, new_pool, omp_next);
- }
-
- return (0);
-}
-
-/**
- * De-registers all mbuf pools from msys.
- */
-void
-os_msys_reset(void)
-{
- STAILQ_INIT(&g_msys_pool_list);
-}
-
-static struct os_mbuf_pool *
-_os_msys_find_pool(uint16_t dsize)
-{
- struct os_mbuf_pool *pool;
-
- pool = NULL;
- STAILQ_FOREACH(pool, &g_msys_pool_list, omp_next) {
- if (dsize <= pool->omp_databuf_len) {
- break;
- }
- }
-
- if (!pool) {
- pool = STAILQ_LAST(&g_msys_pool_list, os_mbuf_pool, omp_next);
- }
-
- return (pool);
-}
-
-
-
-struct os_mbuf *
-os_msys_get(uint16_t dsize, uint16_t leadingspace)
-{
- struct os_mbuf *m;
- struct os_mbuf_pool *pool;
-
- pool = _os_msys_find_pool(dsize);
- if (!pool) {
- goto err;
- }
-
- m = os_mbuf_get(pool, leadingspace);
- return (m);
-err:
- return (NULL);
-}
-
-
-struct os_mbuf *
-os_msys_get_pkthdr(uint16_t dsize, uint16_t user_hdr_len)
-{
- uint16_t total_pkthdr_len;
- struct os_mbuf *m;
- struct os_mbuf_pool *pool;
-
- total_pkthdr_len = user_hdr_len + sizeof(struct os_mbuf_pkthdr);
- pool = _os_msys_find_pool(dsize + total_pkthdr_len);
- if (!pool) {
- goto err;
- }
-
- m = os_mbuf_get_pkthdr(pool, user_hdr_len);
- return (m);
-err:
- return (NULL);
-}
-
-
-/**
- * Initialize a pool of mbufs.
- *
- * @param omp The mbuf pool to initialize
- * @param mp The memory pool that will hold this mbuf pool
- * @param buf_len The length of the buffer itself.
- * @param nbufs The number of buffers in the pool
- *
- * @return 0 on success, error code on failure.
- */
-int
-os_mbuf_pool_init(struct os_mbuf_pool *omp, struct os_mempool *mp, uint16_t buf_len,
- uint16_t nbufs)
-{
- omp->omp_databuf_len = buf_len - sizeof(struct os_mbuf);
- omp->omp_mbuf_count = nbufs;
- omp->omp_pool = mp;
-
- return (0);
-}
-
-/**
- * Get an mbuf from the mbuf pool. The mbuf is allocated, and initialized
- * prior to being returned.
- *
- * @param omp The mbuf pool to return the packet from
- * @param leadingspace The amount of leadingspace to put before the data
- * section by default.
- *
- * @return An initialized mbuf on success, and NULL on failure.
- */
-struct os_mbuf *
-os_mbuf_get(struct os_mbuf_pool *omp, uint16_t leadingspace)
-{
- struct os_mbuf *om;
-
- om = os_memblock_get(omp->omp_pool);
- if (!om) {
- goto err;
- }
-
- SLIST_NEXT(om, om_next) = NULL;
- om->om_flags = 0;
- om->om_pkthdr_len = 0;
- om->om_len = 0;
- om->om_data = (&om->om_databuf[0] + leadingspace);
- om->om_omp = omp;
-
- return (om);
-err:
- return (NULL);
-}
-
-/* Allocate a new packet header mbuf out of the os_mbuf_pool */
-struct os_mbuf *
-os_mbuf_get_pkthdr(struct os_mbuf_pool *omp, uint8_t user_pkthdr_len)
-{
- struct os_mbuf_pkthdr *pkthdr;
- struct os_mbuf *om;
-
- om = os_mbuf_get(omp, 0);
- if (om) {
- om->om_pkthdr_len = user_pkthdr_len + sizeof(struct os_mbuf_pkthdr);
- om->om_data += user_pkthdr_len + sizeof(struct os_mbuf_pkthdr);
-
- pkthdr = OS_MBUF_PKTHDR(om);
- pkthdr->omp_len = 0;
- pkthdr->omp_flags = 0;
- STAILQ_NEXT(pkthdr, omp_next) = NULL;
- }
-
- return om;
-}
-
-/**
- * Release a mbuf back to the pool
- *
- * @param omp The Mbuf pool to release back to
- * @param om The Mbuf to release back to the pool
- *
- * @return 0 on success, -1 on failure
- */
-int
-os_mbuf_free(struct os_mbuf *om)
-{
- int rc;
-
- if (om->om_omp != NULL) {
- rc = os_memblock_put(om->om_omp->omp_pool, om);
- if (rc != 0) {
- goto err;
- }
- }
-
- return (0);
-err:
- return (rc);
-}
-
-/**
- * Free a chain of mbufs
- *
- * @param omp The mbuf pool to free the chain of mbufs into
- * @param om The starting mbuf of the chain to free back into the pool
- *
- * @return 0 on success, -1 on failure
- */
-int
-os_mbuf_free_chain(struct os_mbuf *om)
-{
- struct os_mbuf *next;
- int rc;
-
- while (om != NULL) {
- next = SLIST_NEXT(om, om_next);
-
- rc = os_mbuf_free(om);
- if (rc != 0) {
- goto err;
- }
-
- om = next;
- }
-
- return (0);
-err:
- return (rc);
-}
-
-/**
- * Copy a packet header from one mbuf to another.
- *
- * @param omp The mbuf pool associated with these buffers
- * @param new_buf The new buffer to copy the packet header into
- * @param old_buf The old buffer to copy the packet header from
- */
-static inline void
-_os_mbuf_copypkthdr(struct os_mbuf *new_buf, struct os_mbuf *old_buf)
-{
- memcpy(&new_buf->om_databuf[0], &old_buf->om_databuf[0],
- old_buf->om_pkthdr_len);
-}
-
-/**
- * Append data onto a mbuf
- *
- * @param om The mbuf to append the data onto
- * @param data The data to append onto the mbuf
- * @param len The length of the data to append
- *
- * @return 0 on success, and an error code on failure
- */
-int
-os_mbuf_append(struct os_mbuf *om, const void *data, uint16_t len)
-{
- struct os_mbuf_pool *omp;
- struct os_mbuf *last;
- struct os_mbuf *new;
- int remainder;
- int space;
- int rc;
-
- if (om == NULL) {
- rc = OS_EINVAL;
- goto err;
- }
-
- omp = om->om_omp;
-
- /* Scroll to last mbuf in the chain */
- last = om;
- while (SLIST_NEXT(last, om_next) != NULL) {
- last = SLIST_NEXT(last, om_next);
- }
-
- remainder = len;
- space = OS_MBUF_TRAILINGSPACE(last);
-
- /* If room in current mbuf, copy the first part of the data into the
- * remaining space in that mbuf.
- */
- if (space > 0) {
- if (space > remainder) {
- space = remainder;
- }
-
- memcpy(OS_MBUF_DATA(last, uint8_t *) + last->om_len , data, space);
-
- last->om_len += space;
- data += space;
- remainder -= space;
- }
-
- /* Take the remaining data, and keep allocating new mbufs and copying
- * data into it, until data is exhausted.
- */
- while (remainder > 0) {
- new = os_mbuf_get(omp, 0);
- if (!new) {
- break;
- }
-
- new->om_len = min(omp->omp_databuf_len, remainder);
- memcpy(OS_MBUF_DATA(new, void *), data, new->om_len);
- data += new->om_len;
- remainder -= new->om_len;
- SLIST_NEXT(last, om_next) = new;
- last = new;
- }
-
- /* Adjust the packet header length in the buffer */
- if (OS_MBUF_IS_PKTHDR(om)) {
- OS_MBUF_PKTHDR(om)->omp_len += len - remainder;
- }
-
- if (remainder != 0) {
- rc = OS_ENOMEM;
- goto err;
- }
-
-
- return (0);
-err:
- return (rc);
-}
-
-
-/**
- * Duplicate a chain of mbufs. Return the start of the duplicated chain.
- *
- * @param omp The mbuf pool to duplicate out of
- * @param om The mbuf chain to duplicate
- *
- * @return A pointer to the new chain of mbufs
- */
-struct os_mbuf *
-os_mbuf_dup(struct os_mbuf *om)
-{
- struct os_mbuf_pool *omp;
- struct os_mbuf *head;
- struct os_mbuf *copy;
-
- omp = om->om_omp;
-
- head = NULL;
- copy = NULL;
-
- for (; om != NULL; om = SLIST_NEXT(om, om_next)) {
- if (head) {
- SLIST_NEXT(copy, om_next) = os_mbuf_get(omp,
- OS_MBUF_LEADINGSPACE(om));
- if (!SLIST_NEXT(copy, om_next)) {
- os_mbuf_free_chain(head);
- goto err;
- }
-
- copy = SLIST_NEXT(copy, om_next);
- } else {
- head = os_mbuf_get(omp, OS_MBUF_LEADINGSPACE(om));
- if (!head) {
- goto err;
- }
-
- if (OS_MBUF_IS_PKTHDR(om)) {
- _os_mbuf_copypkthdr(head, om);
- }
- copy = head;
- }
- copy->om_flags = om->om_flags;
- copy->om_len = om->om_len;
- memcpy(OS_MBUF_DATA(copy, uint8_t *), OS_MBUF_DATA(om, uint8_t *),
- om->om_len);
- }
-
- return (head);
-err:
- return (NULL);
-}
-
-/**
- * Locates the specified absolute offset within an mbuf chain. The offset
- * can be one past than the total length of the chain, but no greater.
- *
- * @param om The start of the mbuf chain to seek within.
- * @param off The absolute address to find.
- * @param out_off On success, this points to the relative offset
- * within the returned mbuf.
- *
- * @return The mbuf containing the specified offset on
- * success.
- * NULL if the specified offset is out of bounds.
- */
-struct os_mbuf *
-os_mbuf_off(struct os_mbuf *om, int off, int *out_off)
-{
- struct os_mbuf *next;
-
- while (1) {
- if (om == NULL) {
- return NULL;
- }
-
- next = SLIST_NEXT(om, om_next);
-
- if (om->om_len > off ||
- (om->om_len == off && next == NULL)) {
-
- *out_off = off;
- return om;
- }
-
- off -= om->om_len;
- om = next;
- }
-}
-
-/*
- * Copy data from an mbuf chain starting "off" bytes from the beginning,
- * continuing for "len" bytes, into the indicated buffer.
- *
- * @return 0 on success;
- * -1 if the mbuf does not contain enough data.
- */
-int
-os_mbuf_copydata(const struct os_mbuf *m, int off, int len, void *dst)
-{
- unsigned int count;
- uint8_t *udst;
-
- udst = dst;
-
- while (off > 0) {
- if (!m) {
- return (-1);
- }
-
- if (off < m->om_len)
- break;
- off -= m->om_len;
- m = SLIST_NEXT(m, om_next);
- }
- while (len > 0 && m != NULL) {
- count = min(m->om_len - off, len);
- memcpy(udst, m->om_data + off, count);
- len -= count;
- udst += count;
- off = 0;
- m = SLIST_NEXT(m, om_next);
- }
-
- return (len > 0 ? -1 : 0);
-}
-
-void
-os_mbuf_adj(struct os_mbuf *mp, int req_len)
-{
- int len = req_len;
- struct os_mbuf *m;
- int count;
-
- if ((m = mp) == NULL)
- return;
- if (len >= 0) {
- /*
- * Trim from head.
- */
- while (m != NULL && len > 0) {
- if (m->om_len <= len) {
- len -= m->om_len;
- m->om_len = 0;
- m = SLIST_NEXT(m, om_next);
- } else {
- m->om_len -= len;
- m->om_data += len;
- len = 0;
- }
- }
- if (OS_MBUF_IS_PKTHDR(mp))
- OS_MBUF_PKTHDR(mp)->omp_len -= (req_len - len);
- } else {
- /*
- * Trim from tail. Scan the mbuf chain,
- * calculating its length and finding the last mbuf.
- * If the adjustment only affects this mbuf, then just
- * adjust and return. Otherwise, rescan and truncate
- * after the remaining size.
- */
- len = -len;
- count = 0;
- for (;;) {
- count += m->om_len;
- if (SLIST_NEXT(m, om_next) == (struct os_mbuf *)0)
- break;
- m = SLIST_NEXT(m, om_next);
- }
- if (m->om_len >= len) {
- m->om_len -= len;
- if (OS_MBUF_IS_PKTHDR(mp))
- OS_MBUF_PKTHDR(mp)->omp_len -= len;
- return;
- }
- count -= len;
- if (count < 0)
- count = 0;
- /*
- * Correct length for chain is "count".
- * Find the mbuf with last data, adjust its length,
- * and toss data from remaining mbufs on chain.
- */
- m = mp;
- if (OS_MBUF_IS_PKTHDR(m))
- OS_MBUF_PKTHDR(m)->omp_len = count;
- for (; m; m = SLIST_NEXT(m, om_next)) {
- if (m->om_len >= count) {
- m->om_len = count;
- if (SLIST_NEXT(m, om_next) != NULL) {
- os_mbuf_free_chain(SLIST_NEXT(m, om_next));
- SLIST_NEXT(m, om_next) = NULL;
- }
- break;
- }
- count -= m->om_len;
- }
- }
-}
-
-/**
- * Performs a memory compare of the specified region of an mbuf chain against a
- * flat buffer.
- *
- * @param om The start of the mbuf chain to compare.
- * @param off The offset within the mbuf chain to start the
- * comparison.
- * @param data The flat buffer to compare.
- * @param len The length of the flat buffer.
- *
- * @return 0 if both memory regions are identical;
- * A memcmp return code if there is a mismatch;
- * -1 if the mbuf is too short.
- */
-int
-os_mbuf_memcmp(const struct os_mbuf *om, int off, const void *data, int len)
-{
- int chunk_sz;
- int data_off;
- int om_off;
- int rc;
-
- if (len <= 0) {
- return 0;
- }
-
- data_off = 0;
- om = os_mbuf_off((struct os_mbuf *)om, off, &om_off);
- while (1) {
- if (om == NULL) {
- return -1;
- }
-
- chunk_sz = min(om->om_len - om_off, len - data_off);
- if (chunk_sz > 0) {
- rc = memcmp(om->om_data + om_off, data + data_off, chunk_sz);
- if (rc != 0) {
- return rc;
- }
- }
-
- data_off += chunk_sz;
- if (data_off == len) {
- return 0;
- }
-
- om = SLIST_NEXT(om, om_next);
- om_off = 0;
-
- if (om == NULL) {
- return -1;
- }
- }
-}
-
-/**
- * Increases the length of an mbuf chain by adding data to the front. If there
- * is insufficient room in the leading mbuf, additional mbufs are allocated and
- * prepended as necessary. If this function fails to allocate an mbuf, the
- * entire chain is freed.
- *
- * The specified mbuf chain does not need to contain a packet header.
- *
- * @param omp The mbuf pool to allocate from.
- * @param om The head of the mbuf chain.
- * @param len The number of bytes to prepend.
- *
- * @return The new head of the chain on success;
- * NULL on failure.
- */
-struct os_mbuf *
-os_mbuf_prepend(struct os_mbuf *om, int len)
-{
- struct os_mbuf *p;
- int leading;
-
- while (1) {
- /* Fill the available space at the front of the head of the chain, as
- * needed.
- */
- leading = min(len, OS_MBUF_LEADINGSPACE(om));
-
- om->om_data -= leading;
- om->om_len += leading;
- if (OS_MBUF_IS_PKTHDR(om)) {
- OS_MBUF_PKTHDR(om)->omp_len += leading;
- }
-
- len -= leading;
- if (len == 0) {
- break;
- }
-
- /* The current head didn't have enough space; allocate a new head. */
- if (OS_MBUF_IS_PKTHDR(om)) {
- p = os_mbuf_get_pkthdr(om->om_omp,
- om->om_pkthdr_len - sizeof (struct os_mbuf_pkthdr));
- } else {
- p = os_mbuf_get(om->om_omp, 0);
- }
- if (p == NULL) {
- os_mbuf_free_chain(om);
- om = NULL;
- break;
- }
-
- if (OS_MBUF_IS_PKTHDR(om)) {
- _os_mbuf_copypkthdr(p, om);
- om->om_pkthdr_len = 0;
- }
-
- /* Move the new head's data pointer to the end so that data can be
- * prepended.
- */
- p->om_data += OS_MBUF_TRAILINGSPACE(p);
-
- SLIST_NEXT(p, om_next) = om;
- om = p;
- }
-
- return om;
-}
-
-/**
- * Copies the contents of a flat buffer into an mbuf chain, starting at the
- * specified destination offset. If the mbuf is too small for the source data,
- * it is extended as necessary. If the destination mbuf contains a packet
- * header, the header length is updated.
- *
- * @param omp The mbuf pool to allocate from.
- * @param om The mbuf chain to copy into.
- * @param off The offset within the chain to copy to.
- * @param src The source buffer to copy from.
- * @param len The number of bytes to copy.
- *
- * @return 0 on success; nonzero on failure.
- */
-int
-os_mbuf_copyinto(struct os_mbuf *om, int off, const void *src, int len)
-{
- struct os_mbuf *next;
- struct os_mbuf *cur;
- const uint8_t *sptr;
- int copylen;
- int cur_off;
- int rc;
-
- /* Find the mbuf,offset pair for the start of the destination. */
- cur = os_mbuf_off(om, off, &cur_off);
- if (cur == NULL) {
- return -1;
- }
-
- /* Overwrite existing data until we reach the end of the chain. */
- sptr = src;
- while (1) {
- copylen = min(cur->om_len - cur_off, len);
- if (copylen > 0) {
- memcpy(cur->om_data + cur_off, sptr, copylen);
- sptr += copylen;
- len -= copylen;
-
- copylen = 0;
- }
-
- if (len == 0) {
- /* All the source data fit in the existing mbuf chain. */
- return 0;
- }
-
- next = SLIST_NEXT(cur, om_next);
- if (next == NULL) {
- break;
- }
-
- cur = next;
- }
-
- /* Append the remaining data to the end of the chain. */
- rc = os_mbuf_append(cur, sptr, len);
- if (rc != 0) {
- return rc;
- }
-
- /* Fix up the packet header, if one is present. */
- if (OS_MBUF_IS_PKTHDR(om)) {
- OS_MBUF_PKTHDR(om)->omp_len =
- max(OS_MBUF_PKTHDR(om)->omp_len, off + len);
- }
-
- return 0;
-}
-
-/**
- * Attaches a second mbuf chain onto the end of the first. If the first chain
- * contains a packet header, the header's length is updated. If the second
- * chain has a packet header, its header is cleared.
- *
- * @param first The mbuf chain being attached to.
- * @param second The mbuf chain that gets attached.
- */
-void
-os_mbuf_concat(struct os_mbuf *first, struct os_mbuf *second)
-{
- struct os_mbuf *next;
- struct os_mbuf *cur;
-
- /* Point 'cur' to the last buffer in the first chain. */
- cur = first;
- while (1) {
- next = SLIST_NEXT(cur, om_next);
- if (next == NULL) {
- break;
- }
-
- cur = next;
- }
-
- /* Attach the second chain to the end of the first. */
- SLIST_NEXT(cur, om_next) = second;
-
- /* If the first chain has a packet header, calculate the length of the
- * second chain and add it to the header length.
- */
- if (OS_MBUF_IS_PKTHDR(first)) {
- if (OS_MBUF_IS_PKTHDR(second)) {
- OS_MBUF_PKTHDR(first)->omp_len += OS_MBUF_PKTHDR(second)->omp_len;
- } else {
- for (cur = second; cur != NULL; cur = SLIST_NEXT(cur, om_next)) {
- OS_MBUF_PKTHDR(first)->omp_len += cur->om_len;
- }
- }
- }
-
- second->om_pkthdr_len = 0;
-}
-
-/**
- * Increases the length of an mbuf chain by the specified amount. If there is
- * not sufficient room in the last buffer, a new buffer is allocated and
- * appended to the chain. It is an error to request more data than can fit in
- * a single buffer.
- *
- * @param omp
- * @param om The head of the chain to extend.
- * @param len The number of bytes to extend by.
- *
- * @return A pointer to the new data on success;
- * NULL on failure.
- */
-void *
-os_mbuf_extend(struct os_mbuf *om, uint16_t len)
-{
- struct os_mbuf *newm;
- struct os_mbuf *last;
- void *data;
-
- if (len > om->om_omp->omp_databuf_len) {
- return NULL;
- }
-
- /* Scroll to last mbuf in the chain */
- last = om;
- while (SLIST_NEXT(last, om_next) != NULL) {
- last = SLIST_NEXT(last, om_next);
- }
-
- if (OS_MBUF_TRAILINGSPACE(last) < len) {
- newm = os_mbuf_get(om->om_omp, 0);
- if (newm == NULL) {
- return NULL;
- }
-
- SLIST_NEXT(last, om_next) = newm;
- last = newm;
- }
-
- data = last->om_data + last->om_len;
- last->om_len += len;
-
- if (OS_MBUF_IS_PKTHDR(om)) {
- OS_MBUF_PKTHDR(om)->omp_len += len;
- }
-
- return data;
-}
-
-/**
- * Rearrange a mbuf chain so that len bytes are contiguous,
- * and in the data area of an mbuf (so that OS_MBUF_DATA() will
- * work on a structure of size len.) Returns the resulting
- * mbuf chain on success, free's it and returns NULL on failure.
- *
- * If there is room, it will add up to "max_protohdr - len"
- * extra bytes to the contiguous region, in an attempt to avoid being
- * called next time.
- *
- * @param omp The mbuf pool to take the mbufs out of
- * @param om The mbuf chain to make contiguous
- * @param len The number of bytes in the chain to make contiguous
- *
- * @return The contiguous mbuf chain on success, NULL on failure.
- */
-struct os_mbuf *
-os_mbuf_pullup(struct os_mbuf *om, uint16_t len)
-{
- struct os_mbuf_pool *omp;
- struct os_mbuf *next;
- struct os_mbuf *om2;
- int count;
- int space;
-
- omp = om->om_omp;
-
- /*
- * If first mbuf has no cluster, and has room for len bytes
- * without shifting current data, pullup into it,
- * otherwise allocate a new mbuf to prepend to the chain.
- */
- if (om->om_len >= len) {
- return (om);
- }
- if (om->om_len + OS_MBUF_TRAILINGSPACE(om) >= len &&
- SLIST_NEXT(om, om_next)) {
- om2 = om;
- om = SLIST_NEXT(om, om_next);
- len -= om2->om_len;
- } else {
- if (len > omp->omp_databuf_len - om->om_pkthdr_len)
- goto bad;
- om2 = os_mbuf_get(omp, 0);
- if (om2 == NULL)
- goto bad;
- if (OS_MBUF_IS_PKTHDR(om))
- _os_mbuf_copypkthdr(om2, om);
- }
- space = OS_MBUF_TRAILINGSPACE(om2);
- do {
- count = min(min(len, space), om->om_len);
- memcpy(om2->om_data + om2->om_len, om->om_data, count);
- len -= count;
- om2->om_len += count;
- om->om_len -= count;
- space -= count;
- if (om->om_len) {
- om->om_data += count;
- } else {
- next = SLIST_NEXT(om, om_next);
- os_mbuf_free(om);
- om = next;
- }
- } while (len > 0 && om);
- if (len > 0) {
- os_mbuf_free(om2);
- goto bad;
- }
- SLIST_NEXT(om2, om_next) = om;
- return (om2);
-bad:
- os_mbuf_free_chain(om);
- return (NULL);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_mempool.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mempool.c b/libs/os/src/os_mempool.c
deleted file mode 100644
index 1e7428c..0000000
--- a/libs/os/src/os_mempool.c
+++ /dev/null
@@ -1,205 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <string.h>
-#include <assert.h>
-
-#define OS_MEMPOOL_TRUE_BLOCK_SIZE(bsize) OS_ALIGN(bsize, OS_ALIGNMENT)
-
-STAILQ_HEAD(, os_mempool) g_os_mempool_list =
- STAILQ_HEAD_INITIALIZER(g_os_mempool_list);
-
-/**
- * os mempool init
- *
- * Initialize a memory pool.
- *
- * @param mp Pointer to a pointer to a mempool
- * @param blocks The number of blocks in the pool
- * @param blocks_size The size of the block, in bytes.
- * @param membuf Pointer to memory to contain blocks.
- * @param name Name of the pool.
- *
- * @return os_error_t
- */
-os_error_t
-os_mempool_init(struct os_mempool *mp, int blocks, int block_size, void *membuf,
- char *name)
-{
- int true_block_size;
- uint8_t *block_addr;
- struct os_memblock *block_ptr;
-
- /* Check for valid parameters */
- if ((!mp) || (!membuf) || (blocks <= 0) || (block_size <= 0)) {
- return OS_INVALID_PARM;
- }
-
- /* Blocks need to be sized properly and memory buffer should be aligned */
- if (((uint32_t)membuf & (OS_ALIGNMENT - 1)) != 0) {
- return OS_MEM_NOT_ALIGNED;
- }
- true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(block_size);
-
- /* Initialize the memory pool structure */
- mp->mp_block_size = block_size;
- mp->mp_num_free = blocks;
- mp->mp_num_blocks = blocks;
- mp->mp_membuf_addr = (uint32_t)membuf;
- mp->name = name;
- SLIST_FIRST(mp) = membuf;
-
- /* Chain the memory blocks to the free list */
- block_addr = (uint8_t *)membuf;
- block_ptr = (struct os_memblock *)block_addr;
- while (blocks > 1) {
- block_addr += true_block_size;
- SLIST_NEXT(block_ptr, mb_next) = (struct os_memblock *)block_addr;
- block_ptr = (struct os_memblock *)block_addr;
- --blocks;
- }
-
- /* Last one in the list should be NULL */
- SLIST_NEXT(block_ptr, mb_next) = NULL;
-
- STAILQ_INSERT_TAIL(&g_os_mempool_list, mp, mp_list);
-
- return OS_OK;
-}
-
-/**
- * os memblock get
- *
- * Get a memory block from a memory pool
- *
- * @param mp Pointer to the memory pool
- *
- * @return void* Pointer to block if available; NULL otherwise
- */
-void *
-os_memblock_get(struct os_mempool *mp)
-{
- os_sr_t sr;
- struct os_memblock *block;
-
- /* Check to make sure they passed in a memory pool (or something) */
- block = NULL;
- if (mp) {
- OS_ENTER_CRITICAL(sr);
- /* Check for any free */
- if (mp->mp_num_free) {
- /* Get a free block */
- block = SLIST_FIRST(mp);
-
- /* Set new free list head */
- SLIST_FIRST(mp) = SLIST_NEXT(block, mb_next);
-
- /* Decrement number free by 1 */
- mp->mp_num_free--;
- }
- OS_EXIT_CRITICAL(sr);
- }
-
- return (void *)block;
-}
-
-/**
- * os memblock put
- *
- * Puts the memory block back into the pool
- *
- * @param mp Pointer to memory pool
- * @param block_addr Pointer to memory block
- *
- * @return os_error_t
- */
-os_error_t
-os_memblock_put(struct os_mempool *mp, void *block_addr)
-{
- os_sr_t sr;
- uint32_t end;
- uint32_t true_block_size;
- uint32_t baddr32;
- struct os_memblock *block;
-
- /* Make sure parameters are valid */
- if ((mp == NULL) || (block_addr == NULL)) {
- return OS_INVALID_PARM;
- }
-
- /* Check that the block we are freeing is a valid block! */
- baddr32 = (uint32_t)block_addr;
- true_block_size = OS_MEMPOOL_TRUE_BLOCK_SIZE(mp->mp_block_size);
- end = mp->mp_membuf_addr + (mp->mp_num_blocks * true_block_size);
- if ((baddr32 < mp->mp_membuf_addr) || (baddr32 >= end)) {
- return OS_INVALID_PARM;
- }
-
- /* All freed blocks should be on true block size boundaries! */
- if (((baddr32 - mp->mp_membuf_addr) % true_block_size) != 0) {
- return OS_INVALID_PARM;
- }
-
- /*
- * XXX: we should do boundary checks here! The block had better be within
- * the pool. If it fails, do we return an error or assert()? Add this when
- * we add the memory debug.
- */
- block = (struct os_memblock *)block_addr;
- OS_ENTER_CRITICAL(sr);
-
- /* Chain current free list pointer to this block; make this block head */
- SLIST_NEXT(block, mb_next) = SLIST_FIRST(mp);
- SLIST_FIRST(mp) = block;
-
- /* XXX: Should we check that the number free <= number blocks? */
- /* Increment number free */
- mp->mp_num_free++;
-
- OS_EXIT_CRITICAL(sr);
-
- return OS_OK;
-}
-
-
-struct os_mempool *
-os_mempool_info_get_next(struct os_mempool *mp, struct os_mempool_info *omi)
-{
- struct os_mempool *cur;
-
- if (mp == NULL) {
- cur = STAILQ_FIRST(&g_os_mempool_list);
- } else {
- cur = STAILQ_NEXT(mp, mp_list);
- }
-
- if (cur == NULL) {
- return (NULL);
- }
-
- omi->omi_block_size = cur->mp_block_size;
- omi->omi_num_blocks = cur->mp_num_blocks;
- omi->omi_num_free = cur->mp_num_free;
- strncpy(omi->omi_name, cur->name, sizeof(omi->omi_name));
-
- return (cur);
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_mutex.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mutex.c b/libs/os/src/os_mutex.c
deleted file mode 100644
index 454f6ab..0000000
--- a/libs/os/src/os_mutex.c
+++ /dev/null
@@ -1,298 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <assert.h>
-
-/**
- * os mutex create
- *
- * Create a mutex and initialize it.
- *
- * @param mu Pointer to mutex
- *
- * @return os_error_t
- * OS_INVALID_PARM Mutex passed in was NULL.
- * OS_OK no error.
- */
-os_error_t
-os_mutex_init(struct os_mutex *mu)
-{
- if (!mu) {
- return OS_INVALID_PARM;
- }
-
- /* Initialize to 0 */
- mu->mu_prio = 0;
- mu->mu_level = 0;
- mu->mu_owner = NULL;
- SLIST_FIRST(&mu->mu_head) = NULL;
-
- return OS_OK;
-}
-
-/**
- * os mutex release
- *
- * Release a mutex.
- *
- * @param mu Pointer to the mutex to be released
- *
- * @return os_error_t
- * OS_INVALID_PARM Mutex passed in was NULL.
- * OS_BAD_MUTEX Mutex was not granted to current task (not owner).
- * OS_OK No error
- */
-os_error_t
-os_mutex_release(struct os_mutex *mu)
-{
- int resched;
- os_sr_t sr;
- struct os_task *current;
- struct os_task *rdy;
-
- /* Check if OS is started */
- if (!g_os_started) {
- return (OS_NOT_STARTED);
- }
-
- /* Check for valid mutex */
- if (!mu) {
- return OS_INVALID_PARM;
- }
-
- /* We better own this mutex! */
- current = os_sched_get_current_task();
- if ((mu->mu_level == 0) || (mu->mu_owner != current)) {
- return (OS_BAD_MUTEX);
- }
-
- /* Decrement nesting level by 1. If not zero, nested (so dont release!) */
- --mu->mu_level;
- if (mu->mu_level != 0) {
- return (OS_OK);
- }
-
- OS_ENTER_CRITICAL(sr);
-
- /* Restore owner task's priority; resort list if different */
- if (current->t_prio != mu->mu_prio) {
- current->t_prio = mu->mu_prio;
- os_sched_resort(current);
- }
-
- /* Check if tasks are waiting for the mutex */
- rdy = SLIST_FIRST(&mu->mu_head);
- if (rdy) {
- /* There is one waiting. Wake it up */
- assert(rdy->t_obj);
- os_sched_wakeup(rdy);
-
- /* Set mutex internals */
- mu->mu_level = 1;
- mu->mu_prio = rdy->t_prio;
- }
-
- /* Set new owner of mutex (or NULL if not owned) */
- mu->mu_owner = rdy;
-
- /* Do we need to re-schedule? */
- resched = 0;
- rdy = os_sched_next_task();
- if (rdy != current) {
- resched = 1;
- }
- OS_EXIT_CRITICAL(sr);
-
- /* Re-schedule if needed */
- if (resched) {
- os_sched(rdy, 0);
- }
-
- return OS_OK;
-}
-
-/**
- * os mutex pend
- *
- * Pend (wait) for a mutex.
- *
- * @param mu Pointer to mutex.
- * @param timeout Timeout, in os ticks. A timeout of 0 means do
- * not wait if not available. A timeout of
- * 0xFFFFFFFF means wait forever.
- *
- *
- * @return os_error_t
- * OS_INVALID_PARM Mutex passed in was NULL.
- * OS_TIMEOUT Mutex was owned by another task and timeout=0
- * OS_OK no error.
- */
-os_error_t
-os_mutex_pend(struct os_mutex *mu, uint32_t timeout)
-{
- os_sr_t sr;
- os_error_t rc;
- struct os_task *current;
- struct os_task *entry;
- struct os_task *last;
-
- /* OS must be started when calling this function */
- if (!g_os_started) {
- return (OS_NOT_STARTED);
- }
-
- /* Check for valid mutex */
- if (!mu) {
- return OS_INVALID_PARM;
- }
-
- OS_ENTER_CRITICAL(sr);
-
- /* Is this owned? */
- current = os_sched_get_current_task();
- if (mu->mu_level == 0) {
- mu->mu_owner = current;
- mu->mu_prio = current->t_prio;
- mu->mu_level = 1;
- OS_EXIT_CRITICAL(sr);
- return OS_OK;
- }
-
- /* Are we owner? */
- if (mu->mu_owner == current) {
- ++mu->mu_level;
- OS_EXIT_CRITICAL(sr);
- return OS_OK;
- }
-
- /* Mutex is not owned by us. If timeout is 0, return immediately */
- if (timeout == 0) {
- OS_EXIT_CRITICAL(sr);
- return OS_TIMEOUT;
- }
-
- /* Change priority of owner if needed */
- if (mu->mu_owner->t_prio > current->t_prio) {
- mu->mu_owner->t_prio = current->t_prio;
- os_sched_resort(mu->mu_owner);
- }
-
- /* Link current task to tasks waiting for mutex */
- last = NULL;
- if (!SLIST_EMPTY(&mu->mu_head)) {
- /* Insert in priority order */
- SLIST_FOREACH(entry, &mu->mu_head, t_obj_list) {
- if (current->t_prio < entry->t_prio) {
- break;
- }
- last = entry;
- }
- }
-
- if (last) {
- SLIST_INSERT_AFTER(last, current, t_obj_list);
- } else {
- SLIST_INSERT_HEAD(&mu->mu_head, current, t_obj_list);
- }
-
- /* Set mutex pointer in task */
- current->t_obj = mu;
- current->t_flags |= OS_TASK_FLAG_MUTEX_WAIT;
- os_sched_sleep(current, timeout);
- OS_EXIT_CRITICAL(sr);
-
- os_sched(NULL, 0);
-
- OS_ENTER_CRITICAL(sr);
- current->t_flags &= ~OS_TASK_FLAG_MUTEX_WAIT;
- OS_EXIT_CRITICAL(sr);
-
- /* If we are owner we did not time out. */
- if (mu->mu_owner == current) {
- rc = OS_OK;
- } else {
- rc = OS_TIMEOUT;
- }
-
- return rc;
-}
-
-/**
- * os mutex delete
- *
- * Delete a mutex.
- *
- * @param mu Pointer to mutex to delete
- *
- * @return os_error_t
- * OS_INVALID_PARM Mutex passed in was NULL.
- * OS_OK no error.
- */
-os_error_t
-os_mutex_delete(struct os_mutex *mu)
-{
- os_sr_t sr;
- struct os_task *current;
- struct os_task *rdy;
-
- /* OS must be started to call this function */
- if (!g_os_started) {
- return (OS_NOT_STARTED);
- }
-
- /* Check for valid mutex */
- if (!mu) {
- return OS_INVALID_PARM;
- }
-
- /* Get currently running task */
- current = os_sched_get_current_task();
-
- OS_ENTER_CRITICAL(sr);
-
- /* Restore owner task's priority. */
- if (mu->mu_level != 0) {
- if (mu->mu_owner->t_prio != mu->mu_prio) {
- /* We have to resort the ready list if this task is ready */
- mu->mu_owner->t_prio = mu->mu_prio;
- os_sched_resort(mu->mu_owner);
- }
- }
-
- /* Now, go through all the tasks waiting on the mutex */
- while (!SLIST_EMPTY(&mu->mu_head)) {
- rdy = SLIST_FIRST(&mu->mu_head);
- assert(rdy->t_obj);
- os_sched_wakeup(rdy);
- }
-
- /* Is there a task that is ready that is higher priority than us? */
- rdy = os_sched_next_task();
- if (rdy != current) {
- /* Re-schedule */
- OS_EXIT_CRITICAL(sr);
- os_sched(rdy, 0);
- } else {
- OS_EXIT_CRITICAL(sr);
- }
-
- return OS_OK;
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_priv.h
----------------------------------------------------------------------
diff --git a/libs/os/src/os_priv.h b/libs/os/src/os_priv.h
deleted file mode 100644
index 93a0c3a..0000000
--- a/libs/os/src/os_priv.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 H_OS_PRIV_
-#define H_OS_PRIV_
-
-TAILQ_HEAD(os_task_list, os_task);
-
-extern struct os_task_list g_os_run_list;
-extern struct os_task_list g_os_sleep_list;
-extern struct os_task *g_current_task;
-
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_sanity.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_sanity.c b/libs/os/src/os_sanity.c
deleted file mode 100644
index 0fb2450..0000000
--- a/libs/os/src/os_sanity.c
+++ /dev/null
@@ -1,230 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <assert.h>
-#include <string.h>
-
-#include "os/os.h"
-
-SLIST_HEAD(, os_sanity_check) g_os_sanity_check_list =
- SLIST_HEAD_INITIALIZER(os_sanity_check_list);
-
-struct os_mutex g_os_sanity_check_mu;
-
-struct os_task g_os_sanity_task;
-os_stack_t g_os_sanity_task_stack[OS_STACK_ALIGN(OS_SANITY_STACK_SIZE)];
-
-
-/**
- * Initialize a sanity check
- *
- * @param sc The sanity check to initialize
- *
- * @return 0 on success, error code on failure.
- */
-int
-os_sanity_check_init(struct os_sanity_check *sc)
-{
- memset(sc, 0, sizeof(*sc));
-
- return (0);
-}
-
-/**
- * Lock the sanity check list
- *
- * @return 0 on success, error code on failure.
- */
-static int
-os_sanity_check_list_lock(void)
-{
- int rc;
-
- if (!g_os_started) {
- return (0);
- }
-
- rc = os_mutex_pend(&g_os_sanity_check_mu, OS_WAIT_FOREVER);
- if (rc != OS_OK) {
- goto err;
- }
-
- return (0);
-err:
- return (rc);
-}
-
-/**
- * Unlock the sanity check list
- *
- * @return 0 on success, error code on failure
- */
-static int
-os_sanity_check_list_unlock(void)
-{
- int rc;
-
- if (!g_os_started) {
- return (0);
- }
-
- rc = os_mutex_release(&g_os_sanity_check_mu);
- if (rc != 0) {
- goto err;
- }
-
- return (0);
-err:
- return (rc);
-}
-
-/**
- * Register a sanity check
- *
- * @param sc The sanity check to register
- *
- * @return 0 on success, error code on failure
- */
-int
-os_sanity_check_register(struct os_sanity_check *sc)
-{
- int rc;
-
- rc = os_sanity_check_list_lock();
- if (rc != OS_OK) {
- goto err;
- }
-
- SLIST_INSERT_HEAD(&g_os_sanity_check_list, sc, sc_next);
-
- rc = os_sanity_check_list_unlock();
- if (rc != OS_OK) {
- goto err;
- }
-
- return (0);
-err:
- return (rc);
-}
-
-
-/**
- * Reset the os sanity check, so that it doesn't trip up the
- * sanity timer.
- *
- * @param sc The sanity check to reset
- *
- * @return 0 on success, error code on failure
- */
-int
-os_sanity_check_reset(struct os_sanity_check *sc)
-{
- int rc;
-
- rc = os_sanity_check_list_lock();
- if (rc != OS_OK) {
- goto err;
- }
-
- sc->sc_checkin_last = os_time_get();
-
- rc = os_sanity_check_list_unlock();
- if (rc != OS_OK) {
- goto err;
- }
-
- return (0);
-err:
- return (rc);
-}
-
-/**
- * The main sanity check task loop. This executes every SANITY_CHECK_NUM_SECS
- * and goes through to see if any of the registered sanity checks are expired.
- * If the sanity checks have expired, it restarts the operating system.
- *
- * @param arg unused
- *
- * @return never
- */
-static void
-os_sanity_task_loop(void *arg)
-{
- struct os_sanity_check *sc;
- int rc;
-
- while (1) {
- rc = os_sanity_check_list_lock();
- if (rc != 0) {
- assert(0);
- }
-
- SLIST_FOREACH(sc, &g_os_sanity_check_list, sc_next) {
- rc = OS_OK;
-
- if (sc->sc_func) {
- rc = sc->sc_func(sc, sc->sc_arg);
- if (rc == OS_OK) {
- sc->sc_checkin_last = os_time_get();
- continue;
- }
- }
-
- if (OS_TIME_TICK_GT(os_time_get() - sc->sc_checkin_last,
- sc->sc_checkin_itvl)) {
- assert(0);
- }
- }
-
-
- rc = os_sanity_check_list_unlock();
- if (rc != 0) {
- assert(0);
- }
-
- os_time_delay(OS_TICKS_PER_SEC);
- }
-}
-
-/**
- * Initialize the sanity task and mutex.
- *
- * @return 0 on success, error code on failure
- */
-int
-os_sanity_task_init(void)
-{
- int rc;
-
- rc = os_mutex_init(&g_os_sanity_check_mu);
- if (rc != 0) {
- goto err;
- }
-
- rc = os_task_init(&g_os_sanity_task, "os_sanity", os_sanity_task_loop,
- NULL, OS_SANITY_PRIO, OS_WAIT_FOREVER, g_os_sanity_task_stack,
- OS_STACK_ALIGN(OS_SANITY_STACK_SIZE));
- if (rc != OS_OK) {
- goto err;
- }
-
- return (0);
-err:
- return (rc);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_sched.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_sched.c b/libs/os/src/os_sched.c
deleted file mode 100644
index d5ce25d..0000000
--- a/libs/os/src/os_sched.c
+++ /dev/null
@@ -1,314 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 "os/queue.h"
-
-#include <assert.h>
-
-TAILQ_HEAD(, os_task) g_os_run_list = TAILQ_HEAD_INITIALIZER(g_os_run_list);
-
-TAILQ_HEAD(, os_task) g_os_sleep_list = TAILQ_HEAD_INITIALIZER(g_os_sleep_list);
-
-struct os_task *g_current_task;
-
-extern os_time_t g_os_time;
-os_time_t g_os_last_ctx_sw_time;
-
-/**
- * os sched insert
- *
- * Insert a task into the scheduler list. This causes the task to be evaluated
- * for running when os_sched is called.
- *
- * @param t Pointer to task to insert in run list
- *
- * @return int OS_OK: task was inserted into run list
- * OS_EINVAL: Task was not in ready state.
- */
-os_error_t
-os_sched_insert(struct os_task *t)
-{
- struct os_task *entry;
- os_sr_t sr;
- os_error_t rc;
-
- if (t->t_state != OS_TASK_READY) {
- rc = OS_EINVAL;
- goto err;
- }
-
- entry = NULL;
- OS_ENTER_CRITICAL(sr);
- TAILQ_FOREACH(entry, &g_os_run_list, t_os_list) {
- if (t->t_prio < entry->t_prio) {
- break;
- }
- }
- if (entry) {
- TAILQ_INSERT_BEFORE(entry, (struct os_task *) t, t_os_list);
- } else {
- TAILQ_INSERT_TAIL(&g_os_run_list, (struct os_task *) t, t_os_list);
- }
- OS_EXIT_CRITICAL(sr);
-
- return (0);
-err:
- return (rc);
-}
-
-void
-os_sched_ctx_sw_hook(struct os_task *next_t)
-{
- if (g_current_task == next_t) {
- return;
- }
-
- next_t->t_ctx_sw_cnt++;
- g_current_task->t_run_time += g_os_time - g_os_last_ctx_sw_time;
- g_os_last_ctx_sw_time = g_os_time;
-}
-
-
-/**
- * os sched get current task
- *
- * Returns the currently running task. Note that this task may or may not be
- * the highest priority task ready to run.
- *
- *
- * @return struct os_task*
- */
-struct os_task *
-os_sched_get_current_task(void)
-{
- return (g_current_task);
-}
-
-/**
- * os sched set current task
- *
- * Sets the currently running task to 't'. Note that this function simply sets
- * the global variable holding the currently running task. It does not perform
- * a context switch or change the os run or sleep list.
- *
- * @param t Pointer to currently running task.
- */
-void
-os_sched_set_current_task(struct os_task *t)
-{
- g_current_task = t;
-}
-
-/**
- * os sched
- *
- * Performs a context switch. When called, it will either find the highest
- * priority task ready to run if next_t is NULL (i.e. the head of the os run
- * list) or will schedule next_t as the task to run.
- *
- * @param next_t Task to run
- * @param isr Flag denoting whether we are inside an isr (0:no, 1:yes).
- */
-void
-os_sched(struct os_task *next_t, int isr)
-{
- os_sr_t sr;
-
- OS_ENTER_CRITICAL(sr);
-
- if (!next_t) {
- next_t = os_sched_next_task();
- }
-
- if (next_t != g_current_task) {
- OS_EXIT_CRITICAL(sr);
- if (isr) {
- os_arch_ctx_sw_isr(next_t);
- } else {
- os_arch_ctx_sw(next_t);
- }
-
- } else {
- OS_EXIT_CRITICAL(sr);
- }
-}
-
-/**
- * os sched sleep
- *
- * Removes the task from the run list and puts it on the sleep list.
- *
- * @param t Task to put to sleep
- * @param nticks Number of ticks to put task to sleep
- *
- * @return int
- *
- * NOTE: must be called with interrupts disabled! This function does not call
- * the scheduler
- */
-int
-os_sched_sleep(struct os_task *t, os_time_t nticks)
-{
- struct os_task *entry;
-
- entry = NULL;
-
- TAILQ_REMOVE(&g_os_run_list, t, t_os_list);
- t->t_state = OS_TASK_SLEEP;
- t->t_next_wakeup = os_time_get() + nticks;
- if (nticks == OS_TIMEOUT_NEVER) {
- t->t_flags |= OS_TASK_FLAG_NO_TIMEOUT;
- TAILQ_INSERT_TAIL(&g_os_sleep_list, t, t_os_list);
- } else {
- TAILQ_FOREACH(entry, &g_os_sleep_list, t_os_list) {
- if ((entry->t_flags & OS_TASK_FLAG_NO_TIMEOUT) ||
- OS_TIME_TICK_GT(entry->t_next_wakeup, t->t_next_wakeup)) {
- break;
- }
- }
- if (entry) {
- TAILQ_INSERT_BEFORE(entry, t, t_os_list);
- } else {
- TAILQ_INSERT_TAIL(&g_os_sleep_list, t, t_os_list);
- }
- }
-
- return (0);
-}
-
-/**
- * os sched wakeup
- *
- * Called to wake up a task. Waking up a task consists of setting the task state
- * to READY and moving it from the sleep list to the run list.
- *
- * @param t Pointer to task to wake up.
- *
- * @return int
- *
- * NOTE: This function must be called with interrupts disabled.
- */
-int
-os_sched_wakeup(struct os_task *t)
-{
- struct os_task_obj *os_obj;
-
- assert(t->t_state == OS_TASK_SLEEP);
-
- /* Remove self from object list if waiting on one */
- if (t->t_obj) {
- os_obj = (struct os_task_obj *)t->t_obj;
- assert(!SLIST_EMPTY(&os_obj->obj_head));
- SLIST_REMOVE(&os_obj->obj_head, t, os_task, t_obj_list);
- SLIST_NEXT(t, t_obj_list) = NULL;
- t->t_obj = NULL;
- }
-
- /* Remove task from sleep list */
- t->t_state = OS_TASK_READY;
- t->t_next_wakeup = 0;
- t->t_flags &= ~OS_TASK_FLAG_NO_TIMEOUT;
- TAILQ_REMOVE(&g_os_sleep_list, t, t_os_list);
- os_sched_insert(t);
-
- return (0);
-}
-
-/**
- * os sched os timer exp
- *
- * Called when the OS tick timer expires. Search the sleep list for any tasks
- * that need waking up. This occurs when the current OS time exceeds the next
- * wakeup time stored in the task. Any tasks that need waking up will be
- * removed from the sleep list and added to the run list.
- *
- */
-void
-os_sched_os_timer_exp(void)
-{
- struct os_task *t;
- struct os_task *next;
- os_time_t now;
- os_sr_t sr;
-
- now = os_time_get();
-
- OS_ENTER_CRITICAL(sr);
-
- /*
- * Wakeup any tasks that have their sleep timer expired
- */
- t = TAILQ_FIRST(&g_os_sleep_list);
- while (t) {
- /* If task waiting forever, do not check next wakeup time */
- if (t->t_flags & OS_TASK_FLAG_NO_TIMEOUT) {
- break;
- }
- next = TAILQ_NEXT(t, t_os_list);
- if (OS_TIME_TICK_GEQ(now, t->t_next_wakeup)) {
- os_sched_wakeup(t);
- } else {
- break;
- }
- t = next;
- }
-
- OS_EXIT_CRITICAL(sr);
-}
-
-/**
- * os sched next task
- *
- * Returns the task that we should be running. This is the task at the head
- * of the run list.
- *
- * NOTE: if you want to guarantee that the os run list does not change after
- * calling this function you have to call it with interrupts disabled.
- *
- * @return struct os_task*
- */
-struct os_task *
-os_sched_next_task(void)
-{
- return (TAILQ_FIRST(&g_os_run_list));
-}
-
-/**
- * os sched resort
- *
- * Resort a task that is in the ready list as its priority has
- * changed. If the task is not in the ready state, there is
- * nothing to do.
- *
- * @param t Pointer to task to insert back into ready to run
- * list.
- *
- * NOTE: this function expects interrupts to be disabled so they
- * are not disabled here.
- */
-void
-os_sched_resort(struct os_task *t)
-{
- if (t->t_state == OS_TASK_READY) {
- TAILQ_REMOVE(&g_os_run_list, t, t_os_list);
- os_sched_insert(t);
- }
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_sem.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_sem.c b/libs/os/src/os_sem.c
deleted file mode 100644
index f96efa7..0000000
--- a/libs/os/src/os_sem.c
+++ /dev/null
@@ -1,275 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <assert.h>
-
-/* XXX:
- * 1) Should I check to see if we are within an ISR for some of these?
- * 2) Would I do anything different for os_sem_release() if we were in an
- * ISR when this was called?
- */
-
-/**
- * os sem create
- *
- * Create a semaphore and initialize it.
- *
- * @param sem Pointer to semaphore
- * tokens: # of tokens the semaphore should contain initially.
- *
- * @return os_error_t
- * OS_INVALID_PARM Semaphore passed in was NULL.
- * OS_OK no error.
- */
-os_error_t
-os_sem_init(struct os_sem *sem, uint16_t tokens)
-{
- if (!sem) {
- return OS_INVALID_PARM;
- }
-
- sem->sem_tokens = tokens;
- SLIST_FIRST(&sem->sem_head) = NULL;
-
- return OS_OK;
-}
-
-/**
- * os sem release
- *
- * Release a semaphore.
- *
- * @param sem Pointer to the semaphore to be released
- *
- * @return os_error_t
- * OS_INVALID_PARM Semaphore passed in was NULL.
- * OS_OK No error
- */
-os_error_t
-os_sem_release(struct os_sem *sem)
-{
- int resched;
- os_sr_t sr;
- struct os_task *current;
- struct os_task *rdy;
-
- /* OS must be started to release semaphores */
- if (!g_os_started) {
- return (OS_NOT_STARTED);
- }
-
- /* Check for valid semaphore */
- if (!sem) {
- return OS_INVALID_PARM;
- }
-
- /* Get current task */
- resched = 0;
- current = os_sched_get_current_task();
-
- OS_ENTER_CRITICAL(sr);
-
- /* Check if tasks are waiting for the semaphore */
- rdy = SLIST_FIRST(&sem->sem_head);
- if (rdy) {
- /* Clear flag that we are waiting on the semaphore; wake up task */
- rdy->t_flags &= ~OS_TASK_FLAG_SEM_WAIT;
- os_sched_wakeup(rdy);
-
- /* Schedule if waiting task higher priority */
- if (current->t_prio > rdy->t_prio) {
- resched = 1;
- }
- } else {
- /* Add to the number of tokens */
- sem->sem_tokens++;
- }
-
- OS_EXIT_CRITICAL(sr);
-
- /* Re-schedule if needed */
- if (resched) {
- os_sched(rdy, 0);
- }
-
- return OS_OK;
-}
-
-/**
- * os sem pend
- *
- * Pend (wait) for a semaphore.
- *
- * @param mu Pointer to semaphore.
- * @param timeout Timeout, in os ticks. A timeout of 0 means do
- * not wait if not available. A timeout of
- * 0xFFFFFFFF means wait forever.
- *
- *
- * @return os_error_t
- * OS_INVALID_PARM Semaphore passed in was NULL.
- * OS_TIMEOUT Semaphore was owned by another task and timeout=0
- * OS_OK no error.
- */
-os_error_t
-os_sem_pend(struct os_sem *sem, uint32_t timeout)
-{
- os_sr_t sr;
- os_error_t rc;
- int sched;
- struct os_task *current;
- struct os_task *entry;
- struct os_task *last;
-
- /* Check if OS is started */
- if (!g_os_started) {
- return (OS_NOT_STARTED);
- }
-
- /* Check for valid semaphore */
- if (!sem) {
- return OS_INVALID_PARM;
- }
-
- /* Assume we dont have to put task to sleep; get current task */
- sched = 0;
- current = os_sched_get_current_task();
-
- OS_ENTER_CRITICAL(sr);
-
- /*
- * If there is a token available, take it. If no token, either return
- * with error if timeout was 0 or put this task to sleep.
- */
- if (sem->sem_tokens != 0) {
- sem->sem_tokens--;
- rc = OS_OK;
- } else if (timeout == 0) {
- rc = OS_TIMEOUT;
- } else {
- /* Silence gcc maybe-uninitialized warning. */
- rc = OS_OK;
-
- /* Link current task to tasks waiting for semaphore */
- current->t_obj = sem;
- current->t_flags |= OS_TASK_FLAG_SEM_WAIT;
- last = NULL;
- if (!SLIST_EMPTY(&sem->sem_head)) {
- /* Insert in priority order */
- SLIST_FOREACH(entry, &sem->sem_head, t_obj_list) {
- if (current->t_prio < entry->t_prio) {
- break;
- }
- last = entry;
- }
- }
-
- if (last) {
- SLIST_INSERT_AFTER(last, current, t_obj_list);
- } else {
- SLIST_INSERT_HEAD(&sem->sem_head, current, t_obj_list);
- }
-
- /* We will put this task to sleep */
- sched = 1;
- os_sched_sleep(current, timeout);
- }
-
- OS_EXIT_CRITICAL(sr);
-
- if (sched) {
- os_sched(NULL, 0);
- /* Check if we timed out or got the semaphore */
- if (current->t_flags & OS_TASK_FLAG_SEM_WAIT) {
- OS_ENTER_CRITICAL(sr);
- current->t_flags &= ~OS_TASK_FLAG_SEM_WAIT;
- OS_EXIT_CRITICAL(sr);
- rc = OS_TIMEOUT;
- } else {
- rc = OS_OK;
- }
- }
-
- return rc;
-}
-
-/**
- * os sem delete
- *
- * Delete a semaphore.
- *
- * @param mu Pointer to semaphore to delete
- *
- * @return os_error_t
- * OS_INVALID_PARM Semaphore passed in was NULL.
- * OS_OK no error.
- */
-os_error_t
-os_sem_delete(struct os_sem *sem)
-{
- int resched;
- os_sr_t sr;
- struct os_task *current;
- struct os_task *rdy;
-
- /* OS must be started in order to call this function */
- if (!g_os_started) {
- return (OS_NOT_STARTED);
- }
-
- /* Check for valid semaphore */
- if (!sem) {
- return OS_INVALID_PARM;
- }
-
- /* Get currently running task */
- resched = 0;
- current = os_sched_get_current_task();
-
- OS_ENTER_CRITICAL(sr);
-
- /* Remove all tokens from semaphore */
- sem->sem_tokens = 0;
-
- /* Any tasks waiting? */
- rdy = SLIST_FIRST(&sem->sem_head);
- if (rdy) {
- /* If higher priority than current task, we must resched */
- if (current->t_prio > rdy->t_prio) {
- resched = 1;
- }
-
- /* Now, go through all the tasks waiting on the semaphore */
- while (rdy != NULL) {
- os_sched_wakeup(rdy);
- rdy = SLIST_FIRST(&sem->sem_head);
- }
- }
-
- OS_EXIT_CRITICAL(sr);
-
- /* Re-schedule if needed*/
- if (resched) {
- os_sched(rdy, 0);
- }
-
- return OS_OK;
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_task.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_task.c b/libs/os/src/os_task.c
deleted file mode 100644
index 16db2a1..0000000
--- a/libs/os/src/os_task.c
+++ /dev/null
@@ -1,180 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <string.h>
-
-uint8_t g_task_id;
-
-STAILQ_HEAD(, os_task) g_os_task_list = STAILQ_HEAD_INITIALIZER(g_os_task_list);
-
-static void
-_clear_stack(os_stack_t *stack_bottom, int size)
-{
- int i;
-
- for (i = 0; i < size; i++) {
- stack_bottom[i] = OS_STACK_PATTERN;
- }
-}
-
-static inline uint8_t
-os_task_next_id(void)
-{
- uint8_t rc;
- os_sr_t sr;
-
- OS_ENTER_CRITICAL(sr);
- rc = g_task_id;
- g_task_id++;
- OS_EXIT_CRITICAL(sr);
-
- return (rc);
-}
-
-uint8_t
-os_task_count(void)
-{
- return (g_task_id);
-}
-
-int
-os_task_sanity_checkin(struct os_task *t)
-{
- int rc;
-
- if (t == NULL) {
- t = os_sched_get_current_task();
- }
-
- rc = os_sanity_check_reset(&t->t_sanity_check);
- if (rc != OS_OK) {
- goto err;
- }
-
- return (0);
-err:
- return (rc);
-}
-
-
-
-int
-os_task_init(struct os_task *t, char *name, os_task_func_t func, void *arg,
- uint8_t prio, os_time_t sanity_itvl, os_stack_t *stack_bottom,
- uint16_t stack_size)
-{
- struct os_sanity_check *sc;
- int rc;
-
- memset(t, 0, sizeof(*t));
-
- t->t_func = func;
- t->t_arg = arg;
-
- t->t_taskid = os_task_next_id();
- t->t_prio = prio;
-
- t->t_state = OS_TASK_READY;
- t->t_name = name;
- t->t_next_wakeup = 0;
-
- rc = os_sanity_check_init(&t->t_sanity_check);
- if (rc != OS_OK) {
- goto err;
- }
-
- if (sanity_itvl != OS_WAIT_FOREVER) {
- sc = (struct os_sanity_check *) &t->t_sanity_check;
- sc->sc_checkin_itvl = sanity_itvl;
-
- rc = os_sanity_check_register(sc);
- if (rc != OS_OK) {
- goto err;
- }
- }
-
- _clear_stack(stack_bottom, stack_size);
- t->t_stackptr = os_arch_task_stack_init(t, &stack_bottom[stack_size],
- stack_size);
- t->t_stacktop = &stack_bottom[stack_size];
- t->t_stacksize = stack_size;
-
- /* insert this task into the task list */
- STAILQ_INSERT_TAIL(&g_os_task_list, t, t_os_task_list);
-
- /* insert this task into the scheduler list */
- rc = os_sched_insert(t);
- if (rc != OS_OK) {
- goto err;
- }
-
-
- return (0);
-err:
- return (rc);
-}
-
-struct os_task *
-os_task_info_get_next(const struct os_task *prev, struct os_task_info *oti)
-{
- struct os_task *next;
- os_stack_t *top;
- os_stack_t *bottom;
-
- if (prev != NULL) {
- next = STAILQ_NEXT(prev, t_os_task_list);
- } else {
- next = STAILQ_FIRST(&g_os_task_list);
- }
-
- if (next == NULL) {
- return (NULL);
- }
-
- /* Otherwise, copy OS task information into the OTI structure, and
- * return 1, which means continue
- */
- oti->oti_prio = next->t_prio;
- oti->oti_taskid = next->t_taskid;
- oti->oti_state = next->t_state;
-
- top = next->t_stacktop;
- bottom = next->t_stacktop - next->t_stacksize;
- while (bottom < top) {
- if (*bottom != OS_STACK_PATTERN) {
- break;
- }
- ++bottom;
- }
-
- oti->oti_stkusage = (uint16_t) (next->t_stacktop - bottom);
- oti->oti_stksize = next->t_stacksize;
- oti->oti_cswcnt = next->t_ctx_sw_cnt;
- oti->oti_runtime = next->t_run_time;
- oti->oti_last_checkin = next->t_sanity_check.sc_checkin_last;
- oti->oti_next_checkin = next->t_sanity_check.sc_checkin_last +
- next->t_sanity_check.sc_checkin_itvl;
- strncpy(oti->oti_name, next->t_name, sizeof(oti->oti_name));
-
- return (next);
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/os_time.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_time.c b/libs/os/src/os_time.c
deleted file mode 100644
index 783dc14..0000000
--- a/libs/os/src/os_time.c
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 "os/queue.h"
-
-os_time_t g_os_time = 0;
-
-os_time_t
-os_time_get(void)
-{
- return (g_os_time);
-}
-
-/**
- * Called for every single tick by the architecture specific functions.
- *
- * Increases the os_time by 1 tick.
- */
-void
-os_time_tick(void)
-{
- os_sr_t sr;
-
- OS_ENTER_CRITICAL(sr);
- ++g_os_time;
- OS_EXIT_CRITICAL(sr);
-}
-
-/**
- * Puts the current task to sleep for the specified number of os ticks. There
- * is no delay if ticks is <= 0.
- *
- * @param osticks Number of ticks to delay (<= 0 means no delay).
- */
-void
-os_time_delay(int32_t osticks)
-{
- os_sr_t sr;
-
- if (osticks > 0) {
- OS_ENTER_CRITICAL(sr);
- os_sched_sleep(os_sched_get_current_task(), (os_time_t)osticks);
- OS_EXIT_CRITICAL(sr);
- os_sched(NULL, 0);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c b/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c
deleted file mode 100644
index 35134f7..0000000
--- a/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 "testutil/testutil.h"
-#include "os_test_priv.h"
-
-void
-os_test_restart(void)
-{
- tu_restart();
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/test/arch/sim/os_test_arch_sim.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/arch/sim/os_test_arch_sim.c b/libs/os/src/test/arch/sim/os_test_arch_sim.c
deleted file mode 100644
index 3b6cfbf..0000000
--- a/libs/os/src/test/arch/sim/os_test_arch_sim.c
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 <stdio.h>
-#include <setjmp.h>
-#include <signal.h>
-#include <string.h>
-#include <sys/time.h>
-#include "testutil/testutil.h"
-#include "os/os.h"
-#include "os_test_priv.h"
-
-void
-os_test_restart(void)
-{
- struct sigaction sa;
- struct itimerval it;
- int rc;
-
- g_os_started = 0;
-
- memset(&sa, 0, sizeof sa);
- sa.sa_handler = SIG_IGN;
-
- sigaction(SIGALRM, &sa, NULL);
- sigaction(SIGVTALRM, &sa, NULL);
-
- memset(&it, 0, sizeof(it));
- rc = setitimer(ITIMER_VIRTUAL, &it, NULL);
- if (rc != 0) {
- perror("Cannot set itimer");
- abort();
- }
-
- tu_restart();
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-blinky/blob/8878610e/libs/os/src/test/mbuf_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/mbuf_test.c b/libs/os/src/test/mbuf_test.c
deleted file mode 100644
index 0d22229..0000000
--- a/libs/os/src/test/mbuf_test.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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 "testutil/testutil.h"
-#include "os/os.h"
-#include "os_test_priv.h"
-
-#include <string.h>
-
-#define MBUF_TEST_POOL_BUF_SIZE (256)
-#define MBUF_TEST_POOL_BUF_COUNT (10)
-
-os_membuf_t os_mbuf_membuf[OS_MEMPOOL_SIZE(MBUF_TEST_POOL_BUF_SIZE,
- MBUF_TEST_POOL_BUF_COUNT)];
-
-struct os_mbuf_pool os_mbuf_pool;
-struct os_mempool os_mbuf_mempool;
-
-static void
-os_mbuf_test_setup(void)
-{
- int rc;
-
- rc = os_mempool_init(&os_mbuf_mempool, MBUF_TEST_POOL_BUF_COUNT,
- MBUF_TEST_POOL_BUF_SIZE, &os_mbuf_membuf[0], "mbuf_pool");
- TEST_ASSERT_FATAL(rc == 0, "Error creating memory pool %d", rc);
-
- rc = os_mbuf_pool_init(&os_mbuf_pool, &os_mbuf_mempool,
- MBUF_TEST_POOL_BUF_SIZE, MBUF_TEST_POOL_BUF_COUNT);
- TEST_ASSERT_FATAL(rc == 0, "Error creating mbuf pool %d", rc);
-}
-
-
-
-TEST_CASE(os_mbuf_test_case_1)
-{
- struct os_mbuf *m;
- int rc;
-
- os_mbuf_test_setup();
-
- m = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(m != NULL, "Error allocating mbuf");
-
- rc = os_mbuf_free(m);
- TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf %d", rc);
-}
-
-TEST_CASE(os_mbuf_test_case_2)
-{
- struct os_mbuf *m;
- struct os_mbuf *m2;
- struct os_mbuf *dup;
- int rc;
-
- os_mbuf_test_setup();
-
- /* Test first allocating and duplicating a single mbuf */
- m = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(m != NULL, "Error allocating mbuf");
-
- dup = os_mbuf_dup(m);
- TEST_ASSERT_FATAL(dup != NULL, "NULL mbuf returned from dup");
- TEST_ASSERT_FATAL(dup != m, "duplicate matches original.");
-
- rc = os_mbuf_free(m);
- TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf m %d", rc);
-
- rc = os_mbuf_free(dup);
- TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf dup %d", rc);
-
- m = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(m != NULL, "Error allocating mbuf");
-
- m2 = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(m2 != NULL, "Error allocating mbuf");
-
- SLIST_NEXT(m, om_next) = m2;
-
- dup = os_mbuf_dup(m);
- TEST_ASSERT_FATAL(dup != NULL, "NULL mbuf returned from dup");
- TEST_ASSERT_FATAL(dup != m, "Duplicate matches original");
- TEST_ASSERT_FATAL(SLIST_NEXT(dup, om_next) != NULL,
- "NULL chained element, duplicate should match original");
-
- rc = os_mbuf_free_chain(m);
- TEST_ASSERT_FATAL(rc == 0, "Cannot free mbuf chain %d", rc);
-
- rc = os_mbuf_free_chain(dup);
- TEST_ASSERT_FATAL(rc == 0, "Cannot free mbuf chain %d", rc);
-}
-
-TEST_CASE(os_mbuf_test_case_3)
-{
- struct os_mbuf *m;
- int rc;
- uint8_t databuf[] = {0xa, 0xb, 0xc, 0xd};
- uint8_t cmpbuf[] = {0xff, 0xff, 0xff, 0xff};
-
- os_mbuf_test_setup();
-
- m = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(m != NULL, "Error allocating mbuf");
-
- rc = os_mbuf_append(m, databuf, sizeof(databuf));
- TEST_ASSERT_FATAL(rc == 0, "Cannot add %d bytes to mbuf",
- sizeof(databuf));
- TEST_ASSERT_FATAL(m->om_len == sizeof(databuf),
- "Length doesn't match size appended %d vs %d", m->om_len,
- sizeof(databuf));
-
- memcpy(cmpbuf, OS_MBUF_DATA(m, uint8_t *), m->om_len);
- TEST_ASSERT_FATAL(memcmp(cmpbuf, databuf, sizeof(databuf)) == 0,
- "Databuf doesn't match cmpbuf");
-}
-
-static void
-os_mbuf_test_misc_assert_contiguous(struct os_mbuf *om, void *data, int len)
-{
- TEST_ASSERT_FATAL(om != NULL);
-
- if (OS_MBUF_IS_PKTHDR(om)) {
- TEST_ASSERT(OS_MBUF_PKTLEN(om) == len);
- }
- TEST_ASSERT(om->om_len == len);
- TEST_ASSERT(memcmp(om->om_data, data, len) == 0);
-}
-
-TEST_CASE(os_mbuf_test_pullup)
-{
- struct os_mbuf *om;
- struct os_mbuf *om2;
- uint8_t data[256];
- int rc;
- int i;
-
- os_mbuf_test_setup();
-
- for (i = 0; i < sizeof data; i++) {
- data[i] = i;
- }
-
- /*** Free when too much data is requested. */
- om = os_mbuf_get_pkthdr(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(om != NULL);
-
- om = os_mbuf_pullup(om, 1);
- TEST_ASSERT(om == NULL);
-
- /*** No effect when all data is already at the start. */
- om = os_mbuf_get_pkthdr(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(om != NULL);
-
- rc = os_mbuf_append(om, data, 1);
- TEST_ASSERT_FATAL(rc == 0);
- os_mbuf_test_misc_assert_contiguous(om, data, 1);
-
- om = os_mbuf_pullup(om, 1);
- os_mbuf_test_misc_assert_contiguous(om, data, 1);
-
- /*** Spread data across four mbufs. */
- om2 = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(om2 != NULL);
- rc = os_mbuf_append(om2, data + 1, 1);
- TEST_ASSERT_FATAL(rc == 0);
- os_mbuf_concat(om, om2);
-
- om2 = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(om2 != NULL);
- rc = os_mbuf_append(om2, data + 2, 1);
- TEST_ASSERT_FATAL(rc == 0);
- os_mbuf_concat(om, om2);
-
- om2 = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(om2 != NULL);
- rc = os_mbuf_append(om2, data + 3, 1);
- TEST_ASSERT_FATAL(rc == 0);
- os_mbuf_concat(om, om2);
-
- TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(om) == 4);
-
- om = os_mbuf_pullup(om, 4);
- os_mbuf_test_misc_assert_contiguous(om, data, 4);
-
- os_mbuf_free_chain(om);
-
- /*** Require an allocation. */
- om = os_mbuf_get_pkthdr(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(om != NULL);
-
- om->om_data += 100;
- rc = os_mbuf_append(om, data, 100);
- TEST_ASSERT_FATAL(rc == 0);
-
- om2 = os_mbuf_get(&os_mbuf_pool, 0);
- TEST_ASSERT_FATAL(om2 != NULL);
- rc = os_mbuf_append(om2, data + 100, 100);
- TEST_ASSERT_FATAL(rc == 0);
- os_mbuf_concat(om, om2);
-
- om = os_mbuf_pullup(om, 200);
- os_mbuf_test_misc_assert_contiguous(om, data, 200);
-}
-
-TEST_SUITE(os_mbuf_test_suite)
-{
- os_mbuf_test_case_1();
- os_mbuf_test_case_2();
- os_mbuf_test_case_3();
- os_mbuf_test_pullup();
-}