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