Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: base/shared_memory.h

Issue 21063: Transition POSIX shmem to use lockf(), not semaphores. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/shared_memory_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 7
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 9
10 #if defined(OS_POSIX) 10 #if defined(OS_POSIX)
11 #include <semaphore.h> 11 #include <semaphore.h>
12 #endif 12 #endif
13 #include <string> 13 #include <string>
14 14
15 #include "base/basictypes.h" 15 #include "base/basictypes.h"
16 #include "base/process.h" 16 #include "base/process.h"
17 17
18 namespace base { 18 namespace base {
19 19
20 // SharedMemoryHandle is a platform specific type which represents 20 // SharedMemoryHandle is a platform specific type which represents
21 // the underlying OS handle to a shared memory segment. 21 // the underlying OS handle to a shared memory segment.
22 #if defined(OS_WIN) 22 #if defined(OS_WIN)
23 typedef HANDLE SharedMemoryHandle; 23 typedef HANDLE SharedMemoryHandle;
24 typedef HANDLE SharedMemoryLock; 24 typedef HANDLE SharedMemoryLock;
25 #elif defined(OS_POSIX) 25 #elif defined(OS_POSIX)
26 typedef int SharedMemoryHandle; 26 typedef int SharedMemoryHandle;
27 // TODO(port): these semaphores can leak if we crash, causing 27 // On POSIX, the lock is implemented as a lockf() on the mapped file,
28 // autobuilder problems. Transition to something easier to clean up 28 // so no additional member (or definition of SharedMemoryLock) is
29 // (e.g. lockf/flock). 29 // needed.
30 // TODO(port): make sure what we transition to is fast enough.
31 typedef sem_t* SharedMemoryLock;
32 #endif 30 #endif
33 31
34 // Platform abstraction for shared memory. Provides a C++ wrapper 32 // Platform abstraction for shared memory. Provides a C++ wrapper
35 // around the OS primitive for a memory mapped file. 33 // around the OS primitive for a memory mapped file.
36 class SharedMemory { 34 class SharedMemory {
37 public: 35 public:
38 // Create a new SharedMemory object. 36 // Create a new SharedMemory object.
39 SharedMemory(); 37 SharedMemory();
40 38
41 // Create a new SharedMemory object from an existing, open 39 // Create a new SharedMemory object from an existing, open
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 // Close(); 114 // Close();
117 // return ok; 115 // return ok;
118 bool GiveToProcess(ProcessHandle process, 116 bool GiveToProcess(ProcessHandle process,
119 SharedMemoryHandle* new_handle) { 117 SharedMemoryHandle* new_handle) {
120 return ShareToProcessCommon(process, new_handle, true); 118 return ShareToProcessCommon(process, new_handle, true);
121 } 119 }
122 120
123 // Lock the shared memory. 121 // Lock the shared memory.
124 // This is a cross-process lock which may be recursively 122 // This is a cross-process lock which may be recursively
125 // locked by the same thread. 123 // locked by the same thread.
124 // TODO(port):
125 // WARNING: on POSIX the lock only works across processes, not
126 // across threads. 2 threads in the same process can both grab the
127 // lock at the same time. There are several solutions for this
128 // (futex, lockf+anon_semaphore) but none are both clean and common
129 // across Mac and Linux.
126 void Lock(); 130 void Lock();
127 131
128 // Release the shared memory lock. 132 // Release the shared memory lock.
129 void Unlock(); 133 void Unlock();
130 134
131 private: 135 private:
132 #if defined(OS_POSIX) 136 #if defined(OS_POSIX)
133 bool CreateOrOpen(const std::wstring &name, int posix_flags, size_t size); 137 bool CreateOrOpen(const std::wstring &name, int posix_flags, size_t size);
134 bool FilenameForMemoryName(const std::wstring &memname, 138 bool FilenameForMemoryName(const std::wstring &memname,
135 std::wstring *filename); 139 std::wstring *filename);
140 void LockOrUnlockCommon(int function);
141
136 #endif 142 #endif
137 bool ShareToProcessCommon(ProcessHandle process, 143 bool ShareToProcessCommon(ProcessHandle process,
138 SharedMemoryHandle* new_handle, 144 SharedMemoryHandle* new_handle,
139 bool close_self); 145 bool close_self);
140 146
141 std::wstring name_; 147 std::wstring name_;
142 SharedMemoryHandle mapped_file_; 148 SharedMemoryHandle mapped_file_;
143 void* memory_; 149 void* memory_;
144 bool read_only_; 150 bool read_only_;
145 size_t max_size_; 151 size_t max_size_;
152 #if !defined(OS_POSIX)
146 SharedMemoryLock lock_; 153 SharedMemoryLock lock_;
147 #if defined(OS_POSIX)
148 std::string sem_name_;
149 #endif 154 #endif
150 155
151 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); 156 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory);
152 }; 157 };
153 158
154 // A helper class that acquires the shared memory lock while 159 // A helper class that acquires the shared memory lock while
155 // the SharedMemoryAutoLock is in scope. 160 // the SharedMemoryAutoLock is in scope.
156 class SharedMemoryAutoLock { 161 class SharedMemoryAutoLock {
157 public: 162 public:
158 explicit SharedMemoryAutoLock(SharedMemory* shared_memory) 163 explicit SharedMemoryAutoLock(SharedMemory* shared_memory)
159 : shared_memory_(shared_memory) { 164 : shared_memory_(shared_memory) {
160 shared_memory_->Lock(); 165 shared_memory_->Lock();
161 } 166 }
162 167
163 ~SharedMemoryAutoLock() { 168 ~SharedMemoryAutoLock() {
164 shared_memory_->Unlock(); 169 shared_memory_->Unlock();
165 } 170 }
166 171
167 private: 172 private:
168 SharedMemory* shared_memory_; 173 SharedMemory* shared_memory_;
169 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); 174 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock);
170 }; 175 };
171 176
172 } // namespace base 177 } // namespace base
173 178
174 #endif // BASE_SHARED_MEMORY_H_ 179 #endif // BASE_SHARED_MEMORY_H_
OLDNEW
« no previous file with comments | « no previous file | base/shared_memory_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698