Cygwin: access: Fix X_OK behaviour for backup operators and admins
[newlib-cygwin.git] / newlib / libc / sys / or1k / mlock.c
bloba0c03833565184d3cdb459a04b9786208c945df8
1 /* malloc-lock.c. Lock malloc.
3 * Copyright (C) 2014, Authors
5 * Contributor Stefan Wallentowitz <stefan.wallentowitz@tum.de>
7 * The authors hereby grant permission to use, copy, modify, distribute,
8 * and license this software and its documentation for any purpose, provided
9 * that existing copyright notices are retained in all copies and that this
10 * notice is included verbatim in any distributions. No written agreement,
11 * license, or royalty fee is required for any of the authorized uses.
12 * Modifications to this software may be copyrighted by their authors
13 * and need not follow the licensing terms described here, provided that
14 * the new terms are clearly indicated on the first page of each file where
15 * they apply.
18 #include <reent.h>
19 #include <stdint.h>
21 /* Lock calls from different cores, but allows recursive calls from the same
22 * core. The lock is not only atomic to other cores calling malloc, but also
23 * disables all external interrupts. This is necessary as it could otherwise
24 * lead to a deadlock to interrupt while in malloc and then call it from an
25 * exception. But as we want the exceptions to be flexible to use all library
26 * calls and especially memory management this is necessary.
29 // The lock. It is zero when unlocked and contains a unique value for each core.
30 // This value is not the core id (to avoid id zero), but the pointer value of
31 // the core specific struct _reent.
32 volatile uint32_t _or1k_malloc_lock;
34 // Count how often the current holder has entered the lock
35 volatile uint32_t _or1k_malloc_lock_cnt;
36 // The exception enable restore of the current mutex holder
37 volatile uint32_t _or1k_malloc_lock_restore;
39 extern uint32_t or1k_sync_cas(void *address, uint32_t compare, uint32_t swap);
41 extern uint32_t or1k_critical_begin();
42 extern void or1k_critical_end(uint32_t restore);
44 /**
45 * Recursive lock of the malloc
47 void __malloc_lock(struct _reent *ptr) {
48 uint32_t restore;
49 uint32_t id;
51 // Each core is identified by its struct _reent pointer
52 id = (uint32_t) ptr;
54 // Disable timer and interrupt exception, save TEE and IEE flag
55 // temporarily to restore them later on unlock
56 restore = or1k_critical_begin();
58 // We cannot be disturbed by an interrupt or timer exception from here
60 // Check if we currently don't hold the lock
61 if (_or1k_malloc_lock != id) {
62 do {
63 // Repeatedly check the lock until it is set to zero
64 while (_or1k_malloc_lock != 0) {}
65 // .. and then try to set it atomically. As this may
66 // fail, we need to repeat this
67 } while (or1k_sync_cas((void*) &_or1k_malloc_lock, 0, id) != 0);
70 // Store the TEE and IEE flags for later restore
71 if (_or1k_malloc_lock_cnt == 0) {
72 _or1k_malloc_lock_restore = restore;
75 // Increment counter. The lock may be accessed recursively
76 _or1k_malloc_lock_cnt++;
78 return;
81 void __malloc_unlock(struct _reent *ptr) {
82 // Decrement counter. The lock may be unlocked recursively
83 _or1k_malloc_lock_cnt--;
85 // If this was the last recursive unlock call
86 if(_or1k_malloc_lock_cnt == 0){
87 // We need to temporarily store the value to avoid a race
88 // condition between unlocking and reading restore
89 uint32_t restore = _or1k_malloc_lock_restore;
90 // unset lock
91 _or1k_malloc_lock = 0;
92 // Restore flags
93 or1k_critical_end(restore);
96 return;