summaryrefslogtreecommitdiff
path: root/lib/simple-atomic.h
blob: 9180ea6a66349da97e25c8893a909e7b110287ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Simple atomic operations for multithreading.
   Copyright (C) 2021-2023 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2021.  */

#ifndef _SIMPLE_ATOMIC_H
#define _SIMPLE_ATOMIC_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Guarantees that memory stores that are in code before this call
   are finished before this call, and that memory loads that are in code
   after this call are started after this call.  */
extern void memory_barrier (void);

/* Stores NEWVAL in *VP if the old value *VP is == CMP.
   Returns the old value.  */
extern unsigned int atomic_compare_and_swap (unsigned int volatile *vp,
                                             unsigned int cmp,
                                             unsigned int newval);

/* Stores NEWVAL in *VP if the old value *VP is == CMP.
   Returns the old value.  */
extern uintptr_t atomic_compare_and_swap_ptr (uintptr_t volatile *vp,
                                              uintptr_t cmp,
                                              uintptr_t newval);

#ifdef __cplusplus
}
#endif

#endif /* _SIMPLE_ATOMIC_H */