Skip to content

Commit 20a02b4

Browse files
committed
Fix build on CentOS 7 / x86_64.
The issue is that the default compiler on CentOS 7 (and presumably RHEL 7) is an old GCC that's too old to support atomic types. In this case we attempt to approximate an atomic 64-bit type with a normal 64-bit integer, and warn at runtime if the system doesn't support lock-free operations with this data type. This has been compile-tested and lightly run-tested on CentOS 7 x86_64. It might not work well with ancient non-GCC compilers or 32-bit hosts.
1 parent a326ec8 commit 20a02b4

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/iperf.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@
7777
#include <pthread.h>
7878
#endif // HAVE_PTHREAD
7979

80+
/*
81+
* Atomic types highly desired, but if not, we approximate what we need
82+
* with normal integers and warn.
83+
*/
8084
#ifdef HAVE_STDATOMIC_H
8185
#include <stdatomic.h>
86+
#else
87+
#warning "No <stdatomic.h> available."
88+
typedef uint64_t atomic_uint_fast64_t;
8289
#endif // HAVE_STDATOMIC_H
8390

8491
#if !defined(__IPERF_API_H)
85-
//typedef uint64_t iperf_size_t;
8692
typedef uint_fast64_t iperf_size_t;
8793
typedef atomic_uint_fast64_t atomic_iperf_size_t;
8894
#endif // __IPERF_API_H

src/iperf_api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@
3838
extern "C" { /* open extern "C" */
3939
#endif
4040

41+
/*
42+
* Atomic types highly desired, but if not, we approximate what we need
43+
* with normal integers and warn.
44+
*/
4145
#ifdef HAVE_STDATOMIC_H
4246
#include <stdatomic.h>
47+
#else
48+
#warning "No <stdatomic.h> available"
49+
typedef u_int64_t atomic_uint_fast64_t;
4350
#endif // HAVE_STDATOMIC_H
4451

4552
struct iperf_test;

src/main.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* iperf, Copyright (c) 2014-2022, The Regents of the University of
2+
* iperf, Copyright (c) 2014-2023, The Regents of the University of
33
* California, through Lawrence Berkeley National Laboratory (subject
44
* to receipt of any required approvals from the U.S. Dept. of
55
* Energy). All rights reserved.
@@ -59,6 +59,24 @@ main(int argc, char **argv)
5959
{
6060
struct iperf_test *test;
6161

62+
/*
63+
* Atomics check. We prefer to have atomic types (which is
64+
* basically on any compiler supporting C11 or better). If we
65+
* don't have them, we try to approximate the type we need with a
66+
* regular integer, but complain if they're not lock-free. We only
67+
* know how to check this on GCC. GCC on CentOS 7 / RHEL 7 is the
68+
* targeted use case for these check.
69+
*/
70+
#ifndef HAVE_STDATOMIC_H
71+
#ifdef __GNUC__
72+
if (! __atomic_always_lock_free (sizeof (u_int64_t), 0)) {
73+
#endif // __GNUC__
74+
fprintf(stderr, "Warning: Cannot guarantee lock-free operation with 64-bit data types\n");
75+
#ifdef __GNUC__
76+
}
77+
#endif // __GNUC__
78+
#endif // HAVE_STDATOMIC_H
79+
6280
// XXX: Setting the process affinity requires root on most systems.
6381
// Is this a feature we really need?
6482
#ifdef TEST_PROC_AFFINITY

0 commit comments

Comments
 (0)