OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_THREAD_H_ | 5 #ifndef VM_THREAD_H_ |
6 #define VM_THREAD_H_ | 6 #define VM_THREAD_H_ |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
| 9 #include "platform/assert.h" |
| 10 #include "vm/atomic.h" |
| 11 #include "vm/bitfield.h" |
9 #include "vm/globals.h" | 12 #include "vm/globals.h" |
10 #include "vm/handles.h" | 13 #include "vm/handles.h" |
11 #include "vm/os_thread.h" | 14 #include "vm/os_thread.h" |
12 #include "vm/store_buffer.h" | 15 #include "vm/store_buffer.h" |
13 #include "vm/runtime_entry_list.h" | 16 #include "vm/runtime_entry_list.h" |
14 | 17 |
15 namespace dart { | 18 namespace dart { |
16 | 19 |
17 class AbstractType; | 20 class AbstractType; |
18 class ApiLocalScope; | 21 class ApiLocalScope; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 139 |
137 // Empties the store buffer block into the isolate. | 140 // Empties the store buffer block into the isolate. |
138 void PrepareForGC(); | 141 void PrepareForGC(); |
139 | 142 |
140 // OSThread corresponding to this thread. | 143 // OSThread corresponding to this thread. |
141 OSThread* os_thread() const { return os_thread_; } | 144 OSThread* os_thread() const { return os_thread_; } |
142 void set_os_thread(OSThread* os_thread) { | 145 void set_os_thread(OSThread* os_thread) { |
143 os_thread_ = os_thread; | 146 os_thread_ = os_thread; |
144 } | 147 } |
145 | 148 |
| 149 // Monitor corresponding to this thread. |
| 150 Monitor* thread_lock() const { return thread_lock_; } |
| 151 |
146 // The topmost zone used for allocation in this thread. | 152 // The topmost zone used for allocation in this thread. |
147 Zone* zone() const { return zone_; } | 153 Zone* zone() const { return zone_; } |
148 | 154 |
149 // The reusable api local scope for this thread. | 155 // The reusable api local scope for this thread. |
150 ApiLocalScope* api_reusable_scope() const { return api_reusable_scope_; } | 156 ApiLocalScope* api_reusable_scope() const { return api_reusable_scope_; } |
151 void set_api_reusable_scope(ApiLocalScope* value) { | 157 void set_api_reusable_scope(ApiLocalScope* value) { |
152 ASSERT(value == NULL || api_reusable_scope_ == NULL); | 158 ASSERT(value == NULL || api_reusable_scope_ == NULL); |
153 api_reusable_scope_ = value; | 159 api_reusable_scope_ = value; |
154 } | 160 } |
155 | 161 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 #if defined(TESTING) | 208 #if defined(TESTING) |
203 bool StoreBufferContains(RawObject* obj) const { | 209 bool StoreBufferContains(RawObject* obj) const { |
204 return store_buffer_block_->Contains(obj); | 210 return store_buffer_block_->Contains(obj); |
205 } | 211 } |
206 #endif | 212 #endif |
207 void StoreBufferBlockProcess(StoreBuffer::ThresholdPolicy policy); | 213 void StoreBufferBlockProcess(StoreBuffer::ThresholdPolicy policy); |
208 static intptr_t store_buffer_block_offset() { | 214 static intptr_t store_buffer_block_offset() { |
209 return OFFSET_OF(Thread, store_buffer_block_); | 215 return OFFSET_OF(Thread, store_buffer_block_); |
210 } | 216 } |
211 | 217 |
212 uword top_exit_frame_info() const { return top_exit_frame_info_; } | 218 uword top_exit_frame_info() const { |
| 219 return top_exit_frame_info_; |
| 220 } |
213 static intptr_t top_exit_frame_info_offset() { | 221 static intptr_t top_exit_frame_info_offset() { |
214 return OFFSET_OF(Thread, top_exit_frame_info_); | 222 return OFFSET_OF(Thread, top_exit_frame_info_); |
215 } | 223 } |
216 | 224 |
217 StackResource* top_resource() const { return top_resource_; } | 225 StackResource* top_resource() const { return top_resource_; } |
218 void set_top_resource(StackResource* value) { | 226 void set_top_resource(StackResource* value) { |
219 top_resource_ = value; | 227 top_resource_ = value; |
220 } | 228 } |
221 static intptr_t top_resource_offset() { | 229 static intptr_t top_resource_offset() { |
222 return OFFSET_OF(Thread, top_resource_); | 230 return OFFSET_OF(Thread, top_resource_); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 uword vm_tag() const { | 357 uword vm_tag() const { |
350 return vm_tag_; | 358 return vm_tag_; |
351 } | 359 } |
352 void set_vm_tag(uword tag) { | 360 void set_vm_tag(uword tag) { |
353 vm_tag_ = tag; | 361 vm_tag_ = tag; |
354 } | 362 } |
355 static intptr_t vm_tag_offset() { | 363 static intptr_t vm_tag_offset() { |
356 return OFFSET_OF(Thread, vm_tag_); | 364 return OFFSET_OF(Thread, vm_tag_); |
357 } | 365 } |
358 | 366 |
| 367 RawGrowableObjectArray* pending_functions(); |
| 368 |
359 #if defined(DEBUG) | 369 #if defined(DEBUG) |
360 #define REUSABLE_HANDLE_SCOPE_ACCESSORS(object) \ | 370 #define REUSABLE_HANDLE_SCOPE_ACCESSORS(object) \ |
361 void set_reusable_##object##_handle_scope_active(bool value) { \ | 371 void set_reusable_##object##_handle_scope_active(bool value) { \ |
362 reusable_##object##_handle_scope_active_ = value; \ | 372 reusable_##object##_handle_scope_active_ = value; \ |
363 } \ | 373 } \ |
364 bool reusable_##object##_handle_scope_active() const { \ | 374 bool reusable_##object##_handle_scope_active() const { \ |
365 return reusable_##object##_handle_scope_active_; \ | 375 return reusable_##object##_handle_scope_active_; \ |
366 } | 376 } |
367 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_SCOPE_ACCESSORS) | 377 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_SCOPE_ACCESSORS) |
368 #undef REUSABLE_HANDLE_SCOPE_ACCESSORS | 378 #undef REUSABLE_HANDLE_SCOPE_ACCESSORS |
369 | 379 |
370 bool IsAnyReusableHandleScopeActive() const { | 380 bool IsAnyReusableHandleScopeActive() const { |
371 #define IS_REUSABLE_HANDLE_SCOPE_ACTIVE(object) \ | 381 #define IS_REUSABLE_HANDLE_SCOPE_ACTIVE(object) \ |
372 if (reusable_##object##_handle_scope_active_) return true; | 382 if (reusable_##object##_handle_scope_active_) return true; |
373 REUSABLE_HANDLE_LIST(IS_REUSABLE_HANDLE_SCOPE_ACTIVE) | 383 REUSABLE_HANDLE_LIST(IS_REUSABLE_HANDLE_SCOPE_ACTIVE) |
374 return false; | 384 return false; |
375 #undef IS_REUSABLE_HANDLE_SCOPE_ACTIVE | 385 #undef IS_REUSABLE_HANDLE_SCOPE_ACTIVE |
376 } | 386 } |
377 #endif // defined(DEBUG) | 387 #endif // defined(DEBUG) |
378 | 388 |
379 void ClearReusableHandles(); | 389 void ClearReusableHandles(); |
380 | 390 |
381 #define REUSABLE_HANDLE(object) \ | 391 #define REUSABLE_HANDLE(object) \ |
382 object& object##Handle() const { \ | 392 object& object##Handle() const { \ |
383 return *object##_handle_; \ | 393 return *object##_handle_; \ |
384 } | 394 } |
385 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE) | 395 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE) |
386 #undef REUSABLE_HANDLE | 396 #undef REUSABLE_HANDLE |
387 | 397 |
388 RawGrowableObjectArray* pending_functions(); | 398 /* |
| 399 * Fields used to support safepointing a thread. |
| 400 * |
| 401 * - Bit 0 of the safepoint_state_ field is used to indicate if the thread is |
| 402 * already at a safepoint, |
| 403 * - Bit 1 of the safepoint_state_ field is used to indicate if a safepoint |
| 404 * operation is requested for this thread. |
| 405 * - Bit 2 of the safepoint_state_ field is used to indicate that the thread |
| 406 * is blocked for the safepoint operation to complete. |
| 407 * |
| 408 * The safepoint execution state (described above) for a thread is stored in |
| 409 * in the execution_state_ field. |
| 410 * Potential execution states a thread could be in: |
| 411 * kThreadInGenerated - The thread is running jitted dart/stub code. |
| 412 * kThreadInVM - The thread is running VM code. |
| 413 * kThreadInNative - The thread is running native code. |
| 414 * kThreadInBlockedState - The thread is blocked waiting for a resource. |
| 415 */ |
| 416 static intptr_t safepoint_state_offset() { |
| 417 return OFFSET_OF(Thread, safepoint_state_); |
| 418 } |
| 419 static bool IsAtSafepoint(uint32_t state) { |
| 420 return AtSafepointField::decode(state); |
| 421 } |
| 422 bool IsAtSafepoint() const { |
| 423 return AtSafepointField::decode(safepoint_state_); |
| 424 } |
| 425 static uint32_t SetAtSafepoint(bool value, uint32_t state) { |
| 426 return AtSafepointField::update(value, state); |
| 427 } |
| 428 void SetAtSafepoint(bool value) { |
| 429 ASSERT(thread_lock()->IsOwnedByCurrentThread()); |
| 430 safepoint_state_ = AtSafepointField::update(value, safepoint_state_); |
| 431 } |
| 432 bool IsSafepointRequested() const { |
| 433 return SafepointRequestedField::decode(safepoint_state_); |
| 434 } |
| 435 static uint32_t SetSafepointRequested(bool value, uint32_t state) { |
| 436 return SafepointRequestedField::update(value, state); |
| 437 } |
| 438 uint32_t SetSafepointRequested(bool value) { |
| 439 ASSERT(thread_lock()->IsOwnedByCurrentThread()); |
| 440 uint32_t old_state; |
| 441 uint32_t new_state; |
| 442 do { |
| 443 old_state = safepoint_state_; |
| 444 new_state = SafepointRequestedField::update(value, old_state); |
| 445 } while (AtomicOperations::CompareAndSwapUint32(&safepoint_state_, |
| 446 old_state, |
| 447 new_state) != old_state); |
| 448 return old_state; |
| 449 } |
| 450 static bool IsBlockedForSafepoint(uint32_t state) { |
| 451 return BlockedForSafepointField::decode(state); |
| 452 } |
| 453 bool IsBlockedForSafepoint() const { |
| 454 return BlockedForSafepointField::decode(safepoint_state_); |
| 455 } |
| 456 void SetBlockedForSafepoint(bool value) { |
| 457 ASSERT(thread_lock()->IsOwnedByCurrentThread()); |
| 458 safepoint_state_ = |
| 459 BlockedForSafepointField::update(value, safepoint_state_); |
| 460 } |
| 461 |
| 462 enum ExecutionState { |
| 463 kThreadInVM = 0, |
| 464 kThreadInGenerated, |
| 465 kThreadInNative, |
| 466 kThreadInBlockedState |
| 467 }; |
| 468 |
| 469 ExecutionState execution_state() const { |
| 470 return static_cast<ExecutionState>(execution_state_); |
| 471 } |
| 472 void set_execution_state(ExecutionState state) { |
| 473 execution_state_ = static_cast<uint32_t>(state); |
| 474 } |
| 475 static intptr_t execution_state_offset() { |
| 476 return OFFSET_OF(Thread, execution_state_); |
| 477 } |
| 478 |
| 479 void EnterSafepoint() { |
| 480 // First try a fast update of the thread state to indicate it is at a |
| 481 // safepoint. |
| 482 uint32_t new_state = SetAtSafepoint(true, 0); |
| 483 uword addr = reinterpret_cast<uword>(this) + safepoint_state_offset(); |
| 484 if (AtomicOperations::CompareAndSwapUint32( |
| 485 reinterpret_cast<uint32_t*>(addr), 0, new_state) != 0) { |
| 486 // Fast update failed which means we could potentially be in the middle |
| 487 // of a safepoint operation. |
| 488 EnterSafepointUsingLock(); |
| 489 } |
| 490 } |
| 491 |
| 492 void ExitSafepoint() { |
| 493 // First try a fast update of the thread state to indicate it is not at a |
| 494 // safepoint anymore. |
| 495 uint32_t old_state = SetAtSafepoint(true, 0); |
| 496 uword addr = reinterpret_cast<uword>(this) + safepoint_state_offset(); |
| 497 if (AtomicOperations::CompareAndSwapUint32( |
| 498 reinterpret_cast<uint32_t*>(addr), old_state, 0) != old_state) { |
| 499 // Fast update failed which means we could potentially be in the middle |
| 500 // of a safepoint operation. |
| 501 ExitSafepointUsingLock(); |
| 502 } |
| 503 } |
| 504 |
| 505 void CheckForSafepoint() { |
| 506 if (IsSafepointRequested()) { |
| 507 BlockForSafepoint(); |
| 508 } |
| 509 } |
| 510 |
| 511 Thread* next() const { return next_; } |
389 | 512 |
390 // Visit all object pointers. | 513 // Visit all object pointers. |
391 void VisitObjectPointers(ObjectPointerVisitor* visitor); | 514 void VisitObjectPointers(ObjectPointerVisitor* visitor); |
392 | 515 |
393 bool IsValidLocalHandle(Dart_Handle object) const; | 516 bool IsValidLocalHandle(Dart_Handle object) const; |
394 int CountLocalHandles() const; | 517 int CountLocalHandles() const; |
395 int ZoneSizeInBytes() const; | 518 int ZoneSizeInBytes() const; |
396 void UnwindScopes(uword stack_marker); | 519 void UnwindScopes(uword stack_marker); |
397 | 520 |
398 void InitVMConstants(); | 521 void InitVMConstants(); |
399 | 522 |
400 private: | 523 private: |
401 template<class T> T* AllocateReusableHandle(); | 524 template<class T> T* AllocateReusableHandle(); |
402 | 525 |
403 OSThread* os_thread_; | 526 OSThread* os_thread_; |
| 527 Monitor* thread_lock_; |
404 Isolate* isolate_; | 528 Isolate* isolate_; |
405 Heap* heap_; | 529 Heap* heap_; |
406 Zone* zone_; | 530 Zone* zone_; |
407 ApiLocalScope* api_reusable_scope_; | 531 ApiLocalScope* api_reusable_scope_; |
408 ApiLocalScope* api_top_scope_; | 532 ApiLocalScope* api_top_scope_; |
409 uword top_exit_frame_info_; | 533 uword top_exit_frame_info_; |
410 StackResource* top_resource_; | 534 StackResource* top_resource_; |
411 LongJumpScope* long_jump_base_; | 535 LongJumpScope* long_jump_base_; |
412 StoreBufferBlock* store_buffer_block_; | 536 StoreBufferBlock* store_buffer_block_; |
413 int32_t no_callback_scope_depth_; | 537 int32_t no_callback_scope_depth_; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_FIELDS) | 570 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_FIELDS) |
447 #undef REUSABLE_HANDLE_FIELDS | 571 #undef REUSABLE_HANDLE_FIELDS |
448 | 572 |
449 #if defined(DEBUG) | 573 #if defined(DEBUG) |
450 #define REUSABLE_HANDLE_SCOPE_VARIABLE(object) \ | 574 #define REUSABLE_HANDLE_SCOPE_VARIABLE(object) \ |
451 bool reusable_##object##_handle_scope_active_; | 575 bool reusable_##object##_handle_scope_active_; |
452 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_SCOPE_VARIABLE); | 576 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_SCOPE_VARIABLE); |
453 #undef REUSABLE_HANDLE_SCOPE_VARIABLE | 577 #undef REUSABLE_HANDLE_SCOPE_VARIABLE |
454 #endif // defined(DEBUG) | 578 #endif // defined(DEBUG) |
455 | 579 |
| 580 class AtSafepointField : public BitField<bool, 0, 1> {}; |
| 581 class SafepointRequestedField : public BitField<bool, 1, 1> {}; |
| 582 class BlockedForSafepointField : public BitField<bool, 2, 1> {}; |
| 583 uint32_t safepoint_state_; |
| 584 uint32_t execution_state_; |
| 585 |
456 Thread* next_; // Used to chain the thread structures in an isolate. | 586 Thread* next_; // Used to chain the thread structures in an isolate. |
457 | 587 |
458 explicit Thread(Isolate* isolate); | 588 explicit Thread(Isolate* isolate); |
459 | 589 |
460 void StoreBufferRelease( | 590 void StoreBufferRelease( |
461 StoreBuffer::ThresholdPolicy policy = StoreBuffer::kCheckThreshold); | 591 StoreBuffer::ThresholdPolicy policy = StoreBuffer::kCheckThreshold); |
462 void StoreBufferAcquire(); | 592 void StoreBufferAcquire(); |
463 | 593 |
464 void set_zone(Zone* zone) { | 594 void set_zone(Zone* zone) { |
465 zone_ = zone; | 595 zone_ = zone; |
466 } | 596 } |
467 | 597 |
468 void set_top_exit_frame_info(uword top_exit_frame_info) { | 598 void set_top_exit_frame_info(uword top_exit_frame_info) { |
469 top_exit_frame_info_ = top_exit_frame_info; | 599 top_exit_frame_info_ = top_exit_frame_info; |
470 } | 600 } |
471 | 601 |
| 602 void set_safepoint_state(uint32_t value) { |
| 603 safepoint_state_ = value; |
| 604 } |
| 605 void EnterSafepointUsingLock(); |
| 606 void ExitSafepointUsingLock(); |
| 607 void BlockForSafepoint(); |
| 608 |
472 static void SetCurrent(Thread* current) { | 609 static void SetCurrent(Thread* current) { |
473 OSThread::SetCurrentTLS(reinterpret_cast<uword>(current)); | 610 OSThread::SetCurrentTLS(reinterpret_cast<uword>(current)); |
474 } | 611 } |
475 | 612 |
476 #define REUSABLE_FRIEND_DECLARATION(name) \ | 613 #define REUSABLE_FRIEND_DECLARATION(name) \ |
477 friend class Reusable##name##HandleScope; | 614 friend class Reusable##name##HandleScope; |
478 REUSABLE_HANDLE_LIST(REUSABLE_FRIEND_DECLARATION) | 615 REUSABLE_HANDLE_LIST(REUSABLE_FRIEND_DECLARATION) |
479 #undef REUSABLE_FRIEND_DECLARATION | 616 #undef REUSABLE_FRIEND_DECLARATION |
480 | 617 |
481 friend class ApiZone; | 618 friend class ApiZone; |
(...skipping 15 matching lines...) Expand all Loading... |
497 // Disable thread interrupts. | 634 // Disable thread interrupts. |
498 class DisableThreadInterruptsScope : public StackResource { | 635 class DisableThreadInterruptsScope : public StackResource { |
499 public: | 636 public: |
500 explicit DisableThreadInterruptsScope(Thread* thread); | 637 explicit DisableThreadInterruptsScope(Thread* thread); |
501 ~DisableThreadInterruptsScope(); | 638 ~DisableThreadInterruptsScope(); |
502 }; | 639 }; |
503 | 640 |
504 } // namespace dart | 641 } // namespace dart |
505 | 642 |
506 #endif // VM_THREAD_H_ | 643 #endif // VM_THREAD_H_ |
OLD | NEW |