Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

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     static int initialized = 0;
00037     static pthread_mutex_t Mutex;
00038     if (!initialized)
00039     {
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     pthread_mutex_lock(get_mutex());
00054     if (++irq_nesting_depth == 1) {
00055         int rc;
00056         sigset_t full_set;
00057         rc = sigfillset(&full_set);
00058         assert(rc == 0);
00059         rc = sigprocmask(SIG_BLOCK, &full_set, &old_sig_set);
00060         assert(rc == 0);
00061     }
00062 }
00063 
00064 void aq_critical_section_exit(void) {
00065     assert(irq_nesting_depth > 0);
00066     if (--irq_nesting_depth == 0) {
00067         int rc = sigprocmask(SIG_SETMASK, &old_sig_set, NULL);
00068         assert(rc == 0);
00069     }
00070     pthread_mutex_unlock(get_mutex());
00071 }
00072 #endif // defined(TARGET_LIKE_POSIX)