Example

Dependencies:   FXAS21002 FXOS8700Q

simple-mbed-cloud-client/mbed-cloud-client/update-client-hub/modules/atomic-queue/source/critical-posix.c

Committer:
maygup01
Date:
2019-11-19
Revision:
0:11cc2b7889af

File content as of revision 0:11cc2b7889af:

// ----------------------------------------------------------------------------
// Copyright 2015-2017 ARM Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------

#if defined(TARGET_LIKE_POSIX) && !defined(ATOMIC_QUEUE_USE_PAL)

// It's probably a better idea to define _POSIX_SOURCE in the target description
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif

#include <stdint.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

// Module include
#include "aq_critical.h"

static pthread_mutex_t *get_mutex()
{
    static int initialized = 0;
    static pthread_mutex_t Mutex;
    if (!initialized) {
        pthread_mutexattr_t Attr;
        pthread_mutexattr_init(&Attr);
        pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init(&Mutex, &Attr);
        initialized = 1;
    }
    return &Mutex;
}

static volatile unsigned irq_nesting_depth = 0;
static sigset_t old_sig_set;

void aq_critical_section_enter()
{
    pthread_mutex_lock(get_mutex());
    if (++irq_nesting_depth == 1) {
        int rc;
        sigset_t full_set;
        rc = sigfillset(&full_set);
        assert(rc == 0);
        rc = sigprocmask(SIG_BLOCK, &full_set, &old_sig_set);
        assert(rc == 0);
    }
}

void aq_critical_section_exit(void)
{
    assert(irq_nesting_depth > 0);
    if (--irq_nesting_depth == 0) {
        int rc = sigprocmask(SIG_SETMASK, &old_sig_set, NULL);
        assert(rc == 0);
    }
    pthread_mutex_unlock(get_mutex());
}
#endif // defined(TARGET_LIKE_POSIX)