Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualApochPiQ

Posted 30 May 2013 - 05:13 PM

You can build an atomic one-time lock on any CPU that supports atomic compare and swap:
 
 
// Some global
int counter = 0;
 
if (CompareAndSwap(counter, 1) == 0) {
   // Do initialization
}
 
// Carry on
[edit] For completeness: you also need a spinlock that waits until initialization is completed:
// Another global
int initdone = 0;

if (CompareAndSwap(counter, 1)) {
// init
// atomic set initdone to 1
}

while (AtomicCompare(initdone, 0)) {
// No-op
}

// Carry on

#2ApochPiQ

Posted 30 May 2013 - 05:13 PM

You can build an atomic one-time lock on any CPU that supports atomic compare and swap:
 
 
// Some global
int counter = 0;
 
if (CompareAndSwap(counter, 1) == 0) {
   // Do initialization
}
 
// Carry on


[edit] For completeness: you also need a spinlock that waits until initialization is completed:

[code=auto:0]// Another global
int initdone = 0;

if (CompareAndSwap(counter, 1)) {
// init
// atomic set initdone to 1
}

while (AtomicCompare(initdone, 0)) {
// No-op
}

// Carry on

#1ApochPiQ

Posted 30 May 2013 - 05:11 PM

You can build an atomic one-time lock on any CPU that supports atomic compare and swap:

 

// Some global
int counter = 0;
 
if (CompareAndSwap(counter, 1) == 0) {
   // Do initialization
}
 
// Carry on

PARTNERS