#include "flutter/fml/synchronization/thread_annotations.h"
#include <mutex>
#include "flutter/fml/macros.h"
#include "gtest/gtest.h"
namespace fml {
namespace {
class GuardedByClass {
public:
GuardedByClass() : x_() {}
~GuardedByClass() {}
void GoodSet(int x) {
mu_.lock();
x_ = x;
mu_.unlock();
}
#ifdef NC_GUARDED_BY
void BadSet(int x) { x_ = x; }
#endif
private:
std::mutex mu_;
int x_ FML_GUARDED_BY(mu_);
FML_DISALLOW_COPY_AND_ASSIGN(GuardedByClass);
};
TEST(ThreadAnnotationsTest, GuardedBy) {
GuardedByClass c;
c.GoodSet(123);
}
class AcquiredBeforeClass2;
class AcquiredBeforeClass1 {
public:
AcquiredBeforeClass1() {}
~AcquiredBeforeClass1() {}
void NoOp() {
mu_.lock();
mu_.unlock();
}
#ifdef NC_ACQUIRED_BEFORE
void BadMethod(AcquiredBeforeClass2* c2);
#endif
private:
friend class AcquiredBeforeClass2;
std::mutex mu_;
FML_DISALLOW_COPY_AND_ASSIGN(AcquiredBeforeClass1);
};
class AcquiredBeforeClass2 {
public:
AcquiredBeforeClass2() {}
~AcquiredBeforeClass2() {}
void NoOp() {
mu_.lock();
mu_.unlock();
}
void GoodMethod(AcquiredBeforeClass1* c1) {
mu_.lock();
c1->NoOp();
mu_.unlock();
}
private:
std::mutex mu_ FML_ACQUIRED_BEFORE(AcquiredBeforeClass1::mu_);
FML_DISALLOW_COPY_AND_ASSIGN(AcquiredBeforeClass2);
};
#ifdef NC_ACQUIRED_BEFORE
void AcquiredBeforeClass1::BadMethod(AcquiredBeforeClass2* c2) {
mu_.lock();
c2->NoOp();
mu_.unlock();
}
#endif
TEST(ThreadAnnotationsTest, AcquiredBefore) {
AcquiredBeforeClass1 c1;
AcquiredBeforeClass2 c2;
c2.GoodMethod(&c1);
#ifdef NC_ACQUIRED_BEFORE
c1.BadMethod(&c2);
#endif
}
}
}