Skip to content
Snippets Groups Projects
Commit 8d3322bf authored by Volker Springel's avatar Volker Springel
Browse files

got rid off the ugly memcpy() hack to work-around the implicitly deleted copy...

got rid off the ugly memcpy() hack to work-around the implicitly deleted copy operator of atomic_flag (thanks to an insightful suggestion by Matias Mannerkoski)
parent 8490ce0c
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "gadgetconfig.h" #include "gadgetconfig.h"
#include <stdint.h> #include <stdint.h>
#include <atomic>
#include <cstddef> #include <cstddef>
#ifdef EXPLICIT_VECTORIZATION #ifdef EXPLICIT_VECTORIZATION
#include "../vectorclass/vectorclass.h" #include "../vectorclass/vectorclass.h"
...@@ -356,6 +357,35 @@ struct thread_data ...@@ -356,6 +357,35 @@ struct thread_data
int *Exportflag; int *Exportflag;
}; };
template <typename T>
struct copyable_atomic : std::atomic<T>
{
using std::atomic<T>::atomic;
copyable_atomic(const copyable_atomic &ca) noexcept : std::atomic<T>(ca.load()) {}
using std::atomic<T>::operator=;
copyable_atomic &operator=(const copyable_atomic &other) noexcept
{
this->store(other.load());
return *this;
}
};
struct copyable_atomic_flag : std::atomic_flag
{
using std::atomic_flag::atomic_flag;
copyable_atomic_flag(const copyable_atomic_flag &ca) noexcept { this->clear(); }
copyable_atomic_flag &operator=(const copyable_atomic_flag &other) noexcept
{
this->clear();
return *this;
}
};
#ifdef LONG_X_BITS #ifdef LONG_X_BITS
#define LONG_X (1 << (LONG_X_BITS)) #define LONG_X (1 << (LONG_X_BITS))
#define MAX_LONG_X_BITS LONG_X_BITS #define MAX_LONG_X_BITS LONG_X_BITS
......
...@@ -33,23 +33,12 @@ ...@@ -33,23 +33,12 @@
*/ */
struct particle_data struct particle_data
{ {
// we do this ugly trick of using memcpy for our own copy constructor and assignment operator // Note that because the atomic_flag and atomic data-types in particle_data have an implicitly deleted copy operator,
// because the atomic_flag in particle_data has an implicitly deleted copy operator... so that the implicit functions // we are using them encapsulated in a structure defined in dtypes.h that implements these copy and assignment operators explicitly.
// for this are unavailable. But we know what we are doing here, and surrounding this with an ugly hack // This is fine because the code logic guarantees that there is no concurrent access when such copies of particle_data happen.
// is the easiest way at the moment to work around this in our case unnecessary protection
particle_data() {} particle_data() {}
// declare our own copy constructor
particle_data(particle_data& other) { memcpy(static_cast<void*>(this), static_cast<void*>(&other), sizeof(particle_data)); }
// declare our own assignment operator
particle_data& operator=(particle_data& other)
{
memcpy(static_cast<void*>(this), static_cast<void*>(&other), sizeof(particle_data));
return *this;
}
MyIntPosType IntPos[3]; /**< particle position at its current time, stored as an integer type */ MyIntPosType IntPos[3]; /**< particle position at its current time, stored as an integer type */
MyFloat Vel[3]; /**< particle velocity at its current time */ MyFloat Vel[3]; /**< particle velocity at its current time */
vector<MyFloat> GravAccel; /**< particle acceleration due to gravity */ vector<MyFloat> GravAccel; /**< particle acceleration due to gravity */
...@@ -57,9 +46,9 @@ struct particle_data ...@@ -57,9 +46,9 @@ struct particle_data
MyFloat GravPM[3]; /**< particle acceleration due to long-range PM gravity force */ MyFloat GravPM[3]; /**< particle acceleration due to long-range PM gravity force */
#endif #endif
std::atomic<integertime> Ti_Current; /**< current time on integer timeline */ copyable_atomic<integertime> Ti_Current; /**< current time on integer timeline */
float OldAcc; /**< magnitude of old gravitational force. Used in relative opening criterion */ float OldAcc; /**< magnitude of old gravitational force. Used in relative opening criterion */
int GravCost; /**< weight factors used for balancing the work-load */ int GravCost; /**< weight factors used for balancing the work-load */
#ifndef LEAN #ifndef LEAN
private: private:
...@@ -85,7 +74,7 @@ struct particle_data ...@@ -85,7 +74,7 @@ struct particle_data
#endif #endif
#ifndef LEAN #ifndef LEAN
std::atomic_flag access; copyable_atomic_flag access;
#endif #endif
#ifdef REARRANGE_OPTION #ifdef REARRANGE_OPTION
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment