Permalink
Browse files

Volatile members

This declares the member variables which getting mucked with from
different fibers as volatile. Hopefully this will take care of some of
the heisenbugs a few users have been reporting.
  • Loading branch information...
laverdet committed Feb 17, 2011
1 parent 19dfdca commit 3c0f9f71b15b2b50bbefc54e35a7dfb5bfd75ce5
Showing with 21 additions and 21 deletions.
  1. +6 −6 src/coroutine.cc
  2. +3 −3 src/coroutine.h
  3. +12 −12 src/fibers.cc
View
@@ -80,8 +80,8 @@ class Thread {
public:
pthread_t handle;
- Coroutine* current_fiber;
- Coroutine* delete_me;
+ volatile Coroutine* current_fiber;
+ volatile Coroutine* delete_me;
static void free(void* that) {
delete static_cast<Thread*>(that);
@@ -197,7 +197,7 @@ void Coroutine::trampoline(Coroutine &that) {
Coroutine& Coroutine::current() {
Thread& thread = *static_cast<Thread*>(o_pthread_getspecific(thread_key));
- return *thread.current_fiber;
+ return *const_cast<Coroutine*>(thread.current_fiber);
}
const bool Coroutine::is_local_storage_enabled() {
@@ -228,16 +228,16 @@ void Coroutine::reset(entry_t* entry, void* arg) {
this->arg = arg;
}
-void Coroutine::run() {
- Coroutine& current = *thread.current_fiber;
+void Coroutine::run() volatile {
+ Coroutine& current = *const_cast<Coroutine*>(thread.current_fiber);
assert(&current != this);
thread.current_fiber = this;
if (thread.delete_me) {
assert(this != thread.delete_me);
delete thread.delete_me;
thread.delete_me = NULL;
}
- swapcontext(&current.context, &context);
+ swapcontext(&current.context, const_cast<ucontext_t*>(&context));
thread.current_fiber = &current;
}
View
@@ -20,8 +20,8 @@ class Coroutine {
size_t id;
ucontext_t context;
std::vector<char_noinit, __gnu_cxx::__pool_alloc<char_noinit> > stack;
- entry_t* entry;
- void* arg;
+ volatile entry_t* entry;
+ volatile void* arg;
static void trampoline(Coroutine& that);
~Coroutine() {}
@@ -67,7 +67,7 @@ class Coroutine {
* Start or resume execution in this fiber. Note there is no explicit yield() function,
* you must manually run another fiber.
*/
- void run();
+ void run() volatile;
/**
* Finish this coroutine.. This will halt execution of this coroutine and resume execution
View
@@ -23,7 +23,7 @@ class Fiber {
private:
static Locker* locker; // Node does not use locks or threads, so we need a global lock
static Persistent<FunctionTemplate> tmpl;
- static Fiber* current;
+ static volatile Fiber* current;
static vector<Fiber*> orphaned_fibers;
static Persistent<Value> fatal_stack;
static Persistent<String> fiber_token;
@@ -33,13 +33,13 @@ class Fiber {
Persistent<Context> v8_context;
Persistent<Value> zombie_exception;
Persistent<Value> yielded;
- bool yielded_exception;
- Coroutine* entry_fiber;
+ volatile bool yielded_exception;
+ volatile Coroutine* entry_fiber;
Coroutine* this_fiber;
- bool started;
- bool yielding;
- bool zombie;
- bool resetting;
+ volatile bool started;
+ volatile bool yielding;
+ volatile bool zombie;
+ volatile bool resetting;
Fiber(Persistent<Object> handle, Persistent<Function> cb, Persistent<Context> v8_context) :
handle(handle),
@@ -294,7 +294,7 @@ class Fiber {
void SwapContext() {
entry_fiber = &Coroutine::current();
- Fiber* last_fiber = current;
+ Fiber* last_fiber = const_cast<Fiber*>(current);
current = this;
// This will jump into either `RunFiber()` or `Yield()`, depending on if the fiber was
@@ -378,7 +378,7 @@ class Fiber {
// The function returned (instead of yielding).
that.started = false;
- that.this_fiber->finish(*that.entry_fiber);
+ that.this_fiber->finish(*const_cast<Coroutine*>(that.entry_fiber));
}
/**
@@ -392,7 +392,7 @@ class Fiber {
THROW(Exception::Error, "yield() called with no fiber running");
}
- Fiber& that = *current;
+ Fiber& that = *const_cast<Fiber*>(current);
if (that.zombie) {
return ThrowException(that.zombie_exception);
@@ -436,7 +436,7 @@ class Fiber {
static Handle<Value> GetCurrent(Local<String> property, const AccessorInfo& info) {
if (current) {
- return current->handle;
+ return const_cast<Fiber*>(current)->handle;
} else {
return Undefined();
}
@@ -476,7 +476,7 @@ class Fiber {
Persistent<FunctionTemplate> Fiber::tmpl;
Locker* Fiber::locker;
-Fiber* Fiber::current = NULL;
+volatile Fiber* Fiber::current = NULL;
vector<Fiber*> Fiber::orphaned_fibers;
Persistent<Value> Fiber::fatal_stack;
Persistent<String> Fiber::fiber_token;

0 comments on commit 3c0f9f7

Please sign in to comment.