CrossProcessSemaphore.h (3179B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_CrossProcessSemaphore_h 8 #define mozilla_CrossProcessSemaphore_h 9 10 #include "base/process.h" 11 #include "mozilla/TimeStamp.h" 12 #include "mozilla/Maybe.h" 13 14 #if defined(XP_WIN) || defined(XP_DARWIN) 15 # include "mozilla/UniquePtrExtensions.h" 16 #else 17 # include <pthread.h> 18 # include <semaphore.h> 19 # include "mozilla/ipc/SharedMemoryMapping.h" 20 # include "mozilla/Atomics.h" 21 #endif 22 23 namespace IPC { 24 template <typename T> 25 struct ParamTraits; 26 } // namespace IPC 27 28 // 29 // Provides: 30 // 31 // - CrossProcessSemaphore, a semaphore that can be shared across processes 32 namespace mozilla { 33 34 template <typename T> 35 inline bool IsHandleValid(const T& handle) { 36 return bool(handle); 37 } 38 39 #if defined(XP_WIN) 40 typedef mozilla::UniqueFileHandle CrossProcessSemaphoreHandle; 41 #elif defined(XP_DARWIN) 42 typedef mozilla::UniqueMachSendRight CrossProcessSemaphoreHandle; 43 #else 44 typedef mozilla::ipc::MutableSharedMemoryHandle CrossProcessSemaphoreHandle; 45 #endif 46 47 class CrossProcessSemaphore { 48 public: 49 /** 50 * CrossProcessSemaphore 51 * @param name A name which can reference this lock (currently unused) 52 **/ 53 static CrossProcessSemaphore* Create(const char* aName, 54 uint32_t aInitialValue); 55 56 /** 57 * CrossProcessSemaphore 58 * @param handle A handle of an existing cross process semaphore that can be 59 * opened. 60 */ 61 static CrossProcessSemaphore* Create(CrossProcessSemaphoreHandle aHandle); 62 63 ~CrossProcessSemaphore(); 64 65 /** 66 * Decrements the current value of the semaphore. This will block if the 67 * current value of the semaphore is 0, up to a maximum of aWaitTime (if 68 * specified). 69 * Returns true if the semaphore was succesfully decremented, false otherwise. 70 **/ 71 bool Wait(const Maybe<TimeDuration>& aWaitTime = Nothing()); 72 73 /** 74 * Increments the current value of the semaphore. 75 **/ 76 void Signal(); 77 78 /** 79 * CloneHandle 80 * This function is called to generate a serializable structure that can 81 * be sent to the specified process and opened on the other side. 82 * 83 * @returns A handle that can be shared to another process 84 */ 85 CrossProcessSemaphoreHandle CloneHandle(); 86 87 void CloseHandle(); 88 89 private: 90 friend struct IPC::ParamTraits<CrossProcessSemaphore>; 91 92 CrossProcessSemaphore(); 93 CrossProcessSemaphore(const CrossProcessSemaphore&); 94 CrossProcessSemaphore& operator=(const CrossProcessSemaphore&); 95 96 #if defined(XP_WIN) 97 explicit CrossProcessSemaphore(HANDLE aSemaphore); 98 99 HANDLE mSemaphore; 100 #elif defined(XP_DARWIN) 101 explicit CrossProcessSemaphore(CrossProcessSemaphoreHandle aSemaphore); 102 103 CrossProcessSemaphoreHandle mSemaphore; 104 #else 105 mozilla::ipc::MutableSharedMemoryHandle mHandle; 106 mozilla::ipc::SharedMemoryMapping mSharedBuffer; 107 sem_t* mSemaphore; 108 mozilla::Atomic<int32_t>* mRefCount; 109 #endif 110 }; 111 112 } // namespace mozilla 113 114 #endif