diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/shared_memory.h | 13 | ||||
-rw-r--r-- | base/shared_memory_win.cc | 20 |
2 files changed, 31 insertions, 2 deletions
diff --git a/base/shared_memory.h b/base/shared_memory.h index 79ea8fd..719eb69 100644 --- a/base/shared_memory.h +++ b/base/shared_memory.h @@ -43,6 +43,13 @@ class SharedMemory { public: SharedMemory(); +#if defined(OS_WIN) + // Similar to the default constructor, except that this allows for + // calling Lock() to acquire the named mutex before either Create or Open + // are called on Windows. + explicit SharedMemory(const std::wstring& name); +#endif + // Create a new SharedMemory object from an existing, open // shared memory file. SharedMemory(SharedMemoryHandle handle, bool read_only); @@ -165,6 +172,12 @@ class SharedMemory { // across Mac and Linux. void Lock(); +#if defined(OS_WIN) + // A Lock() implementation with a timeout. Returns true if the Lock() has + // been acquired, false if the timeout was reached. + bool Lock(uint32 timeout_ms); +#endif + // Releases the shared memory lock. void Unlock(); diff --git a/base/shared_memory_win.cc b/base/shared_memory_win.cc index a0b2a5a..5f293fc 100644 --- a/base/shared_memory_win.cc +++ b/base/shared_memory_win.cc @@ -17,6 +17,15 @@ SharedMemory::SharedMemory() lock_(NULL) { } +SharedMemory::SharedMemory(const std::wstring& name) + : mapped_file_(NULL), + memory_(NULL), + read_only_(false), + created_size_(0), + lock_(NULL), + name_(name) { +} + SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) : mapped_file_(handle), memory_(NULL), @@ -188,6 +197,10 @@ void SharedMemory::Close() { } void SharedMemory::Lock() { + Lock(INFINITE); +} + +bool SharedMemory::Lock(uint32 timeout_ms) { if (lock_ == NULL) { std::wstring name = name_; name.append(L"lock"); @@ -195,10 +208,13 @@ void SharedMemory::Lock() { DCHECK(lock_ != NULL); if (lock_ == NULL) { DLOG(ERROR) << "Could not create mutex" << GetLastError(); - return; // there is nothing good we can do here. + return false; // there is nothing good we can do here. } } - WaitForSingleObject(lock_, INFINITE); + DWORD result = WaitForSingleObject(lock_, timeout_ms); + + // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED. + return (result == WAIT_OBJECT_0); } void SharedMemory::Unlock() { |