summaryrefslogtreecommitdiffstats
path: root/base/shared_memory_win.cc
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2010-11-18 18:32:45 +0000
committerBen Murdoch <benm@google.com>2010-11-18 18:38:07 +0000
commit513209b27ff55e2841eac0e4120199c23acce758 (patch)
treeaeba30bb08c5f47c57003544e378a377c297eee6 /base/shared_memory_win.cc
parent164f7496de0fbee436b385a79ead9e3cb81a50c1 (diff)
downloadexternal_chromium-513209b27ff55e2841eac0e4120199c23acce758.zip
external_chromium-513209b27ff55e2841eac0e4120199c23acce758.tar.gz
external_chromium-513209b27ff55e2841eac0e4120199c23acce758.tar.bz2
Merge Chromium at r65505: Initial merge by git.
Change-Id: I31d8f1d8cd33caaf7f47ffa7350aef42d5fbdb45
Diffstat (limited to 'base/shared_memory_win.cc')
-rw-r--r--base/shared_memory_win.cc59
1 files changed, 45 insertions, 14 deletions
diff --git a/base/shared_memory_win.cc b/base/shared_memory_win.cc
index f35ada1..5f293fc 100644
--- a/base/shared_memory_win.cc
+++ b/base/shared_memory_win.cc
@@ -13,15 +13,24 @@ SharedMemory::SharedMemory()
: mapped_file_(NULL),
memory_(NULL),
read_only_(false),
- max_size_(0),
+ created_size_(0),
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),
read_only_(read_only),
- max_size_(0),
+ created_size_(0),
lock_(NULL) {
}
@@ -30,7 +39,7 @@ SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
: mapped_file_(NULL),
memory_(NULL),
read_only_(read_only),
- max_size_(0),
+ created_size_(0),
lock_(NULL) {
::DuplicateHandle(process, handle,
GetCurrentProcess(), &mapped_file_,
@@ -61,9 +70,19 @@ void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
::CloseHandle(handle);
}
-bool SharedMemory::Create(const std::string& name, bool read_only,
- bool open_existing, uint32 size) {
+bool SharedMemory::CreateAndMapAnonymous(uint32 size) {
+ return CreateAnonymous(size) && Map(size);
+}
+
+bool SharedMemory::CreateAnonymous(uint32 size) {
+ return CreateNamed("", false, size);
+}
+
+bool SharedMemory::CreateNamed(const std::string& name,
+ bool open_existing, uint32 size) {
DCHECK(mapped_file_ == NULL);
+ if (size == 0)
+ return false;
// NaCl's memory allocator requires 0mod64K alignment and size for
// shared memory objects. To allow passing shared memory to NaCl,
@@ -72,20 +91,25 @@ bool SharedMemory::Create(const std::string& name, bool read_only,
// actual requested size.
uint32 rounded_size = (size + 0xffff) & ~0xffff;
name_ = ASCIIToWide(name);
- read_only_ = read_only;
mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
- read_only_ ? PAGE_READONLY : PAGE_READWRITE, 0,
- static_cast<DWORD>(rounded_size),
+ PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size),
name_.empty() ? NULL : name_.c_str());
if (!mapped_file_)
return false;
+ created_size_ = size;
+
// Check if the shared memory pre-exists.
- if (GetLastError() == ERROR_ALREADY_EXISTS && !open_existing) {
- Close();
- return false;
+ if (GetLastError() == ERROR_ALREADY_EXISTS) {
+ // If the file already existed, set created_size_ to 0 to show that
+ // we don't know the size.
+ created_size_ = 0;
+ if (!open_existing) {
+ Close();
+ return false;
+ }
}
- max_size_ = size;
+
return true;
}
@@ -173,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");
@@ -180,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() {