Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ns_event_loop_mutex.c Source File

ns_event_loop_mutex.c

00001 /*
00002  * Copyright (c) 2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "eventOS_scheduler.h"
00019 
00020 #include "ns_event_loop_mutex.h"
00021 
00022 #ifdef MBED_CONF_RTOS_PRESENT
00023 #include "mbed_assert.h"
00024 #include "cmsis.h"
00025 #include "cmsis_os2.h"
00026 #include "mbed_rtos_storage.h"
00027 #include "ns_trace.h"
00028 
00029 
00030 #define TRACE_GROUP "evlm"
00031 
00032 static mbed_rtos_storage_mutex_t event_mutex;
00033 static const osMutexAttr_t event_mutex_attr = {
00034     .name = "nanostack_event_mutex",
00035     .attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust,
00036     .cb_mem = &event_mutex,
00037     .cb_size = sizeof event_mutex,
00038 };
00039 static osMutexId_t event_mutex_id;
00040 static osThreadId_t event_mutex_owner_id = NULL;
00041 static uint32_t owner_count = 0;
00042 
00043 void eventOS_scheduler_mutex_wait(void)
00044 {
00045     osMutexAcquire(event_mutex_id, osWaitForever);
00046     if (0 == owner_count) {
00047         event_mutex_owner_id = osThreadGetId();
00048     }
00049     owner_count++;
00050 }
00051 
00052 void eventOS_scheduler_mutex_release(void)
00053 {
00054     owner_count--;
00055     if (0 == owner_count) {
00056         event_mutex_owner_id = NULL;
00057     }
00058     osMutexRelease(event_mutex_id);
00059 }
00060 
00061 uint8_t eventOS_scheduler_mutex_is_owner(void)
00062 {
00063     return osThreadGetId() == event_mutex_owner_id ? 1 : 0;
00064 }
00065 
00066 void ns_event_loop_mutex_init(void)
00067 {
00068     event_mutex_id = osMutexNew(&event_mutex_attr);
00069     MBED_ASSERT(event_mutex_id != NULL);
00070 }
00071 
00072 #else
00073 
00074 void eventOS_scheduler_mutex_wait(void)
00075 {
00076 }
00077 
00078 void eventOS_scheduler_mutex_release(void)
00079 {
00080 }
00081 
00082 uint8_t eventOS_scheduler_mutex_is_owner(void)
00083 {
00084     return 1;
00085 }
00086 
00087 void ns_event_loop_mutex_init(void)
00088 {
00089 }
00090 
00091 #endif // MBED_CONF_RTOS_PRESENT