Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers critical-posix.c Source File

critical-posix.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2015-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #if defined(TARGET_LIKE_POSIX) && !defined(ATOMIC_QUEUE_USE_PAL)
00020 
00021 // It's probably a better idea to define _POSIX_SOURCE in the target description
00022 #ifndef _XOPEN_SOURCE
00023 #define _XOPEN_SOURCE 600
00024 #endif
00025 
00026 #include <stdint.h>
00027 #include <assert.h>
00028 #include <unistd.h>
00029 #include <signal.h>
00030 #include <pthread.h>
00031 
00032 // Module include
00033 #include "aq_critical.h"
00034 
00035 static pthread_mutex_t *get_mutex()
00036 {
00037     static int initialized = 0;
00038     static pthread_mutex_t Mutex;
00039     if (!initialized) {
00040         pthread_mutexattr_t Attr;
00041         pthread_mutexattr_init(&Attr);
00042         pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
00043         pthread_mutex_init(&Mutex, &Attr);
00044         initialized = 1;
00045     }
00046     return &Mutex;
00047 }
00048 
00049 static volatile unsigned irq_nesting_depth = 0;
00050 static sigset_t old_sig_set;
00051 
00052 void aq_critical_section_enter()
00053 {
00054     pthread_mutex_lock(get_mutex());
00055     if (++irq_nesting_depth == 1) {
00056         int rc;
00057         sigset_t full_set;
00058         rc = sigfillset(&full_set);
00059         assert(rc == 0);
00060         rc = sigprocmask(SIG_BLOCK, &full_set, &old_sig_set);
00061         assert(rc == 0);
00062     }
00063 }
00064 
00065 void aq_critical_section_exit(void)
00066 {
00067     assert(irq_nesting_depth > 0);
00068     if (--irq_nesting_depth == 0) {
00069         int rc = sigprocmask(SIG_SETMASK, &old_sig_set, NULL);
00070         assert(rc == 0);
00071     }
00072     pthread_mutex_unlock(get_mutex());
00073 }
00074 #endif // defined(TARGET_LIKE_POSIX)