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