From 4d39e590546d73c6e2f445311a8cfa4522ad52c2 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 1 Nov 2011 21:00:46 -0700 Subject: [PATCH] vm: add atomic::load and atomic::store functions Abstract out the fence-and-load and store-and-fence patterns --- vm/atomic-cl-32.hpp | 3 +++ vm/atomic-cl-64.hpp | 3 +++ vm/atomic-gcc.hpp | 3 +++ vm/errors.cpp | 8 ++++---- vm/os-windows.cpp | 5 ++--- vm/sampling_profiler.cpp | 7 +++---- vm/vm.cpp | 2 +- vm/vm.hpp | 4 ++-- 8 files changed, 21 insertions(+), 14 deletions(-) diff --git a/vm/atomic-cl-32.hpp b/vm/atomic-cl-32.hpp index a27516884d..bfc3cc366f 100644 --- a/vm/atomic-cl-32.hpp +++ b/vm/atomic-cl-32.hpp @@ -1,3 +1,5 @@ +#define FACTOR_FORCE_INLINE __forceinline + namespace factor { namespace atomic { __forceinline static bool cas(volatile cell *ptr, cell old_val, cell new_val) @@ -44,3 +46,4 @@ namespace factor { } } +#include "atomic.hpp" diff --git a/vm/atomic-cl-64.hpp b/vm/atomic-cl-64.hpp index 595bf5f30d..e0606840e8 100644 --- a/vm/atomic-cl-64.hpp +++ b/vm/atomic-cl-64.hpp @@ -1,3 +1,5 @@ +#define FACTOR_FORCE_INLINE __forceinline + namespace factor { namespace atomic { __forceinline static bool cas(volatile cell *ptr, cell old_val, cell new_val) @@ -44,3 +46,4 @@ namespace factor { } } +#include "atomic.hpp" diff --git a/vm/atomic-gcc.hpp b/vm/atomic-gcc.hpp index d6fd5a2b77..9b027e201f 100644 --- a/vm/atomic-gcc.hpp +++ b/vm/atomic-gcc.hpp @@ -1,3 +1,4 @@ +#define FACTOR_FORCE_INLINE __attribute__((always_inline)) inline namespace factor { namespace atomic { __attribute__((always_inline)) @@ -40,3 +41,5 @@ namespace factor { } } } + +#include "atomic.hpp" diff --git a/vm/errors.cpp b/vm/errors.cpp index dcc3a54eb2..5e27ffd24c 100755 --- a/vm/errors.cpp +++ b/vm/errors.cpp @@ -172,20 +172,20 @@ void factor_vm::enqueue_safepoint_fep() { if (fep_p) fatal_error("Low-level debugger interrupted", 0); - safepoint_fep = true; + atomic::store(&safepoint_fep_p, true); code->guard_safepoint(); } void factor_vm::handle_safepoint() { code->unguard_safepoint(); - if (safepoint_fep) + if (atomic::load(&safepoint_fep_p)) { - if (sampling_profiler_p) + if (atomic::load(&sampling_profiler_p)) end_sampling_profiler(); std::cout << "Interrupted\n"; factorbug(); - safepoint_fep = false; + atomic::store(&safepoint_fep_p, false); } else if (sampling_profiler_p) record_sample(); diff --git a/vm/os-windows.cpp b/vm/os-windows.cpp index 81c10757d6..c8f35809f9 100755 --- a/vm/os-windows.cpp +++ b/vm/os-windows.cpp @@ -309,7 +309,7 @@ void factor_vm::sampler_thread_loop() assert(QueryPerformanceCounter(&counter)); counter.QuadPart *= samples_per_second; - while (atomic::fence(), sampling_profiler_p) + while (atomic::load(&sampling_profiler_p)) { SwitchToThread(); assert(QueryPerformanceCounter(&new_counter)); @@ -345,8 +345,7 @@ void factor_vm::start_sampling_profiler_timer() void factor_vm::end_sampling_profiler_timer() { - sampling_profiler_p = false; - atomic::fence(); + atomic::store(&sampling_profiler_p, false); DWORD wait_result = WaitForSingleObject(sampler_thread, 3000*(DWORD)samples_per_second); if (wait_result != WAIT_OBJECT_0) diff --git a/vm/sampling_profiler.cpp b/vm/sampling_profiler.cpp index 9655728bab..d3c20111e0 100644 --- a/vm/sampling_profiler.cpp +++ b/vm/sampling_profiler.cpp @@ -93,8 +93,7 @@ void factor_vm::start_sampling_profiler(fixnum rate) void factor_vm::end_sampling_profiler() { - sampling_profiler_p = false; - atomic::fence(); + atomic::store(&sampling_profiler_p, false); end_sampling_profiler_timer(); record_sample(); } @@ -151,13 +150,13 @@ void factor_vm::primitive_clear_samples() void factor_vm::enqueue_safepoint_sample(cell samples, cell pc, bool foreign_thread_p) { - if (atomic::fence(), sampling_profiler_p) + if (atomic::load(&sampling_profiler_p)) { atomic::add(&safepoint_sample_counts.sample_count, samples); if (foreign_thread_p) atomic::add(&safepoint_sample_counts.foreign_thread_sample_count, samples); else { - if (atomic::fence(), current_gc) + if (atomic::load(¤t_gc_p)) atomic::add(&safepoint_sample_counts.gc_sample_count, samples); if (pc != 0 && !code->seg->in_segment_p(pc)) atomic::add(&safepoint_sample_counts.foreign_sample_count, samples); diff --git a/vm/vm.cpp b/vm/vm.cpp index 0f921e76a0..d7cdf99e85 100755 --- a/vm/vm.cpp +++ b/vm/vm.cpp @@ -9,7 +9,7 @@ factor_vm::factor_vm() : c_to_factor_func(NULL), counting_profiler_p(false), sampling_profiler_p(false), - safepoint_fep(false), + safepoint_fep_p(false), gc_off(false), current_gc(NULL), gc_events(NULL), diff --git a/vm/vm.hpp b/vm/vm.hpp index 08195a81e7..b58cdcb99a 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -62,7 +62,7 @@ struct factor_vm /* Is profiling enabled? */ bool counting_profiler_p; - volatile bool sampling_profiler_p; + volatile cell sampling_profiler_p; fixnum samples_per_second; /* Global variables used to pass fault handler state from signal handler @@ -71,7 +71,7 @@ struct factor_vm cell signal_number; cell signal_fault_addr; unsigned int signal_fpu_status; - volatile bool safepoint_fep; + volatile cell safepoint_fep_p; /* State kept by the sampling profiler */ std::vector samples;