| 49 | }; |
| 50 | |
| 51 | TEST(LockTest, Basic) { |
| 52 | Lock lock; |
| 53 | BasicLockTestThread thread(&lock); |
| 54 | PlatformThreadHandle handle; |
| 55 | |
| 56 | ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 57 | |
| 58 | int acquired = 0; |
| 59 | for (int i = 0; i < 5; i++) { |
| 60 | lock.Acquire(); |
| 61 | acquired++; |
| 62 | lock.Release(); |
| 63 | } |
| 64 | for (int i = 0; i < 10; i++) { |
| 65 | lock.Acquire(); |
| 66 | acquired++; |
| 67 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20)); |
| 68 | lock.Release(); |
| 69 | } |
| 70 | for (int i = 0; i < 10; i++) { |
| 71 | if (lock.Try()) { |
| 72 | acquired++; |
| 73 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20)); |
| 74 | lock.Release(); |
| 75 | } |
| 76 | } |
| 77 | for (int i = 0; i < 5; i++) { |
| 78 | lock.Acquire(); |
| 79 | acquired++; |
| 80 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20)); |
| 81 | lock.Release(); |
| 82 | } |
| 83 | |
| 84 | PlatformThread::Join(handle); |
| 85 | |
| 86 | EXPECT_GE(acquired, 20); |
| 87 | EXPECT_GE(thread.acquired(), 20); |
| 88 | } |
| 89 | |
| 90 | // Test that Try() works as expected ------------------------------------------- |
| 91 | |