forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guard the service protocol's global handlers list with a reader/write…
…r lock (flutter#6888) (flutter#6895) The service protocol holds the lock while waiting for completion of service RPC tasks. These tasks (specifically hot restart/RunInView) may need to modify a handler's description data. Task execution and ServiceProtocol::SetHandlerDescription will obtain a shared lock to make this possible. AddHandler and RemoveHandler will obtain an exclusive lock in order to guard against a handler being deleted while a service task is running.
- Loading branch information
1 parent
fffcce4
commit fd0911c
Showing
10 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/platform/posix/shared_mutex_posix.h" | ||
#include "flutter/fml/logging.h" | ||
|
||
namespace fml { | ||
|
||
SharedMutex* SharedMutex::Create() { | ||
return new SharedMutexPosix(); | ||
} | ||
|
||
SharedMutexPosix::SharedMutexPosix() { | ||
FML_CHECK(pthread_rwlock_init(&rwlock_, nullptr) == 0); | ||
} | ||
|
||
void SharedMutexPosix::Lock() { | ||
pthread_rwlock_wrlock(&rwlock_); | ||
} | ||
|
||
void SharedMutexPosix::LockShared() { | ||
pthread_rwlock_rdlock(&rwlock_); | ||
} | ||
|
||
void SharedMutexPosix::Unlock() { | ||
pthread_rwlock_unlock(&rwlock_); | ||
} | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_POSIX_H_ | ||
#define FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_POSIX_H_ | ||
|
||
#include <shared_mutex> | ||
#include "flutter/fml/synchronization/shared_mutex.h" | ||
|
||
namespace fml { | ||
|
||
class SharedMutexPosix : public SharedMutex { | ||
public: | ||
virtual void Lock(); | ||
virtual void LockShared(); | ||
virtual void Unlock(); | ||
|
||
private: | ||
friend SharedMutex* SharedMutex::Create(); | ||
SharedMutexPosix(); | ||
|
||
pthread_rwlock_t rwlock_; | ||
}; | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_POSIX_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_SYNCHRONIZATION_ATOMIC_OBJECT_H_ | ||
#define FLUTTER_FML_SYNCHRONIZATION_ATOMIC_OBJECT_H_ | ||
|
||
#include <mutex> | ||
|
||
namespace fml { | ||
|
||
// A wrapper for an object instance that can be read or written atomically. | ||
template <typename T> | ||
class AtomicObject { | ||
public: | ||
AtomicObject() = default; | ||
AtomicObject(T object) : object_(object) {} | ||
|
||
T Load() const { | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
return object_; | ||
} | ||
|
||
void Store(const T& object) { | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
object_ = object; | ||
} | ||
|
||
private: | ||
mutable std::mutex mutex_; | ||
T object_; | ||
}; | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_SYNCHRONIZATION_ATOMIC_OBJECT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_ | ||
#define FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_ | ||
|
||
namespace fml { | ||
|
||
// Interface for a reader/writer lock. | ||
class SharedMutex { | ||
public: | ||
static SharedMutex* Create(); | ||
virtual ~SharedMutex() = default; | ||
|
||
virtual void Lock() = 0; | ||
virtual void LockShared() = 0; | ||
virtual void Unlock() = 0; | ||
}; | ||
|
||
// RAII wrapper that does a shared acquire of a SharedMutex. | ||
class SharedLock { | ||
public: | ||
explicit SharedLock(SharedMutex& shared_mutex) : shared_mutex_(shared_mutex) { | ||
shared_mutex_.LockShared(); | ||
} | ||
|
||
~SharedLock() { shared_mutex_.Unlock(); } | ||
|
||
private: | ||
SharedMutex& shared_mutex_; | ||
}; | ||
|
||
// RAII wrapper that does an exclusive acquire of a SharedMutex. | ||
class UniqueLock { | ||
public: | ||
explicit UniqueLock(SharedMutex& shared_mutex) : shared_mutex_(shared_mutex) { | ||
shared_mutex_.Lock(); | ||
} | ||
|
||
~UniqueLock() { shared_mutex_.Unlock(); } | ||
|
||
private: | ||
SharedMutex& shared_mutex_; | ||
}; | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/synchronization/shared_mutex_std.h" | ||
|
||
namespace fml { | ||
|
||
SharedMutex* SharedMutex::Create() { | ||
return new SharedMutexStd(); | ||
} | ||
|
||
void SharedMutexStd::Lock() { | ||
mutex_.lock(); | ||
} | ||
|
||
void SharedMutexStd::LockShared() { | ||
mutex_.lock_shared(); | ||
} | ||
|
||
void SharedMutexStd::Unlock() { | ||
mutex_.unlock(); | ||
} | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_STD_H_ | ||
#define FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_STD_H_ | ||
|
||
#include <shared_mutex> | ||
#include "flutter/fml/synchronization/shared_mutex.h" | ||
|
||
namespace fml { | ||
|
||
class SharedMutexStd : public SharedMutex { | ||
public: | ||
virtual void Lock(); | ||
virtual void LockShared(); | ||
virtual void Unlock(); | ||
|
||
private: | ||
friend SharedMutex* SharedMutex::Create(); | ||
SharedMutexStd() = default; | ||
|
||
std::shared_timed_mutex mutex_; | ||
}; | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_STD_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters