File: test_locks.cpp

package info (click to toggle)
dolphin-emu 2603%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,040 kB
  • sloc: cpp: 442,137; ansic: 117,979; python: 6,438; sh: 2,387; asm: 726; makefile: 394; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 8
file content (613 lines) | stat: -rw-r--r-- 13,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/**
 * @file test_locks.cpp
 * @brief Comprehensive unit tests for ipc::rw_lock and ipc::spin_lock classes
 * 
 * This test suite covers:
 * - spin_lock: basic lock/unlock operations
 * - rw_lock: read-write lock functionality
 * - rw_lock: exclusive (write) locks
 * - rw_lock: shared (read) locks
 * - Concurrent access patterns
 * - Reader-writer scenarios
 */

#include <gtest/gtest.h>
#include <thread>
#include <chrono>
#include <atomic>
#include <vector>
#include "libipc/rw_lock.h"

using namespace ipc;

// ========== spin_lock Tests ==========

class SpinLockTest : public ::testing::Test {
protected:
  void TearDown() override {
      std::this_thread::sleep_for(std::chrono::milliseconds(5));
  }
};

// Test basic lock and unlock
TEST_F(SpinLockTest, BasicLockUnlock) {
  spin_lock lock;
  
  lock.lock();
  lock.unlock();
  
  // Should complete without hanging
}

// Test multiple lock/unlock cycles
TEST_F(SpinLockTest, MultipleCycles) {
  spin_lock lock;
  
  for (int i = 0; i < 100; ++i) {
      lock.lock();
      lock.unlock();
  }
}

// Test critical section protection
TEST_F(SpinLockTest, CriticalSection) {
  spin_lock lock;
  int counter = 0;
  const int iterations = 1000;
  
  auto increment_task = [&]() {
      for (int i = 0; i < iterations; ++i) {
          lock.lock();
          ++counter;
          lock.unlock();
      }
  };
  
  std::thread t1(increment_task);
  std::thread t2(increment_task);
  
  t1.join();
  t2.join();
  
  EXPECT_EQ(counter, iterations * 2);
}

// Test mutual exclusion
TEST_F(SpinLockTest, MutualExclusion) {
  spin_lock lock;
  std::atomic<bool> thread1_in_cs{false};
  std::atomic<bool> thread2_in_cs{false};
  std::atomic<bool> violation{false};
  
  auto cs_task = [&](std::atomic<bool>& my_flag, std::atomic<bool>& other_flag) {
      for (int i = 0; i < 100; ++i) {
          lock.lock();
          
          my_flag.store(true);
          if (other_flag.load()) {
              violation.store(true);
          }
          
          std::this_thread::sleep_for(std::chrono::microseconds(10));
          
          my_flag.store(false);
          lock.unlock();
          
          std::this_thread::yield();
      }
  };
  
  std::thread t1(cs_task, std::ref(thread1_in_cs), std::ref(thread2_in_cs));
  std::thread t2(cs_task, std::ref(thread2_in_cs), std::ref(thread1_in_cs));
  
  t1.join();
  t2.join();
  
  EXPECT_FALSE(violation.load());
}

// Test concurrent access
TEST_F(SpinLockTest, ConcurrentAccess) {
  spin_lock lock;
  std::atomic<int> shared_data{0};
  const int num_threads = 4;
  const int ops_per_thread = 100;
  
  std::vector<std::thread> threads;
  for (int i = 0; i < num_threads; ++i) {
      threads.emplace_back([&]() {
          for (int j = 0; j < ops_per_thread; ++j) {
              lock.lock();
              int temp = shared_data.load();
              std::this_thread::yield();
              shared_data.store(temp + 1);
              lock.unlock();
          }
      });
  }
  
  for (auto& t : threads) {
      t.join();
  }
  
  EXPECT_EQ(shared_data.load(), num_threads * ops_per_thread);
}

