Instantly share code, notes, and snippets.
Last active
July 23, 2020 13:50
-
Save jmarantz/f2b83a3ec253535b30366c2b697da35a to your computer and use it in GitHub Desktop.
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
Change by morlovich@morlovich:flake:1956:citc on 2020/01/28 | |
Fix shared mem tests to lock properly. | |
Makes them a bit less bootstrap-y, but needed for correctness anyway | |
Index: pagespeed/kernel/sharedmem/shared_mem_test_base.ccxxxxxpagespeed/kernel/sharedmem/shared_mem_test_base.cc ==== | |
--- /google/src/files/266908245/depot/google3pagespeed/kernel/sharedmem/shared_mem_test_base.cc 2019-09-03 08:33:36.000000000 -0400 | |
+++ pagespeed/kernel/sharedmem/shared_mem_test_base.cc 2020-01-28 16:22:01.000000000 -0500 | |
@@ -59,13 +59,21 @@ | |
seg.reset(AttachDefault()); | |
} | |
+ scoped_ptr<AbstractMutex> mutex(AttachDefaultMutex(seg.get())); | |
+ | |
// Wait for kid to write out stuff | |
+ mutex->Lock(); | |
while (*seg->Base() != '1') { | |
+ mutex->Unlock(); | |
test_env_->ShortSleep(); | |
+ mutex->Lock(); | |
} | |
+ mutex->Unlock(); | |
// Write out stuff. | |
+ mutex->Lock(); | |
*seg->Base() = '2'; | |
+ mutex->Unlock(); | |
// Wait for termination. | |
test_env_->WaitForChildren(); | |
@@ -76,14 +84,21 @@ | |
void SharedMemTestBase::TestReadWriteChild() { | |
scoped_ptr<AbstractSharedMemSegment> seg(AttachDefault()); | |
+ scoped_ptr<AbstractMutex> mutex(AttachDefaultMutex(seg.get())); | |
// Write out '1', which the parent will wait for. | |
+ mutex->Lock(); | |
*seg->Base() = '1'; | |
+ mutex->Unlock(); | |
// Wait for '2' from parent | |
+ mutex->Lock(); | |
while (*seg->Base() != '2') { | |
+ mutex->Unlock(); | |
test_env_->ShortSleep(); | |
+ mutex->Lock(); | |
} | |
+ mutex->Unlock(); | |
} | |
void SharedMemTestBase::TestLarge() { | |
@@ -194,17 +209,25 @@ | |
void SharedMemTestBase::TwoKidsChild1() { | |
scoped_ptr<AbstractSharedMemSegment> seg(AttachDefault()); | |
ASSERT_TRUE(seg.get() != NULL); | |
+ scoped_ptr<AbstractMutex> mutex(AttachDefaultMutex(seg.get())); | |
// Write out '1', which the other kid will wait for. | |
+ mutex->Lock(); | |
*seg->Base() = '1'; | |
+ mutex->Unlock(); | |
} | |
void SharedMemTestBase::TwoKidsChild2() { | |
scoped_ptr<AbstractSharedMemSegment> seg(AttachDefault()); | |
ASSERT_TRUE(seg.get() != NULL); | |
+ scoped_ptr<AbstractMutex> mutex(AttachDefaultMutex(seg.get())); | |
// Wait for '1' | |
+ mutex->Lock(); | |
while (*seg->Base() != '1') { | |
+ mutex->Unlock(); | |
test_env_->ShortSleep(); | |
+ mutex->Lock(); | |
} | |
+ mutex->Unlock(); | |
*seg->Base() = '2'; | |
} | |
@@ -298,11 +321,20 @@ | |
} | |
AbstractSharedMemSegment* SharedMemTestBase::CreateDefault() { | |
- return shmem_runtime_->CreateSegment(kTestSegment, 4, &handler_); | |
+ AbstractSharedMemSegment* result = shmem_runtime_->CreateSegment( | |
+ kTestSegment, 4 + shmem_runtime_->SharedMutexSize(), &handler_); | |
+ EXPECT_TRUE(result->InitializeSharedMutex(4, &handler_)); | |
+ return result; | |
} | |
AbstractSharedMemSegment* SharedMemTestBase::AttachDefault() { | |
- return shmem_runtime_->AttachToSegment(kTestSegment, 4, &handler_); | |
+ return shmem_runtime_->AttachToSegment( | |
+ kTestSegment, 4 + shmem_runtime_->SharedMutexSize(), &handler_); | |
+} | |
+ | |
+AbstractMutex* SharedMemTestBase::AttachDefaultMutex( | |
+ AbstractSharedMemSegment* segment) { | |
+ return segment->AttachToSharedMutex(4); | |
} | |
void SharedMemTestBase::DestroyDefault() { | |
Index: pagespeed/kernel/sharedmem/shared_mem_test_base.hxxxxxpagespeed/kernel/sharedmem/shared_mem_test_base.h ==== | |
--- pagespeed/kernel/sharedmem/shared_mem_test_base.h 2020-01-19 17:33:53.000000000 -0500 | |
+++ pagespeed/kernel/sharedmem/shared_mem_test_base.h 2020-01-28 16:22:01.000000000 -0500 | |
@@ -99,6 +99,8 @@ | |
AbstractSharedMemSegment* CreateDefault(); | |
AbstractSharedMemSegment* AttachDefault(); | |
+ AbstractMutex* AttachDefaultMutex(AbstractSharedMemSegment* segment); | |
+ | |
void DestroyDefault(); | |
// writes '1' to the default segment's base location. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment