Permalink
Browse files

Correct stack base calculations for Windows

I can never get that math right in my head the first time. This should
be correct and all the fibers tests pass fine in Windows now.
  • Loading branch information...
1 parent e60bd97 commit e9a4d4391271f450923fb8da6974ddea849b0982 @laverdet committed May 8, 2012
Showing with 7 additions and 6 deletions.
  1. +6 −5 src/coroutine.cc
  2. +1 −1 src/coroutine.h
View
@@ -57,9 +57,6 @@ void Coroutine::set_stack_size(size_t size) {
#ifdef CORO_PTHREAD
size += 1024 * 64;
#endif
-#ifdef USE_WINFIBER
- size += 1024 * 64;
-#endif
stack_size = size;
}
@@ -72,7 +69,11 @@ void Coroutine::trampoline(void* that) {
pthread_setspecific(ceil_thread_key, that);
#endif
#ifdef USE_WINFIBER
- static_cast<Coroutine*>(that)->stack_top = _AddressOfReturnAddress(); // hack for getting "bottom" of the stack.
+ // I can't figure out how to get the precise base of the stack in Windows. Since CreateFiber
+ // creates the stack automatically we don't have access to the base. We can however grab the
+ // current esp position, and use that as an approximation. Padding is added for safety since the
+ // base is slightly different.
+ static_cast<Coroutine*>(that)->stack_base = (char*)_AddressOfReturnAddress() - stack_size + 128;
#endif
while (true) {
static_cast<Coroutine*>(that)->entry(const_cast<void*>(static_cast<Coroutine*>(that)->arg));
@@ -206,7 +207,7 @@ void* Coroutine::bottom() const {
#ifndef USE_WINFIBER
return (char*)&stack[0];
#else
- return (void *)(size_t(stack_top) + stack_size);
+ return stack_base;
#endif
}
View
@@ -25,7 +25,7 @@ class Coroutine {
#endif
#ifdef USE_WINFIBER
void* context;
- void* stack_top;
+ void* stack_base;
#endif
std::vector<void*> fls_data;
entry_t* entry;

0 comments on commit e9a4d43

Please sign in to comment.