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