Skip to content

Instantly share code, notes, and snippets.

@laverdet
Created December 4, 2014 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laverdet/26da7e27efa9934fcd2d to your computer and use it in GitHub Desktop.
Save laverdet/26da7e27efa9934fcd2d to your computer and use it in GitHub Desktop.
diff --git a/src/coroutine.cc b/src/coroutine.cc
index f2a0d3d..3c9a8ad 100644
--- a/src/coroutine.cc
+++ b/src/coroutine.cc
@@ -20,8 +20,6 @@
using namespace std;
const size_t v8_tls_keys = 3;
-static pthread_key_t floor_thread_key = 0;
-static pthread_key_t ceil_thread_key = 0;
static pthread_key_t coro_thread_key = 0;
static size_t stack_size = 0;
@@ -30,39 +28,30 @@ static vector<Coroutine*> fiber_pool;
static Coroutine* delete_me = NULL;
size_t Coroutine::pool_size = 120;
-#ifndef WINDOWS
-static void* find_thread_id_key(void* arg)
-#else
-static DWORD __stdcall find_thread_id_key(LPVOID arg)
-#endif
-{
- v8::Isolate* isolate = static_cast<v8::Isolate*>(arg);
- assert(isolate != NULL);
- v8::Locker locker(isolate);
- isolate->Enter();
- floor_thread_key = 0x7777;
- for (pthread_key_t ii = coro_thread_key - 1; ii >= (coro_thread_key >= 20 ? coro_thread_key - 20 : 0); --ii) {
- if (pthread_getspecific(ii) == isolate) {
- floor_thread_key = ii;
- break;
- }
+namespace v8 {
+ namespace internal {
+ class Thread {
+ public:
+ enum LocalStorageKey {
+ LOCAL_STORAGE_KEY_MIN_VALUE = -0x80000000,
+ LOCAL_STORAGE_KEY_MAX_VALUE = 0x7fffffff
+ };
+ };
+ class Isolate {
+ public:
+ static uint32_t per_isolate_thread_data_key_;
+ static uint32_t isolate_key_;
+ static uint32_t thread_id_key_;
+ };
}
- assert(floor_thread_key != 0x7777);
- ceil_thread_key = floor_thread_key + v8_tls_keys - 1;
- isolate->Exit();
- return NULL;
}
/**
* Coroutine class definition
*/
void Coroutine::init(v8::Isolate* isolate) {
- v8::Unlocker unlocker(isolate);
pthread_key_create(&coro_thread_key, NULL);
pthread_setspecific(coro_thread_key, &current());
- pthread_t thread;
- pthread_create(&thread, NULL, find_thread_id_key, isolate);
- pthread_join(thread, NULL);
}
Coroutine& Coroutine::current() {
@@ -160,13 +149,14 @@ void Coroutine::reset(entry_t* entry, void* arg) {
void Coroutine::transfer(Coroutine& next) {
assert(this != &next);
#ifndef CORO_PTHREAD
- {
- for (pthread_key_t ii = floor_thread_key; ii <= ceil_thread_key; ++ii) {
- // Swap TLS keys with fiber locals
- fls_data[ii - floor_thread_key] = pthread_getspecific(ii);
- pthread_setspecific(ii, next.fls_data[ii - floor_thread_key]);
- }
- }
+ fls_data[0] = pthread_getspecific(v8::internal::Isolate::per_isolate_thread_data_key_);
+ fls_data[1] = pthread_getspecific(v8::internal::Isolate::isolate_key_);
+ fls_data[2] = pthread_getspecific(v8::internal::Isolate::thread_id_key_);
+
+ pthread_setspecific(v8::internal::Isolate::per_isolate_thread_data_key_, next.fls_data[0]);
+ pthread_setspecific(v8::internal::Isolate::isolate_key_, next.fls_data[1]);
+ pthread_setspecific(v8::internal::Isolate::thread_id_key_, next.fls_data[2]);
+
pthread_setspecific(coro_thread_key, &next);
#endif
coro_transfer(&context, &next.context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment