[50fda24] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup abs32le
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #ifndef KERN_abs32le_ATOMIC_H_
|
---|
| 36 | #define KERN_abs32le_ATOMIC_H_
|
---|
| 37 |
|
---|
[d99c1d2] | 38 | #include <typedefs.h>
|
---|
[50fda24] | 39 | #include <arch/barrier.h>
|
---|
| 40 | #include <preemption.h>
|
---|
[09a0bd4a] | 41 | #include <verify.h>
|
---|
[50fda24] | 42 |
|
---|
[09a0bd4a] | 43 | ATOMIC static inline void atomic_inc(atomic_t *val)
|
---|
| 44 | REQUIRES(val->count < ATOMIC_COUNT_MAX)
|
---|
| 45 | {
|
---|
[50fda24] | 46 | /* On real hardware the increment has to be done
|
---|
| 47 | as an atomic action. */
|
---|
| 48 |
|
---|
| 49 | val->count++;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[09a0bd4a] | 52 | ATOMIC static inline void atomic_dec(atomic_t *val)
|
---|
| 53 | REQUIRES(val->count > ATOMIC_COUNT_MIN)
|
---|
| 54 | {
|
---|
[50fda24] | 55 | /* On real hardware the decrement has to be done
|
---|
| 56 | as an atomic action. */
|
---|
| 57 |
|
---|
[09a0bd4a] | 58 | val->count--;
|
---|
[50fda24] | 59 | }
|
---|
| 60 |
|
---|
[09a0bd4a] | 61 | ATOMIC static inline atomic_count_t atomic_postinc(atomic_t *val)
|
---|
| 62 | REQUIRES(val->count < ATOMIC_COUNT_MAX)
|
---|
[50fda24] | 63 | {
|
---|
| 64 | /* On real hardware both the storing of the previous
|
---|
| 65 | value and the increment have to be done as a single
|
---|
| 66 | atomic action. */
|
---|
| 67 |
|
---|
[b03a666] | 68 | atomic_count_t prev = val->count;
|
---|
[50fda24] | 69 |
|
---|
| 70 | val->count++;
|
---|
| 71 | return prev;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[09a0bd4a] | 74 | ATOMIC static inline atomic_count_t atomic_postdec(atomic_t *val)
|
---|
| 75 | REQUIRES(val->count > ATOMIC_COUNT_MIN)
|
---|
[50fda24] | 76 | {
|
---|
| 77 | /* On real hardware both the storing of the previous
|
---|
| 78 | value and the decrement have to be done as a single
|
---|
| 79 | atomic action. */
|
---|
| 80 |
|
---|
[b03a666] | 81 | atomic_count_t prev = val->count;
|
---|
[50fda24] | 82 |
|
---|
| 83 | val->count--;
|
---|
| 84 | return prev;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | #define atomic_preinc(val) (atomic_postinc(val) + 1)
|
---|
| 88 | #define atomic_predec(val) (atomic_postdec(val) - 1)
|
---|
| 89 |
|
---|
[09a0bd4a] | 90 | ATOMIC static inline atomic_count_t test_and_set(atomic_t *val)
|
---|
[fb52db8] | 91 | {
|
---|
[09a0bd4a] | 92 | /* On real hardware the retrieving of the original
|
---|
| 93 | value and storing 1 have to be done as a single
|
---|
| 94 | atomic action. */
|
---|
| 95 |
|
---|
[b03a666] | 96 | atomic_count_t prev = val->count;
|
---|
[fb52db8] | 97 | val->count = 1;
|
---|
| 98 | return prev;
|
---|
[50fda24] | 99 | }
|
---|
| 100 |
|
---|
[09a0bd4a] | 101 | ATOMIC static inline atomic_count_t arch_atomic_get(atomic_t *val)
|
---|
| 102 | {
|
---|
| 103 | /* This function is not needed on real hardware, it just
|
---|
| 104 | duplicates the functionality of atomic_get(). It is
|
---|
| 105 | defined here because atomic_get() is an inline function
|
---|
| 106 | declared in a header file which we are included in. */
|
---|
| 107 |
|
---|
| 108 | return val->count;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[50fda24] | 111 | static inline void atomic_lock_arch(atomic_t *val)
|
---|
| 112 | {
|
---|
[fb52db8] | 113 | do {
|
---|
[09a0bd4a] | 114 | while (arch_atomic_get(val));
|
---|
[fb52db8] | 115 | } while (test_and_set(val));
|
---|
[50fda24] | 116 | }
|
---|
| 117 |
|
---|
| 118 | #endif
|
---|
| 119 |
|
---|
| 120 | /** @}
|
---|
| 121 | */
|
---|