3 #include <cds/gc/dhp.h>
5 #include <cds/sync/rwlock.h>
6 #include <cds_test/stress_test.h>
14 static size_t s_nRWLockThreadCount = 6;
15 static size_t s_nRWLockPassCount = 200000;
17 typedef cds_others::RWLock RWLock;
18 class RWLockTest : public cds_test::stress_fixture {
22 static RWLock *rwlock;
24 static void SetUpTestCase() {
25 cds_test::config const &cfg = get_config("Misc");
26 GetConfig(RWLockThreadCount);
27 GetConfig(RWLockPassCount);
30 static void ReaderWriterThread(int write_percentage) {
31 for (size_t i = 0; i < s_nRWLockPassCount; i++) {
32 if (rand(100) < write_percentage) {
33 if (rwlock->read_can_lock()) {
34 if (!rwlock->read_trylock()) {
38 rwlock->read_unlock();
42 rwlock->read_unlock();
45 if (rwlock->write_can_lock()) {
46 if (!rwlock->write_trylock()) {
50 rwlock->write_unlock();
54 rwlock->write_unlock();
63 RWLock *RWLockTest::rwlock;
65 TEST_F(RWLockTest, BasicLockUnlock) {
66 rwlock = new RWLock();
67 int num_threads = s_nRWLockThreadCount;
68 for (int write_percentage = 5; write_percentage < 40; write_percentage += 5) {
69 std::thread *threads = new std::thread[num_threads];
70 for (size_t i = 0; i < num_threads; i++) {
71 threads[i] = std::thread(ReaderWriterThread, write_percentage);
74 for (int i = 0; i < num_threads; i++) {