takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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