OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 int strncasecmp(const char* s1, const char* s2, int n); | 72 int strncasecmp(const char* s1, const char* s2, int n); |
73 | 73 |
74 #endif // _MSC_VER | 74 #endif // _MSC_VER |
75 | 75 |
76 // Random is missing on both Visual Studio and MinGW. | 76 // Random is missing on both Visual Studio and MinGW. |
77 int random(); | 77 int random(); |
78 | 78 |
79 #endif // WIN32 | 79 #endif // WIN32 |
80 | 80 |
81 #include "atomicops.h" | 81 #include "atomicops.h" |
| 82 #include "lazy-instance.h" |
82 #include "platform-tls.h" | 83 #include "platform-tls.h" |
83 #include "utils.h" | 84 #include "utils.h" |
84 #include "v8globals.h" | 85 #include "v8globals.h" |
85 | 86 |
86 namespace v8 { | 87 namespace v8 { |
87 namespace internal { | 88 namespace internal { |
88 | 89 |
89 // Use AtomicWord for a machine-sized pointer. It is assumed that | 90 // Use AtomicWord for a machine-sized pointer. It is assumed that |
90 // reads and writes of naturally aligned values of this type are atomic. | 91 // reads and writes of naturally aligned values of this type are atomic. |
91 typedef intptr_t AtomicWord; | 92 typedef intptr_t AtomicWord; |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 | 522 |
522 // Unlocks the given mutex. The mutex is assumed to be locked and owned by | 523 // Unlocks the given mutex. The mutex is assumed to be locked and owned by |
523 // the calling thread on entrance. | 524 // the calling thread on entrance. |
524 virtual int Unlock() = 0; | 525 virtual int Unlock() = 0; |
525 | 526 |
526 // Tries to lock the given mutex. Returns whether the mutex was | 527 // Tries to lock the given mutex. Returns whether the mutex was |
527 // successfully locked. | 528 // successfully locked. |
528 virtual bool TryLock() = 0; | 529 virtual bool TryLock() = 0; |
529 }; | 530 }; |
530 | 531 |
| 532 struct CreateMutexTrait { |
| 533 static Mutex* Create() { |
| 534 return OS::CreateMutex(); |
| 535 } |
| 536 }; |
| 537 |
| 538 // POD Mutex initialized lazily (i.e. the first time Pointer() is called). |
| 539 // Usage: |
| 540 // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER; |
| 541 // |
| 542 // void my_function() { |
| 543 // ScopedLock my_lock(my_mutex.Pointer()); |
| 544 // // Do something. |
| 545 // } |
| 546 // |
| 547 typedef LazyDynamicInstance<Mutex, CreateMutexTrait>::type LazyMutex; |
| 548 |
| 549 #define LAZY_MUTEX_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER |
531 | 550 |
532 // ---------------------------------------------------------------------------- | 551 // ---------------------------------------------------------------------------- |
533 // ScopedLock | 552 // ScopedLock |
534 // | 553 // |
535 // Stack-allocated ScopedLocks provide block-scoped locking and | 554 // Stack-allocated ScopedLocks provide block-scoped locking and |
536 // unlocking of a mutex. | 555 // unlocking of a mutex. |
537 class ScopedLock { | 556 class ScopedLock { |
538 public: | 557 public: |
539 explicit ScopedLock(Mutex* mutex): mutex_(mutex) { | 558 explicit ScopedLock(Mutex* mutex): mutex_(mutex) { |
540 ASSERT(mutex_ != NULL); | 559 ASSERT(mutex_ != NULL); |
(...skipping 29 matching lines...) Expand all Loading... |
570 // Suspends the calling thread until the counter is non zero or the timeout | 589 // Suspends the calling thread until the counter is non zero or the timeout |
571 // time has passed. If timeout happens the return value is false and the | 590 // time has passed. If timeout happens the return value is false and the |
572 // counter is unchanged. Otherwise the semaphore counter is decremented and | 591 // counter is unchanged. Otherwise the semaphore counter is decremented and |
573 // true is returned. The timeout value is specified in microseconds. | 592 // true is returned. The timeout value is specified in microseconds. |
574 virtual bool Wait(int timeout) = 0; | 593 virtual bool Wait(int timeout) = 0; |
575 | 594 |
576 // Increments the semaphore counter. | 595 // Increments the semaphore counter. |
577 virtual void Signal() = 0; | 596 virtual void Signal() = 0; |
578 }; | 597 }; |
579 | 598 |
| 599 template <int InitialValue> |
| 600 struct CreateSemaphoreTrait { |
| 601 static Semaphore* Create() { |
| 602 return OS::CreateSemaphore(InitialValue); |
| 603 } |
| 604 }; |
| 605 |
| 606 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called). |
| 607 // Usage: |
| 608 // // The following semaphore starts at 0. |
| 609 // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER; |
| 610 // |
| 611 // void my_function() { |
| 612 // // Do something with my_semaphore.Pointer(). |
| 613 // } |
| 614 // |
| 615 template <int InitialValue> |
| 616 struct LazySemaphore { |
| 617 typedef typename LazyDynamicInstance< |
| 618 Semaphore, CreateSemaphoreTrait<InitialValue> >::type type; |
| 619 }; |
| 620 |
| 621 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER |
| 622 |
580 | 623 |
581 // ---------------------------------------------------------------------------- | 624 // ---------------------------------------------------------------------------- |
582 // Socket | 625 // Socket |
583 // | 626 // |
584 | 627 |
585 class Socket { | 628 class Socket { |
586 public: | 629 public: |
587 virtual ~Socket() {} | 630 virtual ~Socket() {} |
588 | 631 |
589 // Server initialization. | 632 // Server initialization. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 Atomic32 active_; | 746 Atomic32 active_; |
704 PlatformData* data_; // Platform specific data. | 747 PlatformData* data_; // Platform specific data. |
705 int samples_taken_; // Counts stack samples taken. | 748 int samples_taken_; // Counts stack samples taken. |
706 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); | 749 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
707 }; | 750 }; |
708 | 751 |
709 | 752 |
710 } } // namespace v8::internal | 753 } } // namespace v8::internal |
711 | 754 |
712 #endif // V8_PLATFORM_H_ | 755 #endif // V8_PLATFORM_H_ |
OLD | NEW |