Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FireDomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// On fait plus de chaines....

// Static variables
long ForeFireAtom::instanceNRCount = 0;
std::atomic<long> ForeFireAtom::instanceNRCount{0};

const double FireDomain::endChain = -1.;
const double FireDomain::endCom = -10.;
Expand Down
15 changes: 13 additions & 2 deletions src/ForeFireAtom.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "include/Futils.h"

#include <atomic>

using namespace std;

namespace libforefire{
Expand All @@ -33,7 +35,13 @@ namespace libforefire{
class ForeFireAtom {
private:

static long instanceNRCount; /*!< Instance Count */
/*! \brief Instance count, the source of every atom's id.
*
* Atomic because objects are created from more than one thread once the
* GIL is out of the way, and a plain `++` there is a data race that can
* hand the same id to two atoms.
*/
static std::atomic<long> instanceNRCount;

double time; /*!< current time of the object */
double updateTime; /*!< next update time */
Expand Down Expand Up @@ -103,7 +111,10 @@ class ForeFireAtom {
return getIDfromLongs((long) did/domainMult(),(long) did%domainMult());
}
void getNewID(const long& domainId){
numID = getIDfromLongs(domainId,instanceNRCount++);
// relaxed: ids only have to be distinct, not ordered against other
// memory operations.
numID = getIDfromLongs(domainId,
instanceNRCount.fetch_add(1, std::memory_order_relaxed));
}

/*! \brief Pure virtual function for inpus */
Expand Down
10 changes: 7 additions & 3 deletions src/SimulationParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ namespace libforefire {



SimulationParameters* SimulationParameters::instance = 0;

string SimulationParameters::undefined = "1234567890";
double SimulationParameters::doubleUndefined = 1234567890.;
int SimulationParameters::intUndefined = 1234567890;
size_t SimulationParameters::sizeUndefined = 1234567890;


SimulationParameters* SimulationParameters::GetInstance(){
if ( instance == 0 ) instance = new SimulationParameters;
// A function-local static: C++11 onwards guarantees its initialisation
// runs exactly once even if several threads arrive here together. The
// previous `if (instance == 0) instance = new ...` let two threads both
// see null and both construct, leaving them with different parameter
// objects. Never deleted, matching the previous behaviour; the parameters
// live for the whole process.
static SimulationParameters* const instance = new SimulationParameters();
return instance;
}

Expand Down
2 changes: 0 additions & 2 deletions src/SimulationParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace libforefire {

class SimulationParameters {

static SimulationParameters* instance; /*!< Singleton-type class */

/*! values for undefined parameters */
static string undefined;
static double doubleUndefined;
Expand Down
Loading