summaryrefslogtreecommitdiff
path: root/compiler.h
blob: f004138457b8ca4d76de4d9842a0ab603e636430 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * compiler.h - compiler specific macros
 *
 * This software is distributed under a BSD-style license. See the
 * file "COPYING" in the toop-level directory of the distribution for details.
 */
#ifndef _GPSD_COMPILER_H_
#define _GPSD_COMPILER_H_

/*
 * Tell GCC that we want thread-safe behavior with _REENTRANT;
 * in particular, errno must be thread-local.
 * Tell POSIX-conforming implementations with _POSIX_THREAD_SAFE_FUNCTIONS.
 * See http://www.unix.org/whitepapers/reentrant.html
 */
#ifndef _REENTRANT
#define _REENTRANT
#endif
#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
#define _POSIX_THREAD_SAFE_FUNCTIONS
#endif

#include "gpsd_config.h"	/* is HAVE_STDATOMIC defined? */

/* Macro for declaring function with printf-like arguments. */
# if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
#define PRINTF_FUNC(format_index, arg_index) \
    __attribute__((__format__(__printf__, format_index, arg_index)))
# else
#define PRINTF_FUNC(format_index, arg_indx)
#endif

/* Macro for declaring function arguments unused. */
#if defined(__GNUC__) || defined(__clang__)
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif

/* Needed because 4.x versions of GCC are really annoying */
#define ignore_return(funcall) \
    do { \
        ssize_t locresult = (funcall); \
        assert(locresult != -23); \
    } while (0)

#ifndef S_SPLINT_S
#ifdef HAVE_STDATOMIC_H
#ifndef __COVERITY__	/* Coverity is confused by a GNU typedef */
#include <stdatomic.h>
#endif /* __COVERITY__ */
#endif /* HAVE_STDATOMIC_H */
#endif /* S_SPLINT_S */

#ifdef HAVE_OSATOMIC_H
#include <libkern/OSAtomic.h>
#endif /* HAVE_STDATOMIC_H */

static /*@unused@*/ inline void memory_barrier(void)
/* prevent instruction reordering across any call to this function */
{
#ifndef S_SPLINT_S
#ifdef STD_ATOMIC_H
#ifndef __COVERITY__
    atomic_thread_fence(memory_order_seq_cst);
#endif /* __COVERITY__ */
#elif defined(HAVE_OSATOMIC_H)
    OSMemoryBarrier();
#elif defined(__GNUC__)
    asm volatile ("" : : : "memory");
#endif /* STD_ATOMIC_H */
#endif /* S_SPLINT_S */
}

#endif /* _GPSD_COMPILER_H_ */