| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 #define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address, | 423 #define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address, |
| 424 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM) | 424 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM) |
| 425 #undef C | 425 #undef C |
| 426 kIsolateAddressCount | 426 kIsolateAddressCount |
| 427 }; | 427 }; |
| 428 | 428 |
| 429 // Returns the PerIsolateThreadData for the current thread (or NULL if one is | 429 // Returns the PerIsolateThreadData for the current thread (or NULL if one is |
| 430 // not currently set). | 430 // not currently set). |
| 431 static PerIsolateThreadData* CurrentPerIsolateThreadData() { | 431 static PerIsolateThreadData* CurrentPerIsolateThreadData() { |
| 432 return reinterpret_cast<PerIsolateThreadData*>( | 432 return reinterpret_cast<PerIsolateThreadData*>( |
| 433 Thread::GetThreadLocal(per_isolate_thread_data_key_)); | 433 Thread::GetThreadLocal(per_isolate_thread_data_key())); |
| 434 } | 434 } |
| 435 | 435 |
| 436 // Returns the isolate inside which the current thread is running. | 436 // Returns the isolate inside which the current thread is running. |
| 437 INLINE(static Isolate* Current()) { | 437 INLINE(static Isolate* Current()) { |
| 438 const Thread::LocalStorageKey key = isolate_key(); |
| 438 Isolate* isolate = reinterpret_cast<Isolate*>( | 439 Isolate* isolate = reinterpret_cast<Isolate*>( |
| 439 Thread::GetExistingThreadLocal(isolate_key_)); | 440 Thread::GetExistingThreadLocal(key)); |
| 441 if (!isolate) { |
| 442 EnsureDefaultIsolate(); |
| 443 isolate = reinterpret_cast<Isolate*>( |
| 444 Thread::GetExistingThreadLocal(key)); |
| 445 } |
| 440 ASSERT(isolate != NULL); | 446 ASSERT(isolate != NULL); |
| 441 return isolate; | 447 return isolate; |
| 442 } | 448 } |
| 443 | 449 |
| 444 INLINE(static Isolate* UncheckedCurrent()) { | 450 INLINE(static Isolate* UncheckedCurrent()) { |
| 445 return reinterpret_cast<Isolate*>(Thread::GetThreadLocal(isolate_key_)); | 451 return reinterpret_cast<Isolate*>(Thread::GetThreadLocal(isolate_key())); |
| 446 } | 452 } |
| 447 | 453 |
| 448 // Usually called by Init(), but can be called early e.g. to allow | 454 // Usually called by Init(), but can be called early e.g. to allow |
| 449 // testing components that require logging but not the whole | 455 // testing components that require logging but not the whole |
| 450 // isolate. | 456 // isolate. |
| 451 // | 457 // |
| 452 // Safe to call more than once. | 458 // Safe to call more than once. |
| 453 void InitializeLoggingAndCounters(); | 459 void InitializeLoggingAndCounters(); |
| 454 | 460 |
| 455 bool Init(Deserializer* des); | 461 bool Init(Deserializer* des); |
| 456 | 462 |
| 457 bool IsInitialized() { return state_ == INITIALIZED; } | 463 bool IsInitialized() { return state_ == INITIALIZED; } |
| 458 | 464 |
| 459 // True if at least one thread Enter'ed this isolate. | 465 // True if at least one thread Enter'ed this isolate. |
| 460 bool IsInUse() { return entry_stack_ != NULL; } | 466 bool IsInUse() { return entry_stack_ != NULL; } |
| 461 | 467 |
| 462 // Destroys the non-default isolates. | 468 // Destroys the non-default isolates. |
| 463 // Sets default isolate into "has_been_disposed" state rather then destroying, | 469 // Sets default isolate into "has_been_disposed" state rather then destroying, |
| 464 // for legacy API reasons. | 470 // for legacy API reasons. |
| 465 void TearDown(); | 471 void TearDown(); |
| 466 | 472 |
| 467 bool IsDefaultIsolate() const { return this == default_isolate_; } | 473 bool IsDefaultIsolate() const; |
| 468 | 474 |
| 469 // Ensures that process-wide resources and the default isolate have been | 475 // Ensures that process-wide resources and the default isolate have been |
| 470 // allocated. It is only necessary to call this method in rare cases, for | 476 // allocated. It is only necessary to call this method in rare cases, for |
| 471 // example if you are using V8 from within the body of a static initializer. | 477 // example if you are using V8 from within the body of a static initializer. |
| 472 // Safe to call multiple times. | 478 // Safe to call multiple times. |
| 473 static void EnsureDefaultIsolate(); | 479 static void EnsureDefaultIsolate(); |
| 474 | 480 |
| 475 // Find the PerThread for this particular (isolate, thread) combination | 481 // Find the PerThread for this particular (isolate, thread) combination |
| 476 // If one does not yet exist, return null. | 482 // If one does not yet exist, return null. |
| 477 PerIsolateThreadData* FindPerThreadDataForThisThread(); | 483 PerIsolateThreadData* FindPerThreadDataForThisThread(); |
| 478 | 484 |
| 479 #ifdef ENABLE_DEBUGGER_SUPPORT | 485 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 480 // Get the debugger from the default isolate. Preinitializes the | 486 // Get the debugger from the default isolate. Preinitializes the |
| 481 // default isolate if needed. | 487 // default isolate if needed. |
| 482 static Debugger* GetDefaultIsolateDebugger(); | 488 static Debugger* GetDefaultIsolateDebugger(); |
| 483 #endif | 489 #endif |
| 484 | 490 |
| 485 // Get the stack guard from the default isolate. Preinitializes the | 491 // Get the stack guard from the default isolate. Preinitializes the |
| 486 // default isolate if needed. | 492 // default isolate if needed. |
| 487 static StackGuard* GetDefaultIsolateStackGuard(); | 493 static StackGuard* GetDefaultIsolateStackGuard(); |
| 488 | 494 |
| 489 // Returns the key used to store the pointer to the current isolate. | 495 // Returns the key used to store the pointer to the current isolate. |
| 490 // Used internally for V8 threads that do not execute JavaScript but still | 496 // Used internally for V8 threads that do not execute JavaScript but still |
| 491 // are part of the domain of an isolate (like the context switcher). | 497 // are part of the domain of an isolate (like the context switcher). |
| 492 static Thread::LocalStorageKey isolate_key() { | 498 static Thread::LocalStorageKey isolate_key(); |
| 493 return isolate_key_; | |
| 494 } | |
| 495 | 499 |
| 496 // Returns the key used to store process-wide thread IDs. | 500 // Returns the key used to store process-wide thread IDs. |
| 497 static Thread::LocalStorageKey thread_id_key() { | 501 static Thread::LocalStorageKey thread_id_key(); |
| 498 return thread_id_key_; | 502 |
| 499 } | 503 static Thread::LocalStorageKey per_isolate_thread_data_key(); |
| 500 | 504 |
| 501 // If a client attempts to create a Locker without specifying an isolate, | 505 // If a client attempts to create a Locker without specifying an isolate, |
| 502 // we assume that the client is using legacy behavior. Set up the current | 506 // we assume that the client is using legacy behavior. Set up the current |
| 503 // thread to be inside the implicit isolate (or fail a check if we have | 507 // thread to be inside the implicit isolate (or fail a check if we have |
| 504 // switched to non-legacy behavior). | 508 // switched to non-legacy behavior). |
| 505 static void EnterDefaultIsolate(); | 509 static void EnterDefaultIsolate(); |
| 506 | 510 |
| 507 // Mutex for serializing access to break control structures. | 511 // Mutex for serializing access to break control structures. |
| 508 Mutex* break_access() { return break_access_; } | 512 Mutex* break_access() { return break_access_; } |
| 509 | 513 |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 void set_date_cache(DateCache* date_cache) { | 1029 void set_date_cache(DateCache* date_cache) { |
| 1026 if (date_cache != date_cache_) { | 1030 if (date_cache != date_cache_) { |
| 1027 delete date_cache_; | 1031 delete date_cache_; |
| 1028 } | 1032 } |
| 1029 date_cache_ = date_cache; | 1033 date_cache_ = date_cache; |
| 1030 } | 1034 } |
| 1031 | 1035 |
| 1032 private: | 1036 private: |
| 1033 Isolate(); | 1037 Isolate(); |
| 1034 | 1038 |
| 1039 friend struct GlobalState; |
| 1040 friend struct InitializeGlobalState; |
| 1041 |
| 1035 // The per-process lock should be acquired before the ThreadDataTable is | 1042 // The per-process lock should be acquired before the ThreadDataTable is |
| 1036 // modified. | 1043 // modified. |
| 1037 class ThreadDataTable { | 1044 class ThreadDataTable { |
| 1038 public: | 1045 public: |
| 1039 ThreadDataTable(); | 1046 ThreadDataTable(); |
| 1040 ~ThreadDataTable(); | 1047 ~ThreadDataTable(); |
| 1041 | 1048 |
| 1042 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id); | 1049 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id); |
| 1043 void Insert(PerIsolateThreadData* data); | 1050 void Insert(PerIsolateThreadData* data); |
| 1044 void Remove(Isolate* isolate, ThreadId thread_id); | 1051 void Remove(Isolate* isolate, ThreadId thread_id); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1067 | 1074 |
| 1068 int entry_count; | 1075 int entry_count; |
| 1069 PerIsolateThreadData* previous_thread_data; | 1076 PerIsolateThreadData* previous_thread_data; |
| 1070 Isolate* previous_isolate; | 1077 Isolate* previous_isolate; |
| 1071 EntryStackItem* previous_item; | 1078 EntryStackItem* previous_item; |
| 1072 | 1079 |
| 1073 private: | 1080 private: |
| 1074 DISALLOW_COPY_AND_ASSIGN(EntryStackItem); | 1081 DISALLOW_COPY_AND_ASSIGN(EntryStackItem); |
| 1075 }; | 1082 }; |
| 1076 | 1083 |
| 1077 // This mutex protects highest_thread_id_, thread_data_table_ and | |
| 1078 // default_isolate_. | |
| 1079 static Mutex* process_wide_mutex_; | |
| 1080 | |
| 1081 static Thread::LocalStorageKey per_isolate_thread_data_key_; | |
| 1082 static Thread::LocalStorageKey isolate_key_; | |
| 1083 static Thread::LocalStorageKey thread_id_key_; | |
| 1084 static Isolate* default_isolate_; | |
| 1085 static ThreadDataTable* thread_data_table_; | |
| 1086 | |
| 1087 void Deinit(); | 1084 void Deinit(); |
| 1088 | 1085 |
| 1089 static void SetIsolateThreadLocals(Isolate* isolate, | 1086 static void SetIsolateThreadLocals(Isolate* isolate, |
| 1090 PerIsolateThreadData* data); | 1087 PerIsolateThreadData* data); |
| 1091 | 1088 |
| 1092 enum State { | 1089 enum State { |
| 1093 UNINITIALIZED, // Some components may not have been allocated. | 1090 UNINITIALIZED, // Some components may not have been allocated. |
| 1094 INITIALIZED // All components are fully initialized. | 1091 INITIALIZED // All components are fully initialized. |
| 1095 }; | 1092 }; |
| 1096 | 1093 |
| 1097 State state_; | 1094 State state_; |
| 1098 EntryStackItem* entry_stack_; | 1095 EntryStackItem* entry_stack_; |
| 1099 | 1096 |
| 1100 // Allocate and insert PerIsolateThreadData into the ThreadDataTable | 1097 // Allocate and insert PerIsolateThreadData into the ThreadDataTable |
| 1101 // (regardless of whether such data already exists). | 1098 // (regardless of whether such data already exists). |
| 1102 PerIsolateThreadData* AllocatePerIsolateThreadData(ThreadId thread_id); | 1099 PerIsolateThreadData* AllocatePerIsolateThreadData(ThreadId thread_id); |
| 1103 | 1100 |
| 1104 // Find the PerThread for this particular (isolate, thread) combination. | 1101 // Find the PerThread for this particular (isolate, thread) combination. |
| 1105 // If one does not yet exist, allocate a new one. | 1102 // If one does not yet exist, allocate a new one. |
| 1106 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread(); | 1103 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread(); |
| 1107 | 1104 |
| 1108 // PreInits and returns a default isolate. Needed when a new thread tries | 1105 // PreInits and returns a default isolate. Needed when a new thread tries |
| 1109 // to create a Locker for the first time (the lock itself is in the isolate). | 1106 // to create a Locker for the first time (the lock itself is in the isolate). |
| 1110 static Isolate* GetDefaultIsolateForLocking(); | 1107 static Isolate* GetDefaultIsolateForLocking(); |
| 1111 | 1108 |
| 1112 // Initializes the current thread to run this Isolate. | 1109 // Initializes the current thread to run this Isolate. |
| 1113 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate | 1110 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate |
| 1114 // at the same time, this should be prevented using external locking. | 1111 // at the same time, this should be prevented using external locking. |
| 1115 void Enter(); | 1112 void Enter(); |
| 1116 | 1113 |
| 1117 // Exits the current thread. The previosuly entered Isolate is restored | 1114 // Exits the current thread. The previosuly entered Isolate is restored |
| 1118 // for the thread. | 1115 // for the thread. |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1400 | 1397 |
| 1401 // Mark the global context with out of memory. | 1398 // Mark the global context with out of memory. |
| 1402 inline void Context::mark_out_of_memory() { | 1399 inline void Context::mark_out_of_memory() { |
| 1403 global_context()->set_out_of_memory(HEAP->true_value()); | 1400 global_context()->set_out_of_memory(HEAP->true_value()); |
| 1404 } | 1401 } |
| 1405 | 1402 |
| 1406 | 1403 |
| 1407 } } // namespace v8::internal | 1404 } } // namespace v8::internal |
| 1408 | 1405 |
| 1409 #endif // V8_ISOLATE_H_ | 1406 #endif // V8_ISOLATE_H_ |
| OLD | NEW |