forked from flutter/flutter
-
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.
Add static thread safety analysis ready synchronization primitives. (f…
- Loading branch information
1 parent
f2e297c
commit e09b0ed
Showing
12 changed files
with
268 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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/testing/testing.h" | ||
#include "impeller/base/thread.h" | ||
|
||
namespace impeller { | ||
namespace testing { | ||
|
||
struct Foo { | ||
Mutex mtx; | ||
int a IPLR_GUARDED_BY(mtx); | ||
}; | ||
|
||
struct RWFoo { | ||
RWMutex mtx; | ||
int a IPLR_GUARDED_BY(mtx); | ||
}; | ||
|
||
TEST(ThreadTest, CanCreateMutex) { | ||
Foo f = {}; | ||
|
||
// f.a = 100; <--- Static analysis error. | ||
f.mtx.Lock(); | ||
f.a = 100; | ||
f.mtx.Unlock(); | ||
} | ||
|
||
TEST(ThreadTest, CanCreateMutexLock) { | ||
Foo f = {}; | ||
|
||
// f.a = 100; <--- Static analysis error. | ||
auto a = Lock(f.mtx); | ||
f.a = 100; | ||
} | ||
|
||
TEST(ThreadTest, CanCreateRWMutex) { | ||
RWFoo f = {}; | ||
|
||
// f.a = 100; <--- Static analysis error. | ||
f.mtx.LockWriter(); | ||
f.a = 100; | ||
f.mtx.UnlockWriter(); | ||
// int b = f.a; <--- Static analysis error. | ||
f.mtx.LockReader(); | ||
int b = f.a; | ||
FML_ALLOW_UNUSED_LOCAL(b); | ||
f.mtx.UnlockReader(); | ||
} | ||
|
||
TEST(ThreadTest, CanCreateRWMutexLock) { | ||
RWFoo f = {}; | ||
|
||
// f.a = 100; <--- Static analysis error. | ||
{ | ||
auto write_lock = WriterLock{f.mtx}; | ||
f.a = 100; | ||
} | ||
|
||
// int b = f.a; <--- Static analysis error. | ||
{ | ||
auto read_lock = ReaderLock(f.mtx); | ||
int b = f.a; | ||
FML_ALLOW_UNUSED_LOCAL(b); | ||
} | ||
|
||
// f.mtx.UnlockReader(); <--- Static analysis error. | ||
} | ||
|
||
} // namespace testing | ||
} // namespace impeller |
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,93 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <thread> | ||
|
||
#include "flutter/fml/macros.h" | ||
#include "flutter/fml/synchronization/shared_mutex.h" | ||
#include "impeller/base/thread_safety.h" | ||
|
||
namespace impeller { | ||
|
||
class IPLR_CAPABILITY("mutex") Mutex { | ||
public: | ||
Mutex() = default; | ||
|
||
~Mutex() = default; | ||
|
||
void Lock() IPLR_ACQUIRE() { mutex_.lock(); } | ||
|
||
void Unlock() IPLR_RELEASE() { mutex_.unlock(); } | ||
|
||
private: | ||
std::mutex mutex_; | ||
|
||
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(Mutex); | ||
}; | ||
|
||
class IPLR_CAPABILITY("mutex") RWMutex { | ||
public: | ||
RWMutex() | ||
: mutex_(std::unique_ptr<fml::SharedMutex>(fml::SharedMutex::Create())) {} | ||
|
||
~RWMutex() = default; | ||
|
||
void LockWriter() IPLR_ACQUIRE() { mutex_->Lock(); } | ||
|
||
void UnlockWriter() IPLR_RELEASE() { mutex_->Unlock(); } | ||
|
||
void LockReader() IPLR_ACQUIRE_SHARED() { mutex_->LockShared(); } | ||
|
||
void UnlockReader() IPLR_RELEASE_SHARED() { mutex_->UnlockShared(); } | ||
|
||
private: | ||
std::unique_ptr<fml::SharedMutex> mutex_; | ||
|
||
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(RWMutex); | ||
}; | ||
|
||
class IPLR_SCOPED_CAPABILITY Lock { | ||
public: | ||
Lock(Mutex& mutex) IPLR_ACQUIRE(mutex) : mutex_(mutex) { mutex_.Lock(); } | ||
|
||
~Lock() IPLR_RELEASE() { mutex_.Unlock(); } | ||
|
||
private: | ||
Mutex& mutex_; | ||
|
||
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(Lock); | ||
}; | ||
|
||
class IPLR_SCOPED_CAPABILITY ReaderLock { | ||
public: | ||
ReaderLock(RWMutex& mutex) IPLR_ACQUIRE_SHARED(mutex) : mutex_(mutex) { | ||
mutex_.LockReader(); | ||
} | ||
|
||
~ReaderLock() IPLR_RELEASE() { mutex_.UnlockReader(); } | ||
|
||
private: | ||
RWMutex& mutex_; | ||
|
||
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(ReaderLock); | ||
}; | ||
|
||
class IPLR_SCOPED_CAPABILITY WriterLock { | ||
public: | ||
WriterLock(RWMutex& mutex) IPLR_ACQUIRE(mutex) : mutex_(mutex) { | ||
mutex_.LockWriter(); | ||
} | ||
|
||
~WriterLock() IPLR_RELEASE() { mutex_.UnlockWriter(); } | ||
|
||
private: | ||
RWMutex& mutex_; | ||
|
||
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(WriterLock); | ||
}; | ||
|
||
} // namespace impeller |
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,11 @@ | ||
// 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 "impeller/base/thread_safety.h" | ||
|
||
namespace impeller { | ||
|
||
// | ||
|
||
} // namespace impeller |
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,69 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#if defined(__clang__) | ||
#define IPLR_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) | ||
#else | ||
#define IPLR_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op | ||
#endif | ||
|
||
#define IPLR_CAPABILITY(x) IPLR_THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) | ||
|
||
#define IPLR_SCOPED_CAPABILITY \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) | ||
|
||
#define IPLR_GUARDED_BY(x) IPLR_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) | ||
|
||
#define IPLR_PT_GUARDED_BY(x) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) | ||
|
||
#define IPLR_ACQUIRED_BEFORE(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) | ||
|
||
#define IPLR_ACQUIRED_AFTER(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) | ||
|
||
#define IPLR_REQUIRES(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_REQUIRES_SHARED(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_ACQUIRE(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_ACQUIRE_SHARED(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_RELEASE(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_RELEASE_SHARED(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_RELEASE_GENERIC(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_TRY_ACQUIRE(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_TRY_ACQUIRE_SHARED(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) | ||
|
||
#define IPLR_EXCLUDES(...) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) | ||
|
||
#define IPLR_ASSERT_CAPABILITY(x) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) | ||
|
||
#define IPLR_ASSERT_SHARED_CAPABILITY(x) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) | ||
|
||
#define IPLR_RETURN_CAPABILITY(x) \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) | ||
|
||
#define IPLR_NO_THREAD_SAFETY_ANALYSIS \ | ||
IPLR_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
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
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