// Test rapid lock/unlock
TEST_F(SpinLockTest, RapidLockUnlock) {
  spin_lock lock;
  
  auto rapid_task = [&]() {
      for (int i = 0; i < 10000; ++i) {
          lock.lock();
          lock.unlock();
      }
  };
  
  std::thread t1(rapid_task);
  std::thread t2(rapid_task);
  
  t1.join();
  t2.join();
  
  // Should complete without deadlock
}

// Test contention scenario
TEST_F(SpinLockTest, Contention) {
  spin_lock lock;
  std::atomic<int> work_done{0};
  const int num_threads = 8;
  
  std::vector<std::thread> threads;
  for (int i = 0; i < num_threads; ++i) {
      threads.emplace_back([&]() {
          for (int j = 0; j < 50; ++j) {
              lock.lock();
              ++work_done;
              std::this_thread::sleep_for(std::chrono::microseconds(100));
              lock.unlock();
              std::this_thread::yield();
          }
      });
  }
  
  for (auto& t : threads) {
      t.join();
  }
  
  EXPECT_EQ(work_done.load(), num_threads * 50);
}

// ========== rw_lock Tests ==========

class RWLockTest : public ::testing::Test {
protected:
  void TearDown() override {
      std::this_thread::sleep_for(std::chrono::milliseconds(5));
  }
};

// Test basic write lock and unlock
TEST_F(RWLockTest, BasicWriteLock) {
  rw_lock lock;
  
  lock.lock();
  lock.unlock();
  
  // Should complete without hanging
}

// Test basic read lock and unlock
TEST_F(RWLockTest, BasicReadLock) {
  rw_lock lock;
  
  lock.lock_shared();
  lock.unlock_shared();
  
  // Should complete without hanging
}

// Test multiple write cycles
TEST_F(RWLockTest, MultipleWriteCycles) {
  rw_lock lock;
  
  for (int i = 0; i < 100; ++i) {
      lock.lock();
      lock.unlock();
  }
}

// Test multiple read cycles
TEST_F(RWLockTest, MultipleReadCycles) {
  rw_lock lock;
  
  for (int i = 0; i < 100; ++i) {
      lock.lock_shared();
      lock.unlock_shared();
  }
}

// Test write lock protects data
TEST_F(RWLockTest, WriteLockProtection) {
  rw_lock lock;
  int data = 0;
  const int iterations = 500;
  
  auto writer_task = [&]() {
      for (int i = 0; i < iterations; ++i) {
          lock.lock();
          ++data;
          lock.unlock();
      }
  };
  
  std::thread t1(writer_task);
  std::thread t2(writer_task);
  
  t1.join();
  t2.join();
  
  EXPECT_EQ(data, iterations * 2);
}

// Test multiple readers can access concurrently
TEST_F(RWLockTest, ConcurrentReaders) {
  rw_lock lock;
  std::atomic<int> concurrent_readers{0};
  std::atomic<int> max_concurrent{0};
  
  const int num_readers = 5;
  
  std::vector<std::thread> readers;
  for (int i = 0; i < num_readers; ++i) {
      readers.emplace_back([&]() {
          for (int j = 0; j < 20; ++j) {
              lock.lock_shared();
              
              int current = ++concurrent_readers;
              
              // Track maximum concurrent readers
              int current_max = max_concurrent.load();
              while (current > current_max) {
                  if (max_concurrent.compare_exchange_weak(current_max, current)) {
                      break;
                  }
              }
              
              std::this_thread::sleep_for(std::chrono::microseconds(100));
              
              --concurrent_readers;
              lock.unlock_shared();
              
              std::this_thread::yield();
          }
      });
  }
  
  for (auto& t : readers) {
      t.join();
  }
  
  // Should have had multiple concurrent readers
  EXPECT_GT(max_concurrent.load(), 1);
}

