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 #ifndef BASE_SHARED_MEMORY_H_ | 5 #ifndef BASE_SHARED_MEMORY_H_ |
| 6 #define BASE_SHARED_MEMORY_H_ | 6 #define BASE_SHARED_MEMORY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 | 10 |
| 11 #include <string> | |
| 12 | |
| 11 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 12 #include <sys/types.h> | 14 #include <sys/types.h> |
| 13 #include <semaphore.h> | 15 #include <semaphore.h> |
| 14 #include "base/file_descriptor_posix.h" | |
| 15 #endif | 16 #endif |
| 16 #include <string> | |
| 17 | 17 |
| 18 #include "base/base_export.h" | 18 #include "base/base_export.h" |
| 19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/process.h" | 20 #include "base/process.h" |
| 21 | 21 |
| 22 #if defined(OS_POSIX) | |
| 23 #include "base/file_descriptor_posix.h" | |
| 24 #endif | |
| 25 | |
| 22 class FilePath; | 26 class FilePath; |
| 23 | 27 |
| 24 namespace base { | 28 namespace base { |
| 25 | 29 |
| 26 // SharedMemoryHandle is a platform specific type which represents | 30 // SharedMemoryHandle is a platform specific type which represents |
| 27 // the underlying OS handle to a shared memory segment. | 31 // the underlying OS handle to a shared memory segment. |
| 28 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
| 29 typedef HANDLE SharedMemoryHandle; | 33 typedef HANDLE SharedMemoryHandle; |
| 30 typedef HANDLE SharedMemoryLock; | 34 typedef HANDLE SharedMemoryLock; |
| 31 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 // Note that the memory is unmapped by calling this method, regardless of the | 199 // Note that the memory is unmapped by calling this method, regardless of the |
| 196 // return value. | 200 // return value. |
| 197 bool GiveToProcess(ProcessHandle process, | 201 bool GiveToProcess(ProcessHandle process, |
| 198 SharedMemoryHandle* new_handle) { | 202 SharedMemoryHandle* new_handle) { |
| 199 return ShareToProcessCommon(process, new_handle, true); | 203 return ShareToProcessCommon(process, new_handle, true); |
| 200 } | 204 } |
| 201 | 205 |
| 202 // Locks the shared memory. | 206 // Locks the shared memory. |
| 203 // This is a cross-process lock which may be recursively | 207 // This is a cross-process lock which may be recursively |
| 204 // locked by the same thread. | 208 // locked by the same thread. |
| 205 // TODO(port): | 209 // |
| 206 // WARNING: on POSIX the lock only works across processes, not | 210 // WARNING: on POSIX the memory locking primitive only works across |
| 207 // across threads. 2 threads in the same process can both grab the | 211 // processes, not across threads. The Lock method is not currently |
| 208 // lock at the same time. There are several solutions for this | 212 // used in inner loops, so we protect against multiple threads in a |
| 209 // (futex, lockf+anon_semaphore) but none are both clean and common | 213 // critical section using a class global lock. |
|
jar (doing other things)
2012/02/25 00:01:39
Since a class-global-lock probably does not allow
vandebo (ex-Chrome)
2012/02/25 01:50:36
Done.
| |
| 210 // across Mac and Linux. | |
| 211 void Lock(); | 214 void Lock(); |
| 212 | 215 |
| 213 #if defined(OS_WIN) | 216 #if defined(OS_WIN) |
| 214 // A Lock() implementation with a timeout that also allows setting | 217 // A Lock() implementation with a timeout that also allows setting |
| 215 // security attributes on the mutex. sec_attr may be NULL. | 218 // security attributes on the mutex. sec_attr may be NULL. |
| 216 // Returns true if the Lock() has been acquired, false if the timeout was | 219 // Returns true if the Lock() has been acquired, false if the timeout was |
| 217 // reached. | 220 // reached. |
| 218 bool Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr); | 221 bool Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr); |
| 219 #endif | 222 #endif |
| 220 | 223 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 } | 266 } |
| 264 | 267 |
| 265 private: | 268 private: |
| 266 SharedMemory* shared_memory_; | 269 SharedMemory* shared_memory_; |
| 267 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); | 270 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); |
| 268 }; | 271 }; |
| 269 | 272 |
| 270 } // namespace base | 273 } // namespace base |
| 271 | 274 |
| 272 #endif // BASE_SHARED_MEMORY_H_ | 275 #endif // BASE_SHARED_MEMORY_H_ |
| OLD | NEW |