diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-12-13 17:58:39 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-12-13 17:58:39 +0100 |
commit | 63462a4dddd238e439360e2e70ecaa9366066fc5 (patch) | |
tree | 483dec5d9857707ce731eba5f6fdd51dcc1cc367 /src/util/time.c | |
parent | bdb3420bc937f0ab9187695a56f0e63d7c227f00 (diff) | |
download | gnunet-63462a4dddd238e439360e2e70ecaa9366066fc5.tar.gz gnunet-63462a4dddd238e439360e2e70ecaa9366066fc5.zip |
make sure mono time uses atomics
Diffstat (limited to 'src/util/time.c')
-rw-r--r-- | src/util/time.c | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/src/util/time.c b/src/util/time.c index 46d3a2b65..741ca1ad2 100644 --- a/src/util/time.c +++ b/src/util/time.c | |||
@@ -23,6 +23,14 @@ | |||
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_util_lib.h" | 25 | #include "gnunet_util_lib.h" |
26 | #if __STDC_NO_ATOMICS__ | ||
27 | #else | ||
28 | #ifdef HAVE_STDATOMIC_H | ||
29 | #include <stdatomic.h> | ||
30 | #else | ||
31 | #define __STDC_NO_ATOMICS__ 1 | ||
32 | #endif | ||
33 | #endif | ||
26 | 34 | ||
27 | #define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__) | 35 | #define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__) |
28 | 36 | ||
@@ -780,7 +788,7 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf | |||
780 | static const struct GNUNET_CONFIGURATION_Handle *last_cfg; | 788 | static const struct GNUNET_CONFIGURATION_Handle *last_cfg; |
781 | static struct GNUNET_TIME_Absolute last_time; | 789 | static struct GNUNET_TIME_Absolute last_time; |
782 | static struct GNUNET_DISK_MapHandle *map_handle; | 790 | static struct GNUNET_DISK_MapHandle *map_handle; |
783 | static struct GNUNET_TIME_AbsoluteNBO *map; | 791 | static uint64_t *map; |
784 | struct GNUNET_TIME_Absolute now; | 792 | struct GNUNET_TIME_Absolute now; |
785 | 793 | ||
786 | now = GNUNET_TIME_absolute_get (); | 794 | now = GNUNET_TIME_absolute_get (); |
@@ -859,13 +867,38 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf | |||
859 | } | 867 | } |
860 | } | 868 | } |
861 | if (NULL != map) | 869 | if (NULL != map) |
862 | last_time = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (*map), | 870 | { |
871 | struct GNUNET_TIME_AbsoluteNBO mt; | ||
872 | |||
873 | #if __STDC_NO_ATOMICS__ | ||
874 | #if __GNUC__ | ||
875 | mt.abs_value_us__ = __sync_fetch_and_or (map, 0); | ||
876 | #else | ||
877 | mt.abs_value_us__ = *map; /* godspeed, pray this is atomic */ | ||
878 | #endif | ||
879 | #else | ||
880 | mt.abs_value_us__ = atomic_load (map); | ||
881 | #endif | ||
882 | last_time = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), | ||
863 | last_time); | 883 | last_time); |
884 | } | ||
864 | if (now.abs_value_us <= last_time.abs_value_us) | 885 | if (now.abs_value_us <= last_time.abs_value_us) |
865 | now.abs_value_us = last_time.abs_value_us+1; | 886 | now.abs_value_us = last_time.abs_value_us+1; |
866 | last_time = now; | 887 | last_time = now; |
867 | if (NULL != map) | 888 | if (NULL != map) |
868 | *map = GNUNET_TIME_absolute_hton (now); | 889 | { |
890 | uint64_t val = GNUNET_TIME_absolute_hton (now).abs_value_us__; | ||
891 | #if __STDC_NO_ATOMICS__ | ||
892 | #if __GNUC__ | ||
893 | (void) __sync_lock_test_and_set (map, val); | ||
894 | #else | ||
895 | *map = val; /* godspeed, pray this is atomic */ | ||
896 | #endif | ||
897 | #else | ||
898 | atomic_store (map, | ||
899 | val); | ||
900 | #endif | ||
901 | } | ||
869 | return now; | 902 | return now; |
870 | } | 903 | } |
871 | 904 | ||