// Test writers have exclusive access
TEST_F(RWLockTest, WriterExclusiveAccess) {
  rw_lock lock;
  std::atomic<bool> writer_in_cs{false};
  std::atomic<bool> violation{false};
  
  auto writer_task = [&]() {
      for (int i = 0; i < 50; ++i) {
          lock.lock();
          
          if (writer_in_cs.exchange(true)) {
              violation.store(true);
          }
          
          std::this_thread::sleep_for(std::chrono::microseconds(50));
          
          writer_in_cs.store(false);
          lock.unlock();
          
          std::this_thread::yield();
      }
  };
  
  std::thread t1(writer_task);
  std::thread t2(writer_task);
  
  t1.join();
  t2.join();
  
  EXPECT_FALSE(violation.load());
}

// Test readers and writers don't overlap
TEST_F(RWLockTest, ReadersWritersNoOverlap) {
  rw_lock lock;
  std::atomic<int> readers{0};
  std::atomic<bool> writer_active{false};
  std::atomic<bool> violation{false};
  
  auto reader_task = [&]() {
      for (int i = 0; i < 30; ++i) {
          lock.lock_shared();
          
          ++readers;
          if (writer_active.load()) {
              violation.store(true);
          }
          
          std::this_thread::sleep_for(std::chrono::microseconds(50));
          
          --readers;
          lock.unlock_shared();
          
          std::this_thread::yield();
      }
  };
  
  auto writer_task = [&]() {
      for (int i = 0; i < 15; ++i) {
          lock.lock();
          
          writer_active.store(true);
          if (readers.load() > 0) {
              violation.store(true);
          }
          
          std::this_thread::sleep_for(std::chrono::microseconds(50));
          
          writer_active.store(false);
          lock.unlock();
          
          std::this_thread::yield();
      }
  };
  
  std::thread r1(reader_task);
  std::thread r2(reader_task);
  std::thread w1(writer_task);
  
  r1.join();
  r2.join();
  w1.join();
  
  EXPECT_FALSE(violation.load());
}

// Test read-write-read pattern
TEST_F(RWLockTest, ReadWriteReadPattern) {
  rw_lock lock;
  int data = 0;
  std::atomic<int> iterations{0};
  
  auto pattern_task = [&](int id) {
      for (int i = 0; i < 20; ++i) {
          // Write: increment based on thread id
          lock.lock();
          data += id;
          lock.unlock();
          
          iterations.fetch_add(1);
          std::this_thread::yield();
          
          // Read: verify data is consistent
          lock.lock_shared();
          int read_val = data;
          EXPECT_GE(read_val, 0);  // Data should be non-negative
          lock.unlock_shared();
          
          std::this_thread::yield();
      }
  };
  
  std::thread t1(pattern_task, 1);
  std::thread t2(pattern_task, 2);
  
  t1.join();
  t2.join();
  
  // Each thread increments by its id (1 or 2), 20 times each
  // Total = 1*20 + 2*20 = 20 + 40 = 60
  EXPECT_EQ(data, 60);
  EXPECT_EQ(iterations.load(), 40);
}

// Test many readers, one writer
TEST_F(RWLockTest, ManyReadersOneWriter) {
  rw_lock lock;
  std::atomic<int> data{0};
  std::atomic<int> read_count{0};
  
  const int num_readers = 10;
  
  std::vector<std::thread> readers;
  for (int i = 0; i < num_readers; ++i) {
      readers.emplace_back([&]() {
          for (int j = 0; j < 50; ++j) {
              lock.lock_shared();
              int val = data.load();
              ++read_count;
              lock.unlock_shared();
              std::this_thread::yield();
          }
      });
  }
  
  std::thread writer([&]() {
      for (int i = 0; i < 100; ++i) {
          lock.lock();
          data.store(data.load() + 1);
          lock.unlock();
          std::this_thread::yield();
      }
  });
  
  for (auto& t : readers) {
      t.join();
  }
  writer.join();
  
  EXPECT_EQ(data.load(), 100);
  EXPECT_EQ(read_count.load(), num_readers * 50);
}

