static int nbglGetExclusiveSceneAccess(scene_t* scene) { int pid = (int) getpid(); int oldPID = OPA_cas_int(&scene->attachedLock, 0, pid); if (oldPID != 0) { if (mwProcessIsAlive(oldPID)) { mw_printf("Could not get exclusive access to simulation shared segment " "(Owned by process %d)\n", oldPID); return 1; } else { mw_printf("Simulation shared segment owned by dead process %d, stealing it\n", oldPID); /* Process is dead, steal the lock */ nbglReleaseSceneLocks(scene); return 0; } } else { OPA_store_int(&scene->attachedPID, 0); return 0; } }
void lock() { do { if (OPA_cas_int(&flag, 1, 0)) break; } while (lockint); }