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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 93 |
94 #endif // _MSC_VER | 94 #endif // _MSC_VER |
95 | 95 |
96 #ifndef __CYGWIN__ | 96 #ifndef __CYGWIN__ |
97 // Random is missing on both Visual Studio and MinGW. | 97 // Random is missing on both Visual Studio and MinGW. |
98 int random(); | 98 int random(); |
99 #endif | 99 #endif |
100 | 100 |
101 #endif // WIN32 | 101 #endif // WIN32 |
102 | 102 |
103 #include "mutex.h" | 103 #include "lazy-instance.h" |
104 #include "utils.h" | 104 #include "utils.h" |
105 #include "v8globals.h" | 105 #include "v8globals.h" |
106 | 106 |
107 namespace v8 { | 107 namespace v8 { |
108 namespace internal { | 108 namespace internal { |
109 | 109 |
110 class Semaphore; | 110 class Semaphore; |
111 class Mutex; | 111 class Mutex; |
112 | 112 |
113 double ceiling(double x); | 113 double ceiling(double x); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 static const int kStackWalkError = -1; | 302 static const int kStackWalkError = -1; |
303 static const int kStackWalkMaxNameLen = 256; | 303 static const int kStackWalkMaxNameLen = 256; |
304 static const int kStackWalkMaxTextLen = 256; | 304 static const int kStackWalkMaxTextLen = 256; |
305 struct StackFrame { | 305 struct StackFrame { |
306 void* address; | 306 void* address; |
307 char text[kStackWalkMaxTextLen]; | 307 char text[kStackWalkMaxTextLen]; |
308 }; | 308 }; |
309 | 309 |
310 static int StackWalk(Vector<StackFrame> frames); | 310 static int StackWalk(Vector<StackFrame> frames); |
311 | 311 |
| 312 // Factory method for creating platform dependent Mutex. |
| 313 // Please use delete to reclaim the storage for the returned Mutex. |
| 314 static Mutex* CreateMutex(); |
| 315 |
312 // Factory method for creating platform dependent Semaphore. | 316 // Factory method for creating platform dependent Semaphore. |
313 // Please use delete to reclaim the storage for the returned Semaphore. | 317 // Please use delete to reclaim the storage for the returned Semaphore. |
314 static Semaphore* CreateSemaphore(int count); | 318 static Semaphore* CreateSemaphore(int count); |
315 | 319 |
316 // Factory method for creating platform dependent Socket. | 320 // Factory method for creating platform dependent Socket. |
317 // Please use delete to reclaim the storage for the returned Socket. | 321 // Please use delete to reclaim the storage for the returned Socket. |
318 static Socket* CreateSocket(); | 322 static Socket* CreateSocket(); |
319 | 323 |
320 class MemoryMappedFile { | 324 class MemoryMappedFile { |
321 public: | 325 public: |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 | 709 |
706 char name_[kMaxThreadNameLength]; | 710 char name_[kMaxThreadNameLength]; |
707 int stack_size_; | 711 int stack_size_; |
708 Semaphore* start_semaphore_; | 712 Semaphore* start_semaphore_; |
709 | 713 |
710 DISALLOW_COPY_AND_ASSIGN(Thread); | 714 DISALLOW_COPY_AND_ASSIGN(Thread); |
711 }; | 715 }; |
712 | 716 |
713 | 717 |
714 // ---------------------------------------------------------------------------- | 718 // ---------------------------------------------------------------------------- |
| 719 // Mutex |
| 720 // |
| 721 // Mutexes are used for serializing access to non-reentrant sections of code. |
| 722 // The implementations of mutex should allow for nested/recursive locking. |
| 723 |
| 724 class Mutex { |
| 725 public: |
| 726 virtual ~Mutex() {} |
| 727 |
| 728 // Locks the given mutex. If the mutex is currently unlocked, it becomes |
| 729 // locked and owned by the calling thread, and immediately. If the mutex |
| 730 // is already locked by another thread, suspends the calling thread until |
| 731 // the mutex is unlocked. |
| 732 virtual int Lock() = 0; |
| 733 |
| 734 // Unlocks the given mutex. The mutex is assumed to be locked and owned by |
| 735 // the calling thread on entrance. |
| 736 virtual int Unlock() = 0; |
| 737 |
| 738 // Tries to lock the given mutex. Returns whether the mutex was |
| 739 // successfully locked. |
| 740 virtual bool TryLock() = 0; |
| 741 }; |
| 742 |
| 743 struct CreateMutexTrait { |
| 744 static Mutex* Create() { |
| 745 return OS::CreateMutex(); |
| 746 } |
| 747 }; |
| 748 |
| 749 // POD Mutex initialized lazily (i.e. the first time Pointer() is called). |
| 750 // Usage: |
| 751 // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER; |
| 752 // |
| 753 // void my_function() { |
| 754 // ScopedLock my_lock(my_mutex.Pointer()); |
| 755 // // Do something. |
| 756 // } |
| 757 // |
| 758 typedef LazyDynamicInstance< |
| 759 Mutex, CreateMutexTrait, ThreadSafeInitOnceTrait>::type LazyMutex; |
| 760 |
| 761 #define LAZY_MUTEX_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER |
| 762 |
| 763 // ---------------------------------------------------------------------------- |
| 764 // ScopedLock |
| 765 // |
| 766 // Stack-allocated ScopedLocks provide block-scoped locking and |
| 767 // unlocking of a mutex. |
| 768 class ScopedLock { |
| 769 public: |
| 770 explicit ScopedLock(Mutex* mutex): mutex_(mutex) { |
| 771 ASSERT(mutex_ != NULL); |
| 772 mutex_->Lock(); |
| 773 } |
| 774 ~ScopedLock() { |
| 775 mutex_->Unlock(); |
| 776 } |
| 777 |
| 778 private: |
| 779 Mutex* mutex_; |
| 780 DISALLOW_COPY_AND_ASSIGN(ScopedLock); |
| 781 }; |
| 782 |
| 783 |
| 784 // ---------------------------------------------------------------------------- |
715 // Socket | 785 // Socket |
716 // | 786 // |
717 | 787 |
718 class Socket { | 788 class Socket { |
719 public: | 789 public: |
720 virtual ~Socket() {} | 790 virtual ~Socket() {} |
721 | 791 |
722 // Server initialization. | 792 // Server initialization. |
723 virtual bool Bind(const int port) = 0; | 793 virtual bool Bind(const int port) = 0; |
724 virtual bool Listen(int backlog) const = 0; | 794 virtual bool Listen(int backlog) const = 0; |
(...skipping 22 matching lines...) Expand all Loading... |
747 static uint16_t HToN(uint16_t value); | 817 static uint16_t HToN(uint16_t value); |
748 static uint16_t NToH(uint16_t value); | 818 static uint16_t NToH(uint16_t value); |
749 static uint32_t HToN(uint32_t value); | 819 static uint32_t HToN(uint32_t value); |
750 static uint32_t NToH(uint32_t value); | 820 static uint32_t NToH(uint32_t value); |
751 }; | 821 }; |
752 | 822 |
753 | 823 |
754 } } // namespace v8::internal | 824 } } // namespace v8::internal |
755 | 825 |
756 #endif // V8_PLATFORM_H_ | 826 #endif // V8_PLATFORM_H_ |
OLD | NEW |