// Test rapid read lock/unlock
TEST_F(RWLockTest, RapidReadLocks) {
  rw_lock lock;
  
  auto rapid_read = [&]() {
      for (int i = 0; i < 5000; ++i) {
          lock.lock_shared();
          lock.unlock_shared();
      }
  };
  
  std::thread t1(rapid_read);
  std::thread t2(rapid_read);
  std::thread t3(rapid_read);
  
  t1.join();
  t2.join();
  t3.join();
}

// Test rapid write lock/unlock
TEST_F(RWLockTest, RapidWriteLocks) {
  rw_lock lock;
  
  auto rapid_write = [&]() {
      for (int i = 0; i < 2000; ++i) {
          lock.lock();
          lock.unlock();
      }
  };
  
  std::thread t1(rapid_write);
  std::thread t2(rapid_write);
  
  t1.join();
  t2.join();
}

// Test mixed rapid operations
TEST_F(RWLockTest, MixedRapidOperations) {
  rw_lock lock;
  
  auto rapid_read = [&]() {
      for (int i = 0; i < 1000; ++i) {
          lock.lock_shared();
          lock.unlock_shared();
      }
  };
  
  auto rapid_write = [&]() {
      for (int i = 0; i < 500; ++i) {
          lock.lock();
          lock.unlock();
      }
  };
  
  std::thread r1(rapid_read);
  std::thread r2(rapid_read);
  std::thread w1(rapid_write);
  
  r1.join();
  r2.join();
  w1.join();
}

// Test write lock doesn't allow concurrent readers
TEST_F(RWLockTest, WriteLockBlocksReaders) {
  rw_lock lock;
  std::atomic<bool> write_locked{false};
  std::atomic<bool> reader_entered{false};
  
  std::thread writer([&]() {
      lock.lock();
      write_locked.store(true);
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
      write_locked.store(false);
      lock.unlock();
  });
  
  std::thread reader([&]() {
      std::this_thread::sleep_for(std::chrono::milliseconds(20));
      
      lock.lock_shared();
      if (write_locked.load()) {
          reader_entered.store(true);
      }
      lock.unlock_shared();
  });
  
  writer.join();
  reader.join();
  
  // Reader should not have entered while writer held the lock
  EXPECT_FALSE(reader_entered.load());
}

// Test multiple write lock upgrades
TEST_F(RWLockTest, MultipleWriteLockPattern) {
  rw_lock lock;
  int data = 0;
  
  for (int i = 0; i < 100; ++i) {
      // Read
      lock.lock_shared();
      int temp = data;
      lock.unlock_shared();
      
      // Write
      lock.lock();
      data = temp + 1;
      lock.unlock();
  }
  
  EXPECT_EQ(data, 100);
}

// Test concurrent mixed operations
TEST_F(RWLockTest, ConcurrentMixedOperations) {
  rw_lock lock;
  std::atomic<int> data{0};
  std::atomic<int> reads{0};
  std::atomic<int> writes{0};
  
  auto mixed_task = [&](int id) {
      for (int i = 0; i < 50; ++i) {
          if (i % 3 == 0) {
              // Write operation
              lock.lock();
              data.store(data.load() + 1);
              ++writes;
              lock.unlock();
          } else {
              // Read operation
              lock.lock_shared();
              int val = data.load();
              ++reads;
              lock.unlock_shared();
          }
          std::this_thread::yield();
      }
  };
  
  std::thread t1(mixed_task, 1);
  std::thread t2(mixed_task, 2);
  std::thread t3(mixed_task, 3);
  std::thread t4(mixed_task, 4);
  
  t1.join();
  t2.join();
  t3.join();
  t4.join();
  
  EXPECT_GT(reads.load(), 0);
  EXPECT_GT(writes.load(), 0);
}