summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-25 06:28:56 +0000
committervandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-25 06:28:56 +0000
commita0c136a0c23998b4a66679d1d8883ee057ef5060 (patch)
tree37f1ebd13d7e3ca72866aa0ff01496327b611f81
parenteb2efb10f5de9dd531d8693d20474f86b9036a74 (diff)
downloadchromium_src-a0c136a0c23998b4a66679d1d8883ee057ef5060.zip
chromium_src-a0c136a0c23998b4a66679d1d8883ee057ef5060.tar.gz
chromium_src-a0c136a0c23998b4a66679d1d8883ee057ef5060.tar.bz2
Improve SharedMemory::Lock on Posix and reenable StatsTableTest.MultipleThreads
BUG=10611 TEST=NONE Review URL: http://codereview.chromium.org/9463018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123647 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/metrics/stats_table_unittest.cc3
-rw-r--r--base/shared_memory.h21
-rw-r--r--base/shared_memory_posix.cc8
3 files changed, 19 insertions, 13 deletions
diff --git a/base/metrics/stats_table_unittest.cc b/base/metrics/stats_table_unittest.cc
index 0a1c38bb..2755ace 100644
--- a/base/metrics/stats_table_unittest.cc
+++ b/base/metrics/stats_table_unittest.cc
@@ -106,8 +106,7 @@ void StatsTableThread::Run() {
}
// Create a few threads and have them poke on their counters.
-// Flaky, http://crbug.com/10611.
-TEST_F(StatsTableTest, DISABLED_MultipleThreads) {
+TEST_F(StatsTableTest, MultipleThreads) {
// Create a stats table.
const std::string kTableName = "MultipleThreadStatTable";
const int kMaxThreads = 20;
diff --git a/base/shared_memory.h b/base/shared_memory.h
index 298baa6..a5744830 100644
--- a/base/shared_memory.h
+++ b/base/shared_memory.h
@@ -8,18 +8,22 @@
#include "build/build_config.h"
+#include <string>
+
#if defined(OS_POSIX)
#include <stdio.h>
#include <sys/types.h>
#include <semaphore.h>
-#include "base/file_descriptor_posix.h"
#endif
-#include <string>
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/process.h"
+#if defined(OS_POSIX)
+#include "base/file_descriptor_posix.h"
+#endif
+
class FilePath;
namespace base {
@@ -201,14 +205,11 @@ class BASE_EXPORT SharedMemory {
}
// Locks the shared memory.
- // This is a cross-process lock which may be recursively
- // locked by the same thread.
- // TODO(port):
- // WARNING: on POSIX the lock only works across processes, not
- // across threads. 2 threads in the same process can both grab the
- // lock at the same time. There are several solutions for this
- // (futex, lockf+anon_semaphore) but none are both clean and common
- // across Mac and Linux.
+ //
+ // WARNING: on POSIX the memory locking primitive only works across
+ // processes, not across threads. The Lock method is not currently
+ // used in inner loops, so we protect against multiple threads in a
+ // critical section using a class global lock.
void Lock();
#if defined(OS_WIN)
diff --git a/base/shared_memory_posix.cc b/base/shared_memory_posix.cc
index 32a55e9..34403c4 100644
--- a/base/shared_memory_posix.cc
+++ b/base/shared_memory_posix.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -11,9 +11,11 @@
#include <unistd.h>
#include "base/file_util.h"
+#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/threading/platform_thread.h"
#include "base/safe_strerror_posix.h"
+#include "base/synchronization/lock.h"
#include "base/threading/thread_restrictions.h"
#include "base/utf_string_conversions.h"
@@ -34,6 +36,8 @@ namespace {
// namespaces, but who knows what's out there.
const char kSemaphoreSuffix[] = "-sem";
+LazyInstance<Lock>::Leaky g_thread_lock_ = LAZY_INSTANCE_INITIALIZER;
+
}
SharedMemory::SharedMemory()
@@ -268,11 +272,13 @@ void SharedMemory::Close() {
}
void SharedMemory::Lock() {
+ g_thread_lock_.Get().Acquire();
LockOrUnlockCommon(F_LOCK);
}
void SharedMemory::Unlock() {
LockOrUnlockCommon(F_ULOCK);
+ g_thread_lock_.Get().Release();
}
#if !defined(OS_ANDROID)