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 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 // Otherwise returns false. | 446 // Otherwise returns false. |
447 static bool HasLazyCommits(); | 447 static bool HasLazyCommits(); |
448 | 448 |
449 private: | 449 private: |
450 void* address_; // Start address of the virtual memory. | 450 void* address_; // Start address of the virtual memory. |
451 size_t size_; // Size of the virtual memory. | 451 size_t size_; // Size of the virtual memory. |
452 }; | 452 }; |
453 | 453 |
454 | 454 |
455 // ---------------------------------------------------------------------------- | 455 // ---------------------------------------------------------------------------- |
| 456 // Semaphore |
| 457 // |
| 458 // A semaphore object is a synchronization object that maintains a count. The |
| 459 // count is decremented each time a thread completes a wait for the semaphore |
| 460 // object and incremented each time a thread signals the semaphore. When the |
| 461 // count reaches zero, threads waiting for the semaphore blocks until the |
| 462 // count becomes non-zero. |
| 463 |
| 464 class Semaphore { |
| 465 public: |
| 466 virtual ~Semaphore() {} |
| 467 |
| 468 // Suspends the calling thread until the semaphore counter is non zero |
| 469 // and then decrements the semaphore counter. |
| 470 virtual void Wait() = 0; |
| 471 |
| 472 // Suspends the calling thread until the counter is non zero or the timeout |
| 473 // time has passed. If timeout happens the return value is false and the |
| 474 // counter is unchanged. Otherwise the semaphore counter is decremented and |
| 475 // true is returned. The timeout value is specified in microseconds. |
| 476 virtual bool Wait(int timeout) = 0; |
| 477 |
| 478 // Increments the semaphore counter. |
| 479 virtual void Signal() = 0; |
| 480 }; |
| 481 |
| 482 template <int InitialValue> |
| 483 struct CreateSemaphoreTrait { |
| 484 static Semaphore* Create() { |
| 485 return OS::CreateSemaphore(InitialValue); |
| 486 } |
| 487 }; |
| 488 |
| 489 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called). |
| 490 // Usage: |
| 491 // // The following semaphore starts at 0. |
| 492 // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER; |
| 493 // |
| 494 // void my_function() { |
| 495 // // Do something with my_semaphore.Pointer(). |
| 496 // } |
| 497 // |
| 498 template <int InitialValue> |
| 499 struct LazySemaphore { |
| 500 typedef typename LazyDynamicInstance< |
| 501 Semaphore, CreateSemaphoreTrait<InitialValue>, |
| 502 ThreadSafeInitOnceTrait>::type type; |
| 503 }; |
| 504 |
| 505 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER |
| 506 |
| 507 |
| 508 // ---------------------------------------------------------------------------- |
456 // Thread | 509 // Thread |
457 // | 510 // |
458 // Thread objects are used for creating and running threads. When the start() | 511 // Thread objects are used for creating and running threads. When the start() |
459 // method is called the new thread starts running the run() method in the new | 512 // method is called the new thread starts running the run() method in the new |
460 // thread. The Thread object should not be deallocated before the thread has | 513 // thread. The Thread object should not be deallocated before the thread has |
461 // terminated. | 514 // terminated. |
462 | 515 |
463 class Thread { | 516 class Thread { |
464 public: | 517 public: |
465 // Opaque data type for thread-local storage keys. | 518 // Opaque data type for thread-local storage keys. |
(...skipping 16 matching lines...) Expand all Loading... |
482 | 535 |
483 private: | 536 private: |
484 const char* name_; | 537 const char* name_; |
485 int stack_size_; | 538 int stack_size_; |
486 }; | 539 }; |
487 | 540 |
488 // Create new thread. | 541 // Create new thread. |
489 explicit Thread(const Options& options); | 542 explicit Thread(const Options& options); |
490 virtual ~Thread(); | 543 virtual ~Thread(); |
491 | 544 |
492 // Start new thread by calling the Run() method in the new thread. | 545 // Start new thread by calling the Run() method on the new thread. |
493 void Start(); | 546 void Start(); |
494 | 547 |
| 548 // Start new thread and wait until Run() method is called on the new thread. |
| 549 void StartSynchronously() { |
| 550 start_semaphore_ = OS::CreateSemaphore(0); |
| 551 Start(); |
| 552 start_semaphore_->Wait(); |
| 553 delete start_semaphore_; |
| 554 start_semaphore_ = NULL; |
| 555 } |
| 556 |
495 // Wait until thread terminates. | 557 // Wait until thread terminates. |
496 void Join(); | 558 void Join(); |
497 | 559 |
498 inline const char* name() const { | 560 inline const char* name() const { |
499 return name_; | 561 return name_; |
500 } | 562 } |
501 | 563 |
502 // Abstract method for run handler. | 564 // Abstract method for run handler. |
503 virtual void Run() = 0; | 565 virtual void Run() = 0; |
504 | 566 |
(...skipping 29 matching lines...) Expand all Loading... |
534 static void YieldCPU(); | 596 static void YieldCPU(); |
535 | 597 |
536 | 598 |
537 // The thread name length is limited to 16 based on Linux's implementation of | 599 // The thread name length is limited to 16 based on Linux's implementation of |
538 // prctl(). | 600 // prctl(). |
539 static const int kMaxThreadNameLength = 16; | 601 static const int kMaxThreadNameLength = 16; |
540 | 602 |
541 class PlatformData; | 603 class PlatformData; |
542 PlatformData* data() { return data_; } | 604 PlatformData* data() { return data_; } |
543 | 605 |
| 606 void NotifyStartedAndRun() { |
| 607 if (start_semaphore_) start_semaphore_->Signal(); |
| 608 Run(); |
| 609 } |
| 610 |
544 private: | 611 private: |
545 void set_name(const char* name); | 612 void set_name(const char* name); |
546 | 613 |
547 PlatformData* data_; | 614 PlatformData* data_; |
548 | 615 |
549 char name_[kMaxThreadNameLength]; | 616 char name_[kMaxThreadNameLength]; |
550 int stack_size_; | 617 int stack_size_; |
| 618 Semaphore* start_semaphore_; |
551 | 619 |
552 DISALLOW_COPY_AND_ASSIGN(Thread); | 620 DISALLOW_COPY_AND_ASSIGN(Thread); |
553 }; | 621 }; |
554 | 622 |
555 | 623 |
556 // ---------------------------------------------------------------------------- | 624 // ---------------------------------------------------------------------------- |
557 // Mutex | 625 // Mutex |
558 // | 626 // |
559 // Mutexes are used for serializing access to non-reentrant sections of code. | 627 // Mutexes are used for serializing access to non-reentrant sections of code. |
560 // The implementations of mutex should allow for nested/recursive locking. | 628 // The implementations of mutex should allow for nested/recursive locking. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 mutex_->Unlock(); | 681 mutex_->Unlock(); |
614 } | 682 } |
615 | 683 |
616 private: | 684 private: |
617 Mutex* mutex_; | 685 Mutex* mutex_; |
618 DISALLOW_COPY_AND_ASSIGN(ScopedLock); | 686 DISALLOW_COPY_AND_ASSIGN(ScopedLock); |
619 }; | 687 }; |
620 | 688 |
621 | 689 |
622 // ---------------------------------------------------------------------------- | 690 // ---------------------------------------------------------------------------- |
623 // Semaphore | |
624 // | |
625 // A semaphore object is a synchronization object that maintains a count. The | |
626 // count is decremented each time a thread completes a wait for the semaphore | |
627 // object and incremented each time a thread signals the semaphore. When the | |
628 // count reaches zero, threads waiting for the semaphore blocks until the | |
629 // count becomes non-zero. | |
630 | |
631 class Semaphore { | |
632 public: | |
633 virtual ~Semaphore() {} | |
634 | |
635 // Suspends the calling thread until the semaphore counter is non zero | |
636 // and then decrements the semaphore counter. | |
637 virtual void Wait() = 0; | |
638 | |
639 // Suspends the calling thread until the counter is non zero or the timeout | |
640 // time has passed. If timeout happens the return value is false and the | |
641 // counter is unchanged. Otherwise the semaphore counter is decremented and | |
642 // true is returned. The timeout value is specified in microseconds. | |
643 virtual bool Wait(int timeout) = 0; | |
644 | |
645 // Increments the semaphore counter. | |
646 virtual void Signal() = 0; | |
647 }; | |
648 | |
649 template <int InitialValue> | |
650 struct CreateSemaphoreTrait { | |
651 static Semaphore* Create() { | |
652 return OS::CreateSemaphore(InitialValue); | |
653 } | |
654 }; | |
655 | |
656 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called). | |
657 // Usage: | |
658 // // The following semaphore starts at 0. | |
659 // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER; | |
660 // | |
661 // void my_function() { | |
662 // // Do something with my_semaphore.Pointer(). | |
663 // } | |
664 // | |
665 template <int InitialValue> | |
666 struct LazySemaphore { | |
667 typedef typename LazyDynamicInstance< | |
668 Semaphore, CreateSemaphoreTrait<InitialValue>, | |
669 ThreadSafeInitOnceTrait>::type type; | |
670 }; | |
671 | |
672 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER | |
673 | |
674 | |
675 // ---------------------------------------------------------------------------- | |
676 // Socket | 691 // Socket |
677 // | 692 // |
678 | 693 |
679 class Socket { | 694 class Socket { |
680 public: | 695 public: |
681 virtual ~Socket() {} | 696 virtual ~Socket() {} |
682 | 697 |
683 // Server initialization. | 698 // Server initialization. |
684 virtual bool Bind(const int port) = 0; | 699 virtual bool Bind(const int port) = 0; |
685 virtual bool Listen(int backlog) const = 0; | 700 virtual bool Listen(int backlog) const = 0; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 Atomic32 active_; | 813 Atomic32 active_; |
799 PlatformData* data_; // Platform specific data. | 814 PlatformData* data_; // Platform specific data. |
800 int samples_taken_; // Counts stack samples taken. | 815 int samples_taken_; // Counts stack samples taken. |
801 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); | 816 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
802 }; | 817 }; |
803 | 818 |
804 | 819 |
805 } } // namespace v8::internal | 820 } } // namespace v8::internal |
806 | 821 |
807 #endif // V8_PLATFORM_H_ | 822 #endif // V8_PLATFORM_H_ |
OLD | NEW |