Skip to content

Commit

Permalink
Fix build on CentOS 7 / x86_64.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bmah888 committed Nov 8, 2023
1 parent a326ec8 commit 20a02b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/iperf.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@
#include <pthread.h>
#endif // HAVE_PTHREAD

/*
* Atomic types highly desired, but if not, we approximate what we need
* with normal integers and warn.
*/
#ifdef HAVE_STDATOMIC_H
#include <stdatomic.h>
#else
#warning "No <stdatomic.h> available."
typedef uint64_t atomic_uint_fast64_t;
#endif // HAVE_STDATOMIC_H

#if !defined(__IPERF_API_H)
//typedef uint64_t iperf_size_t;
typedef uint_fast64_t iperf_size_t;
typedef atomic_uint_fast64_t atomic_iperf_size_t;
#endif // __IPERF_API_H
Expand Down
7 changes: 7 additions & 0 deletions src/iperf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@
extern "C" { /* open extern "C" */
#endif

/*
* Atomic types highly desired, but if not, we approximate what we need
* with normal integers and warn.
*/
#ifdef HAVE_STDATOMIC_H
#include <stdatomic.h>
#else
#warning "No <stdatomic.h> available"
typedef u_int64_t atomic_uint_fast64_t;
#endif // HAVE_STDATOMIC_H

struct iperf_test;
Expand Down
20 changes: 19 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* iperf, Copyright (c) 2014-2022, The Regents of the University of
* iperf, Copyright (c) 2014-2023, The Regents of the University of
* California, through Lawrence Berkeley National Laboratory (subject
* to receipt of any required approvals from the U.S. Dept. of
* Energy). All rights reserved.
Expand Down Expand Up @@ -59,6 +59,24 @@ main(int argc, char **argv)
{
struct iperf_test *test;

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

0 comments on commit 20a02b4

Please sign in to comment.