From 30ce2d5984c5ef90681e0faed6d1a747b6d95999 Mon Sep 17 00:00:00 2001 From: Sarah Larsen Date: Tue, 17 Jan 2023 17:44:27 -0800 Subject: [PATCH] Add atomic_iperf_size_t for atomic (thread-safe) operations --- src/iperf.h | 25 ++++++++++++++----------- src/iperf_api.c | 2 +- src/iperf_api.h | 4 +++- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/iperf.h b/src/iperf.h index afdf1b6c4..87b7b0ed2 100644 --- a/src/iperf.h +++ b/src/iperf.h @@ -74,14 +74,17 @@ #endif // HAVE_SSL #include +#include #if !defined(__IPERF_API_H) -typedef uint64_t iperf_size_t; +//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 struct iperf_interval_results { - iperf_size_t bytes_transferred; /* bytes transferred in this interval */ + atomic_iperf_size_t bytes_transferred; /* bytes transferred in this interval */ struct iperf_time interval_start_time; struct iperf_time interval_end_time; float interval_duration; @@ -115,11 +118,11 @@ struct iperf_interval_results struct iperf_stream_result { - iperf_size_t bytes_received; - iperf_size_t bytes_sent; - iperf_size_t bytes_received_this_interval; - iperf_size_t bytes_sent_this_interval; - iperf_size_t bytes_sent_omit; + atomic_iperf_size_t bytes_received; + atomic_iperf_size_t bytes_sent; + atomic_iperf_size_t bytes_received_this_interval; + atomic_iperf_size_t bytes_sent_this_interval; + atomic_iperf_size_t bytes_sent_omit; long stream_prev_total_retrans; long stream_retrans; long stream_max_rtt; @@ -359,11 +362,11 @@ struct iperf_test int num_streams; /* total streams in the test (-P) */ - iperf_size_t bytes_sent; - iperf_size_t blocks_sent; + atomic_iperf_size_t bytes_sent; + atomic_iperf_size_t blocks_sent; - iperf_size_t bytes_received; - iperf_size_t blocks_received; + atomic_iperf_size_t bytes_received; + atomic_iperf_size_t blocks_received; iperf_size_t bitrate_limit_stats_count; /* Number of stats periods accumulated for server's total bitrate average */ iperf_size_t *bitrate_limit_intervals_traffic_bytes; /* Pointer to a cyclic array that includes the last interval's bytes transferred */ diff --git a/src/iperf_api.c b/src/iperf_api.c index 561a2cab6..eb234030c 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -1889,7 +1889,7 @@ iperf_check_total_rate(struct iperf_test *test, iperf_size_t last_interval_bytes return; /* Calculating total bytes traffic to be averaged */ - for (total_bytes = 0, i = 0; i < test->settings->bitrate_limit_stats_per_interval; i++) { + for (i = 0, total_bytes = 0; i < test->settings->bitrate_limit_stats_per_interval; i++) { total_bytes += test->bitrate_limit_intervals_traffic_bytes[i]; } diff --git a/src/iperf_api.h b/src/iperf_api.h index ed991e12b..1c2cb7ec3 100644 --- a/src/iperf_api.h +++ b/src/iperf_api.h @@ -38,6 +38,7 @@ extern "C" { /* open extern "C" */ #endif +#include struct iperf_test; struct iperf_stream_result; @@ -46,7 +47,8 @@ struct iperf_stream; struct iperf_time; #if !defined(__IPERF_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_H /* default settings */