#include "tile/base/thread/spinlock.h"

#include <thread>

#include "gtest/gtest.h"

#include "tile/base/thread/latch.h"
#include "tile/base/thread/scoped_lock.h"

namespace tile {

std::uint64_t counter{};

TEST(Spinlock, All)
{
    constexpr auto T = 10;
    constexpr auto N = 100000;
    std::thread ts[T];
    Latch latch(1);
    Spinlock splk;

    for (auto &&t : ts) {
        t = std::thread([&] {
            latch.Wait();
            for (int i = 0; i != N; ++i) {
                ScopedLock lk(splk);
                ++counter;
            }
        });
    }
    latch.ArriveAndWait();
    for (auto &&t : ts) { t.join(); }
    ASSERT_EQ(T * N, counter);
}

}// namespace tile
