Commit 84ce2395 authored by davidben's avatar davidben Committed by Commit bot

Make SSLClientSessionCacheOpenSSL thread-safe.

Although the network stack is single-threaded, it *should* be legal to spin up
a parallel instance of everything on another thread. Until
https://crbug.com/458365 is resolved, the session cache is one of the few
global mutable objects in the network stack, apart from test code. Thus it
needs to be locked.

BUG=476713

Review URL: https://codereview.chromium.org/1079943006

Cr-Commit-Position: refs/heads/master@{#325459}
parent 91de260e
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <utility> #include <utility>
#include "base/logging.h"
#include "base/time/clock.h" #include "base/time/clock.h"
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
...@@ -20,11 +19,6 @@ SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) ...@@ -20,11 +19,6 @@ SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config)
} }
SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() {
// TODO(davidben): The session cache is currently a singleton, so it is
// destroyed on a different thread than the one it's created on. When
// https://crbug.com/458365 is fixed, this will no longer be an issue.
thread_checker_.DetachFromThread();
Flush(); Flush();
} }
...@@ -34,7 +28,7 @@ size_t SSLClientSessionCacheOpenSSL::size() const { ...@@ -34,7 +28,7 @@ size_t SSLClientSessionCacheOpenSSL::size() const {
SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup(
const std::string& cache_key) { const std::string& cache_key) {
DCHECK(thread_checker_.CalledOnValidThread()); base::AutoLock lock(lock_);
// Expire stale sessions. // Expire stale sessions.
lookups_since_flush_++; lookups_since_flush_++;
...@@ -55,7 +49,7 @@ SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( ...@@ -55,7 +49,7 @@ SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup(
void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key,
SSL_SESSION* session) { SSL_SESSION* session) {
DCHECK(thread_checker_.CalledOnValidThread()); base::AutoLock lock(lock_);
// Make a new entry. // Make a new entry.
CacheEntry* entry = new CacheEntry; CacheEntry* entry = new CacheEntry;
...@@ -67,15 +61,13 @@ void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, ...@@ -67,15 +61,13 @@ void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key,
} }
void SSLClientSessionCacheOpenSSL::Flush() { void SSLClientSessionCacheOpenSSL::Flush() {
DCHECK(thread_checker_.CalledOnValidThread()); base::AutoLock lock(lock_);
cache_.Clear(); cache_.Clear();
} }
void SSLClientSessionCacheOpenSSL::SetClockForTesting( void SSLClientSessionCacheOpenSSL::SetClockForTesting(
scoped_ptr<base::Clock> clock) { scoped_ptr<base::Clock> clock) {
DCHECK(thread_checker_.CalledOnValidThread());
clock_ = clock.Pass(); clock_ = clock.Pass();
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/containers/mru_cache.h" #include "base/containers/mru_cache.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
...@@ -82,7 +83,10 @@ class NET_EXPORT SSLClientSessionCacheOpenSSL { ...@@ -82,7 +83,10 @@ class NET_EXPORT SSLClientSessionCacheOpenSSL {
CacheEntryMap cache_; CacheEntryMap cache_;
size_t lookups_since_flush_; size_t lookups_since_flush_;
base::ThreadChecker thread_checker_; // TODO(davidben): After https://crbug.com/458365 is fixed, replace this with
// a ThreadChecker. The session cache should be single-threaded like other
// classes in net.
base::Lock lock_;
DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCacheOpenSSL); DISALLOW_COPY_AND_ASSIGN(SSLClientSessionCacheOpenSSL);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment