| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef V8_PLATFORM_SEMAPHORE_H_ | 28 #ifndef V8_PLATFORM_SOCKET_H_ |
| 29 #define V8_PLATFORM_SEMAPHORE_H_ | 29 #define V8_PLATFORM_SOCKET_H_ |
| 30 | 30 |
| 31 #include "lazy-instance.h" | 31 #include "globals.h" |
| 32 #if V8_OS_WIN | 32 #if V8_OS_WIN |
| 33 #include "win32-headers.h" | 33 #include "win32-headers.h" |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #if V8_OS_MACOSX | |
| 37 #include <mach/semaphore.h> // NOLINT | |
| 38 #elif V8_OS_POSIX | |
| 39 #include <semaphore.h> // NOLINT | |
| 40 #endif | |
| 41 | |
| 42 namespace v8 { | 36 namespace v8 { |
| 43 namespace internal { | 37 namespace internal { |
| 44 | 38 |
| 45 // Forward declarations. | 39 // ---------------------------------------------------------------------------- |
| 46 class TimeDelta; | 40 // Socket |
| 41 // |
| 47 | 42 |
| 48 // ---------------------------------------------------------------------------- | 43 class Socket V8_FINAL { |
| 49 // Semaphore | 44 public: |
| 50 // | 45 Socket(); |
| 51 // A semaphore object is a synchronization object that maintains a count. The | 46 ~Socket() { Shutdown(); } |
| 52 // count is decremented each time a thread completes a wait for the semaphore | |
| 53 // object and incremented each time a thread signals the semaphore. When the | |
| 54 // count reaches zero, threads waiting for the semaphore blocks until the | |
| 55 // count becomes non-zero. | |
| 56 | 47 |
| 57 class Semaphore V8_FINAL { | 48 // Server initialization. |
| 58 public: | 49 bool Bind(int port) V8_WARN_UNUSED_RESULT; |
| 59 explicit Semaphore(int count); | 50 bool Listen(int backlog) V8_WARN_UNUSED_RESULT; |
| 60 ~Semaphore(); | 51 Socket* Accept() V8_WARN_UNUSED_RESULT; |
| 61 | 52 |
| 62 // Increments the semaphore counter. | 53 // Client initialization. |
| 63 void Signal(); | 54 bool Connect(const char* host, const char* port) V8_WARN_UNUSED_RESULT; |
| 64 | 55 |
| 65 // Suspends the calling thread until the semaphore counter is non zero | 56 // Shutdown socket for both read and write. This causes blocking Send and |
| 66 // and then decrements the semaphore counter. | 57 // Receive calls to exit. After |Shutdown()| the Socket object cannot be |
| 67 void Wait(); | 58 // used for any communication. |
| 59 bool Shutdown(); |
| 68 | 60 |
| 69 // Suspends the calling thread until the counter is non zero or the timeout | 61 // Data Transimission |
| 70 // time has passed. If timeout happens the return value is false and the | 62 // Return 0 on failure. |
| 71 // counter is unchanged. Otherwise the semaphore counter is decremented and | 63 int Send(const char* buffer, int length) V8_WARN_UNUSED_RESULT; |
| 72 // true is returned. | 64 int Receive(char* buffer, int length) V8_WARN_UNUSED_RESULT; |
| 73 bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT; | |
| 74 | 65 |
| 75 #if V8_OS_MACOSX | 66 // Set the value of the SO_REUSEADDR socket option. |
| 76 typedef semaphore_t NativeHandle; | 67 bool SetReuseAddress(bool reuse_address); |
| 77 #elif V8_OS_POSIX | 68 |
| 78 typedef sem_t NativeHandle; | 69 V8_INLINE(bool IsValid()) const V8_WARN_UNUSED_RESULT { |
| 70 return native_handle_ != kInvalidNativeHandle; |
| 71 } |
| 72 |
| 73 static int GetLastError() V8_WARN_UNUSED_RESULT; |
| 74 |
| 75 // The implementation-defined native handle type. |
| 76 #if V8_OS_POSIX |
| 77 typedef int NativeHandle; |
| 78 static const NativeHandle kInvalidNativeHandle = -1; |
| 79 #elif V8_OS_WIN | 79 #elif V8_OS_WIN |
| 80 typedef HANDLE NativeHandle; | 80 typedef SOCKET NativeHandle; |
| 81 static const NativeHandle kInvalidNativeHandle = INVALID_SOCKET; |
| 81 #endif | 82 #endif |
| 82 | 83 |
| 83 NativeHandle& native_handle() V8_WARN_UNUSED_RESULT { | 84 NativeHandle& native_handle() V8_WARN_UNUSED_RESULT { |
| 84 return native_handle_; | 85 return native_handle_; |
| 85 } | 86 } |
| 86 const NativeHandle& native_handle() const V8_WARN_UNUSED_RESULT { | 87 const NativeHandle& native_handle() const V8_WARN_UNUSED_RESULT { |
| 87 return native_handle_; | 88 return native_handle_; |
| 88 } | 89 } |
| 89 | 90 |
| 90 private: | 91 private: |
| 92 explicit Socket(NativeHandle native_handle) : native_handle_(native_handle) {} |
| 93 |
| 91 NativeHandle native_handle_; | 94 NativeHandle native_handle_; |
| 92 | 95 |
| 93 DISALLOW_COPY_AND_ASSIGN(Semaphore); | 96 DISALLOW_COPY_AND_ASSIGN(Socket); |
| 94 }; | 97 }; |
| 95 | 98 |
| 96 | |
| 97 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called). | |
| 98 // Usage: | |
| 99 // // The following semaphore starts at 0. | |
| 100 // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER; | |
| 101 // | |
| 102 // void my_function() { | |
| 103 // // Do something with my_semaphore.Pointer(). | |
| 104 // } | |
| 105 // | |
| 106 | |
| 107 template <int N> | |
| 108 struct CreateSemaphoreTrait { | |
| 109 static Semaphore* Create() { | |
| 110 return new Semaphore(N); | |
| 111 } | |
| 112 }; | |
| 113 | |
| 114 template <int N> | |
| 115 struct LazySemaphore { | |
| 116 typedef typename LazyDynamicInstance< | |
| 117 Semaphore, | |
| 118 CreateSemaphoreTrait<N>, | |
| 119 ThreadSafeInitOnceTrait>::type type; | |
| 120 }; | |
| 121 | |
| 122 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER | |
| 123 | |
| 124 } } // namespace v8::internal | 99 } } // namespace v8::internal |
| 125 | 100 |
| 126 #endif // V8_PLATFORM_SEMAPHORE_H_ | 101 #endif // V8_PLATFORM_SOCKET_H_ |
| OLD | NEW |