Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maygup01 0:11cc2b7889af 1 // ----------------------------------------------------------------------------
maygup01 0:11cc2b7889af 2 // Copyright 2015-2017 ARM Ltd.
maygup01 0:11cc2b7889af 3 //
maygup01 0:11cc2b7889af 4 // SPDX-License-Identifier: Apache-2.0
maygup01 0:11cc2b7889af 5 //
maygup01 0:11cc2b7889af 6 // Licensed under the Apache License, Version 2.0 (the "License");
maygup01 0:11cc2b7889af 7 // you may not use this file except in compliance with the License.
maygup01 0:11cc2b7889af 8 // You may obtain a copy of the License at
maygup01 0:11cc2b7889af 9 //
maygup01 0:11cc2b7889af 10 // http://www.apache.org/licenses/LICENSE-2.0
maygup01 0:11cc2b7889af 11 //
maygup01 0:11cc2b7889af 12 // Unless required by applicable law or agreed to in writing, software
maygup01 0:11cc2b7889af 13 // distributed under the License is distributed on an "AS IS" BASIS,
maygup01 0:11cc2b7889af 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maygup01 0:11cc2b7889af 15 // See the License for the specific language governing permissions and
maygup01 0:11cc2b7889af 16 // limitations under the License.
maygup01 0:11cc2b7889af 17 // ----------------------------------------------------------------------------
maygup01 0:11cc2b7889af 18
maygup01 0:11cc2b7889af 19 #if defined(TARGET_LIKE_POSIX) && !defined(ATOMIC_QUEUE_USE_PAL)
maygup01 0:11cc2b7889af 20
maygup01 0:11cc2b7889af 21 // It's probably a better idea to define _POSIX_SOURCE in the target description
maygup01 0:11cc2b7889af 22 #ifndef _XOPEN_SOURCE
maygup01 0:11cc2b7889af 23 #define _XOPEN_SOURCE 600
maygup01 0:11cc2b7889af 24 #endif
maygup01 0:11cc2b7889af 25
maygup01 0:11cc2b7889af 26 #include <stdint.h>
maygup01 0:11cc2b7889af 27 #include <assert.h>
maygup01 0:11cc2b7889af 28 #include <unistd.h>
maygup01 0:11cc2b7889af 29 #include <signal.h>
maygup01 0:11cc2b7889af 30 #include <pthread.h>
maygup01 0:11cc2b7889af 31
maygup01 0:11cc2b7889af 32 // Module include
maygup01 0:11cc2b7889af 33 #include "aq_critical.h"
maygup01 0:11cc2b7889af 34
maygup01 0:11cc2b7889af 35 static pthread_mutex_t *get_mutex()
maygup01 0:11cc2b7889af 36 {
maygup01 0:11cc2b7889af 37 static int initialized = 0;
maygup01 0:11cc2b7889af 38 static pthread_mutex_t Mutex;
maygup01 0:11cc2b7889af 39 if (!initialized) {
maygup01 0:11cc2b7889af 40 pthread_mutexattr_t Attr;
maygup01 0:11cc2b7889af 41 pthread_mutexattr_init(&Attr);
maygup01 0:11cc2b7889af 42 pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
maygup01 0:11cc2b7889af 43 pthread_mutex_init(&Mutex, &Attr);
maygup01 0:11cc2b7889af 44 initialized = 1;
maygup01 0:11cc2b7889af 45 }
maygup01 0:11cc2b7889af 46 return &Mutex;
maygup01 0:11cc2b7889af 47 }
maygup01 0:11cc2b7889af 48
maygup01 0:11cc2b7889af 49 static volatile unsigned irq_nesting_depth = 0;
maygup01 0:11cc2b7889af 50 static sigset_t old_sig_set;
maygup01 0:11cc2b7889af 51
maygup01 0:11cc2b7889af 52 void aq_critical_section_enter()
maygup01 0:11cc2b7889af 53 {
maygup01 0:11cc2b7889af 54 pthread_mutex_lock(get_mutex());
maygup01 0:11cc2b7889af 55 if (++irq_nesting_depth == 1) {
maygup01 0:11cc2b7889af 56 int rc;
maygup01 0:11cc2b7889af 57 sigset_t full_set;
maygup01 0:11cc2b7889af 58 rc = sigfillset(&full_set);
maygup01 0:11cc2b7889af 59 assert(rc == 0);
maygup01 0:11cc2b7889af 60 rc = sigprocmask(SIG_BLOCK, &full_set, &old_sig_set);
maygup01 0:11cc2b7889af 61 assert(rc == 0);
maygup01 0:11cc2b7889af 62 }
maygup01 0:11cc2b7889af 63 }
maygup01 0:11cc2b7889af 64
maygup01 0:11cc2b7889af 65 void aq_critical_section_exit(void)
maygup01 0:11cc2b7889af 66 {
maygup01 0:11cc2b7889af 67 assert(irq_nesting_depth > 0);
maygup01 0:11cc2b7889af 68 if (--irq_nesting_depth == 0) {
maygup01 0:11cc2b7889af 69 int rc = sigprocmask(SIG_SETMASK, &old_sig_set, NULL);
maygup01 0:11cc2b7889af 70 assert(rc == 0);
maygup01 0:11cc2b7889af 71 }
maygup01 0:11cc2b7889af 72 pthread_mutex_unlock(get_mutex());
maygup01 0:11cc2b7889af 73 }
maygup01 0:11cc2b7889af 74 #endif // defined(TARGET_LIKE_POSIX)