123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /*
- * Copyright (c) 2018 Intel Corporation
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include <kernel.h>
- #include <string.h>
- #include "wrapper.h"
- K_MEM_SLAB_DEFINE(cv2_mutex_slab, sizeof(struct cv2_mutex),
- CONFIG_CMSIS_V2_MUTEX_MAX_COUNT, 4);
- static const osMutexAttr_t init_mutex_attrs = {
- .name = "ZephyrMutex",
- .attr_bits = osMutexPrioInherit,
- .cb_mem = NULL,
- .cb_size = 0,
- };
- /**
- * @brief Create and Initialize a Mutex object.
- */
- osMutexId_t osMutexNew(const osMutexAttr_t *attr)
- {
- struct cv2_mutex *mutex;
- if (k_is_in_isr()) {
- return NULL;
- }
- if (attr == NULL) {
- attr = &init_mutex_attrs;
- }
- __ASSERT(attr->attr_bits & osMutexPrioInherit,
- "Zephyr supports osMutexPrioInherit by default. Do not unselect it\n");
- __ASSERT(!(attr->attr_bits & osMutexRobust),
- "Zephyr does not support osMutexRobust.\n");
- if (k_mem_slab_alloc(&cv2_mutex_slab, (void **)&mutex, K_MSEC(100)) == 0) {
- memset(mutex, 0, sizeof(struct cv2_mutex));
- } else {
- return NULL;
- }
- k_mutex_init(&mutex->z_mutex);
- mutex->state = attr->attr_bits;
- if (attr->name == NULL) {
- strncpy(mutex->name, init_mutex_attrs.name,
- sizeof(mutex->name) - 1);
- } else {
- strncpy(mutex->name, attr->name, sizeof(mutex->name) - 1);
- }
- return (osMutexId_t)mutex;
- }
- /**
- * @brief Wait until a Mutex becomes available.
- */
- osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
- {
- struct cv2_mutex *mutex = (struct cv2_mutex *) mutex_id;
- int status;
- if (mutex_id == NULL) {
- return osErrorParameter;
- }
- if (k_is_in_isr()) {
- return osErrorISR;
- }
- if (mutex->z_mutex.lock_count == 0U ||
- mutex->z_mutex.owner == _current) {
- }
- /* Throw an error if the mutex is not configured to be recursive and
- * the current thread is trying to acquire the mutex again.
- */
- if ((mutex->state & osMutexRecursive) == 0U) {
- if ((mutex->z_mutex.owner == _current) &&
- (mutex->z_mutex.lock_count != 0U)) {
- return osErrorResource;
- }
- }
- if (timeout == osWaitForever) {
- status = k_mutex_lock(&mutex->z_mutex, K_FOREVER);
- } else if (timeout == 0U) {
- status = k_mutex_lock(&mutex->z_mutex, K_NO_WAIT);
- } else {
- status = k_mutex_lock(&mutex->z_mutex,
- K_TICKS(timeout));
- }
- if (status == -EBUSY) {
- return osErrorResource;
- } else if (status == -EAGAIN) {
- return osErrorTimeout;
- } else {
- return osOK;
- }
- }
- /**
- * @brief Release a Mutex that was obtained by osMutexWait.
- */
- osStatus_t osMutexRelease(osMutexId_t mutex_id)
- {
- struct cv2_mutex *mutex = (struct cv2_mutex *) mutex_id;
- if (mutex_id == NULL) {
- return osErrorParameter;
- }
- if (k_is_in_isr()) {
- return osErrorISR;
- }
- /* Mutex was not obtained before or was not owned by current thread */
- if ((mutex->z_mutex.lock_count == 0U) ||
- (mutex->z_mutex.owner != _current)) {
- return osErrorResource;
- }
- k_mutex_unlock(&mutex->z_mutex);
- return osOK;
- }
- /**
- * @brief Delete a Mutex that was created by osMutexCreate.
- */
- osStatus_t osMutexDelete(osMutexId_t mutex_id)
- {
- struct cv2_mutex *mutex = (struct cv2_mutex *)mutex_id;
- if (mutex_id == NULL) {
- return osErrorParameter;
- }
- if (k_is_in_isr()) {
- return osErrorISR;
- }
- /* The status code "osErrorResource" (mutex specified by parameter
- * mutex_id is in an invalid mutex state) is not supported in Zephyr.
- */
- k_mem_slab_free(&cv2_mutex_slab, (void *) &mutex);
- return osOK;
- }
- osThreadId_t osMutexGetOwner(osMutexId_t mutex_id)
- {
- struct cv2_mutex *mutex = (struct cv2_mutex *)mutex_id;
- if (k_is_in_isr() || (mutex == NULL)) {
- return NULL;
- }
- /* Mutex was not obtained before */
- if (mutex->z_mutex.lock_count == 0U) {
- return NULL;
- }
- return get_cmsis_thread_id(mutex->z_mutex.owner);
- }
- const char *osMutexGetName(osMutexId_t mutex_id)
- {
- struct cv2_mutex *mutex = (struct cv2_mutex *)mutex_id;
- if (k_is_in_isr() || (mutex == NULL)) {
- return NULL;
- }
- return mutex->name;
- }
|