tls_ut.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "tls.h"
  2. #include "thread.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. Y_UNIT_TEST_SUITE(TTestTLS) {
  5. struct X {
  6. inline X()
  7. : V(0)
  8. {
  9. }
  10. inline void Do() {
  11. ++TlsRef(V);
  12. }
  13. inline int Get() {
  14. return TlsRef(V);
  15. }
  16. Y_THREAD(int)
  17. V;
  18. };
  19. Y_UNIT_TEST(TestHugeSetup) {
  20. TArrayHolder<X> x(new X[100000]);
  21. struct TThr: public ISimpleThread {
  22. inline TThr(X* ptr)
  23. : P(ptr)
  24. {
  25. }
  26. void* ThreadProc() noexcept override {
  27. for (size_t i = 0; i < 100000; ++i) {
  28. P[i].Do();
  29. }
  30. return nullptr;
  31. }
  32. X* P;
  33. };
  34. TThr thr1(x.Get());
  35. TThr thr2(x.Get());
  36. thr1.Start();
  37. thr2.Start();
  38. thr1.Join();
  39. thr2.Join();
  40. for (size_t i = 0; i < 100000; ++i) {
  41. UNIT_ASSERT_VALUES_EQUAL(x.Get()[i].Get(), 0);
  42. }
  43. }
  44. } // Y_UNIT_TEST_SUITE(TTestTLS)