}
}
}
- // while (true) {
- // int res = deque->take();
- // if (res != EMPTY) {
- // sums[index] += res;
- // succ_counts[index]++;
- // } else {
- // break;
- // }
- // }
+ while (true) {
+ int res = deque->take();
+ if (res != EMPTY) {
+ sums[index] += res;
+ succ_counts[index]++;
+ } else {
+ break;
+ }
+ }
}
};
received_sum += sums[i];
overall_count += succ_counts[i];
}
- cout << "Sum of push: " << push_sum << "\n";
- cout << "Received sum:" << received_sum << "\n";
- cout << "overall_count:" << overall_count << "\n";
- cout << "push_count=" << push_count << "\n";
+ if (overall_count != push_count || received_sum != push_sum) {
+ cout << "Incorrect deque\n";
+ cout << "Push sum: " << push_sum << "\n";
+ cout << "Received sum:" << received_sum << "\n";
+ cout << "Push count=" << push_count << "\n";
+ cout << "Received count:" << overall_count << "\n";
+ }
}
} // namespace
static void Thread() {
cds_others::mcs_mutex::guard g(my_mutex);
- x = 1;
my_mutex->unlock(&g);
for (ullong i = 0; i < s_nMCSLockPassCount; i++) {
my_mutex->lock(&g);
- x = i;
+ x++;
my_mutex->unlock(&g);
}
my_mutex->lock(&g);
TEST_F(MCSLockTest, BasicLockUnlock) {
my_mutex = new cds_others::mcs_mutex();
+ x = 0;
std::thread *threads = new std::thread[s_nMCSLockThreadCount];
for (int i = 0; i < s_nMCSLockThreadCount; i++) {
threads[i] = std::thread(Thread);
}
-
for (int i = 0; i < s_nMCSLockThreadCount; i++) {
threads[i].join();
}
+ if (x != s_nMCSLockPassCount * s_nMCSLockThreadCount) {
+ cout << "MCS lock incorrect\n";
+ }
}
} // namespace
if (!rwlock->write_trylock()) {
rwlock->write_lock();
}
- x += 1;
+ x++;
rwlock->write_unlock();
} else {
rwlock->write_lock();
- x += 1;
+ x++;
rwlock->write_unlock();
}
}
TEST_F(RWLockTest, BasicLockUnlock) {
rwlock = new RWLock();
int num_threads = s_nRWLockThreadCount;
- for (int write_percentage = 5; write_percentage < 50; write_percentage += 5) {
+ for (int write_percentage = 5; write_percentage < 40; write_percentage += 5) {
std::thread *threads = new std::thread[num_threads];
for (size_t i = 0; i < num_threads; i++) {
threads[i] = std::thread(ReaderWriterThread, write_percentage);
#define TASK(lock_type, lock_ptr, pass_cnt) \
static void Thread##lock_type() { \
for (int i = 0; i < pass_cnt; i++) { \
- for (int j = 0; j < pass_cnt; j++) { \
- lock_ptr->lock(); \
- x = i + j; \
- lock_ptr->unlock(); \
- } \
+ lock_ptr->lock(); \
+ x++; \
+ lock_ptr->unlock(); \
} \
}
-#define LOCK_TEST(lock_type, lock_ptr) \
+#define LOCK_TEST(lock_type, lock_ptr, pass_cnt) \
TEST_F(SpinLockTest, lock_type) { \
lock_ptr = new lock_type(); \
+ x = 0; \
std::thread *threads = new std::thread[s_nSpinLockThreadCount]; \
for (int i = 0; i < s_nSpinLockThreadCount; i++) { \
threads[i] = std::thread(Thread##lock_type); \
for (int i = 0; i < s_nSpinLockThreadCount; i++) { \
threads[i].join(); \
} \
+ if (x != s_nSpinLockThreadCount * pass_cnt) { \
+ cout << "Incorrect " << #lock_type << "\n"; \
+ } \
}
class SpinLockTest : public cds_test::stress_fixture {
Reentrant32 *SpinLockTest::reentrant_mutex32;
Reentrant64 *SpinLockTest::reentrant_mutex64;
-LOCK_TEST(TicketLock, ticket_mutex)
-LOCK_TEST(SpinLock, spin_mutex)
-LOCK_TEST(Reentrant32, reentrant_mutex32)
-LOCK_TEST(Reentrant64, reentrant_mutex64)
+LOCK_TEST(TicketLock, ticket_mutex, s_nTicketLockPassCount)
+LOCK_TEST(SpinLock, spin_mutex, s_nSpinLockPassCount)
+LOCK_TEST(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
+LOCK_TEST(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
} // namespace