Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/shared_memory.h" | 5 #include "base/shared_memory.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/lazy_instance.h" | |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 16 #include "base/safe_strerror_posix.h" | 17 #include "base/safe_strerror_posix.h" |
| 18 #include "base/synchronization/lock.h" | |
| 17 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 18 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 19 | 21 |
| 20 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
| 21 #include "base/mac/foundation_util.h" | 23 #include "base/mac/foundation_util.h" |
| 22 #endif // OS_MACOSX | 24 #endif // OS_MACOSX |
| 23 | 25 |
| 24 #if defined(OS_ANDROID) | 26 #if defined(OS_ANDROID) |
| 25 #include "base/os_compat_android.h" | 27 #include "base/os_compat_android.h" |
| 26 #include "third_party/ashmem/ashmem.h" | 28 #include "third_party/ashmem/ashmem.h" |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 namespace base { | 31 namespace base { |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 // Paranoia. Semaphores and shared memory segments should live in different | 35 // Paranoia. Semaphores and shared memory segments should live in different |
| 34 // namespaces, but who knows what's out there. | 36 // namespaces, but who knows what's out there. |
| 35 const char kSemaphoreSuffix[] = "-sem"; | 37 const char kSemaphoreSuffix[] = "-sem"; |
| 36 | 38 |
| 39 LazyInstance<Lock> g_thread_lock_ = LAZY_INSTANCE_INITIALIZER; | |
|
jar (doing other things)
2012/02/25 00:01:39
Should this be a leaky lazy instance? Are you sur
vandebo (ex-Chrome)
2012/02/25 01:50:36
Good point - The existing uses *might* be ok to us
| |
| 40 | |
| 37 } | 41 } |
| 38 | 42 |
| 39 SharedMemory::SharedMemory() | 43 SharedMemory::SharedMemory() |
| 40 : mapped_file_(-1), | 44 : mapped_file_(-1), |
| 41 mapped_size_(0), | 45 mapped_size_(0), |
| 42 inode_(0), | 46 inode_(0), |
| 43 memory_(NULL), | 47 memory_(NULL), |
| 44 read_only_(false), | 48 read_only_(false), |
| 45 created_size_(0) { | 49 created_size_(0) { |
| 46 } | 50 } |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 Unmap(); | 265 Unmap(); |
| 262 | 266 |
| 263 if (mapped_file_ > 0) { | 267 if (mapped_file_ > 0) { |
| 264 if (HANDLE_EINTR(close(mapped_file_)) < 0) | 268 if (HANDLE_EINTR(close(mapped_file_)) < 0) |
| 265 PLOG(ERROR) << "close"; | 269 PLOG(ERROR) << "close"; |
| 266 mapped_file_ = -1; | 270 mapped_file_ = -1; |
| 267 } | 271 } |
| 268 } | 272 } |
| 269 | 273 |
| 270 void SharedMemory::Lock() { | 274 void SharedMemory::Lock() { |
| 275 g_thread_lock_.Get().Acquire(); | |
| 271 LockOrUnlockCommon(F_LOCK); | 276 LockOrUnlockCommon(F_LOCK); |
| 272 } | 277 } |
| 273 | 278 |
| 274 void SharedMemory::Unlock() { | 279 void SharedMemory::Unlock() { |
| 275 LockOrUnlockCommon(F_ULOCK); | 280 LockOrUnlockCommon(F_ULOCK); |
| 281 g_thread_lock_.Get().Release(); | |
| 276 } | 282 } |
| 277 | 283 |
| 278 #if !defined(OS_ANDROID) | 284 #if !defined(OS_ANDROID) |
| 279 bool SharedMemory::PrepareMapFile(FILE *fp) { | 285 bool SharedMemory::PrepareMapFile(FILE *fp) { |
| 280 DCHECK_EQ(-1, mapped_file_); | 286 DCHECK_EQ(-1, mapped_file_); |
| 281 if (fp == NULL) return false; | 287 if (fp == NULL) return false; |
| 282 | 288 |
| 283 // This function theoretically can block on the disk, but realistically | 289 // This function theoretically can block on the disk, but realistically |
| 284 // the temporary files we create will just go into the buffer cache | 290 // the temporary files we create will just go into the buffer cache |
| 285 // and be deleted before they ever make it out to disk. | 291 // and be deleted before they ever make it out to disk. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 new_handle->fd = new_fd; | 366 new_handle->fd = new_fd; |
| 361 new_handle->auto_close = true; | 367 new_handle->auto_close = true; |
| 362 | 368 |
| 363 if (close_self) | 369 if (close_self) |
| 364 Close(); | 370 Close(); |
| 365 | 371 |
| 366 return true; | 372 return true; |
| 367 } | 373 } |
| 368 | 374 |
| 369 } // namespace base | 375 } // namespace base |
| OLD | NEW |