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

Unified Diff: base/shared_memory_posix.cc

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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/shared_memory.h ('k') | base/shared_memory_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/shared_memory_posix.cc
===================================================================
--- base/shared_memory_posix.cc (revision 9116)
+++ base/shared_memory_posix.cc (working copy)
@@ -10,6 +10,7 @@
#include "base/file_util.h"
#include "base/logging.h"
+#include "base/platform_thread.h"
#include "base/string_util.h"
namespace base {
@@ -24,16 +25,14 @@
: mapped_file_(-1),
memory_(NULL),
read_only_(false),
- max_size_(0),
- lock_(NULL) {
+ max_size_(0) {
}
SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
: mapped_file_(handle),
memory_(NULL),
read_only_(read_only),
- max_size_(0),
- lock_(NULL) {
+ max_size_(0) {
}
SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
@@ -41,8 +40,7 @@
: mapped_file_(handle),
memory_(NULL),
read_only_(read_only),
- max_size_(0),
- lock_(NULL) {
+ max_size_(0) {
// We don't handle this case yet (note the ignored parameter); let's die if
// someone comes calling.
NOTREACHED();
@@ -50,8 +48,6 @@
SharedMemory::~SharedMemory() {
Close();
- if (lock_ != NULL)
- sem_close(lock_);
}
bool SharedMemory::Create(const std::wstring &name, bool read_only,
@@ -205,19 +201,6 @@
mapped_file_ = dup(fileno(fp));
DCHECK(mapped_file_ >= 0);
-
- mode_t posix_mode = S_IRUSR | S_IWUSR; // owner read/write
- // name_ that includes a tmpdir easily exceeds SEM_NAME_LEN,
- // so we cannot use it directly as the basis for a sem_name_.
- sem_name_ = WideToUTF8(name) + kSemaphoreSuffix;
- lock_ = sem_open(sem_name_.c_str(), O_CREAT, posix_mode, 1);
- if (lock_ == SEM_FAILED) {
- close(mapped_file_);
- mapped_file_ = -1;
- lock_ = NULL;
- return false;
- }
-
return true;
}
@@ -263,24 +246,33 @@
close(mapped_file_);
mapped_file_ = -1;
}
+}
- if (lock_) {
- sem_unlink(sem_name_.c_str());
- lock_ = NULL;
+void SharedMemory::LockOrUnlockCommon(int function) {
+ DCHECK(mapped_file_ >= 0);
+ while (lockf(mapped_file_, function, 0) < 0) {
+ if (errno == EINTR) {
+ continue;
+ } else if (errno == ENOLCK) {
+ // temporary kernel resource exaustion
+ PlatformThread::Sleep(500);
+ continue;
+ } else {
+ NOTREACHED() << "lockf() failed."
+ << " function:" << function
+ << " fd:" << mapped_file_
+ << " errno:" << errno
+ << " msg:" << strerror(errno);
+ }
}
}
void SharedMemory::Lock() {
- DCHECK(lock_ != NULL);
- while(sem_wait(lock_) < 0) {
- DCHECK(errno == EAGAIN || errno == EINTR);
- }
+ LockOrUnlockCommon(F_LOCK);
}
void SharedMemory::Unlock() {
- DCHECK(lock_ != NULL);
- int result = sem_post(lock_);
- DCHECK(result == 0);
+ LockOrUnlockCommon(F_ULOCK);
}
} // namespace base
« no previous file with comments | « base/shared_memory.h ('k') | base/shared_memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698