<commit_before>#pragma once


#include <type_traits>
#include <utility>
#include <memory>
#include <string>
#include <uv.h>
#include "loop.hpp"


namespace uvw {


class Thread;
class ThreadLocalStorage;
class Once;
class Mutex;
class RWLock;
class Semaphore;
class Condition;
class Barrier;


class Thread final {
    using InternalTask = std::function<void(std::shared_ptr<void>)>;

    static void createCallback(void *arg) {
        Thread &thread = *(static_cast<Thread*>(arg));
        thread.task(thread.data);
    }

    explicit Thread(std::shared_ptr<Loop> ref, InternalTask t, std::shared_ptr<void> d = nullptr) noexcept
        : pLoop{std::move(ref)},
          data{std::move(d)},
          thread{},
          task{std::move(t)}
    { }

public:
    using Task = InternalTask;
    using Type = uv_thread_t;

    template<typename... Args>
    static std::shared_ptr<Thread> create(Args&&... args) {
        return std::shared_ptr<Thread>{new Thread{std::forward<Args>(args)...}};
    }

    static Type self() noexcept {
        return uv_thread_self();
    }

    static bool equal(const Thread &tl, const Thread &tr) noexcept {
        return !(0 == uv_thread_equal(&tl.thread, &tr.thread));
    }

    ~Thread() noexcept {
        uv_thread_join(&thread);
    }

    bool run() noexcept {
        return (0 == uv_thread_create(&thread, &createCallback, this));
    }

    bool join() noexcept {
        return (0 == uv_thread_join(&thread));
    }

    Loop& loop() const noexcept { return *pLoop; }

private:
    std::shared_ptr<Loop> pLoop;
    std::shared_ptr<void> data;
    uv_thread_t thread;
    Task task;
};


class ThreadLocalStorage final {
    explicit ThreadLocalStorage(std::shared_ptr<Loop> ref) noexcept
        : pLoop{std::move(ref)}
    {
        uv_key_create(&key);
    }

public:
    template<typename... Args>
    static std::shared_ptr<ThreadLocalStorage> create(Args&&... args) {
        return std::shared_ptr<ThreadLocalStorage>{new ThreadLocalStorage{std::forward<Args>(args)...}};
    }

    ~ThreadLocalStorage() noexcept {
        uv_key_delete(&key);
    }

    template<typename T>
    T* get() noexcept {
        return static_cast<T*>(uv_key_get(&key));
    }

    template<typename T>
    void set(T *value) noexcept {
        return uv_key_set(&key, value);
    }

    Loop& loop() const noexcept { return *pLoop; }

private:
    std::shared_ptr<Loop> pLoop;
    uv_key_t key;
};


class Once final {
    explicit Once(std::shared_ptr<Loop> ref) noexcept
        : pLoop{std::move(ref)}
    { }

public:
    template<typename... Args>
    static std::shared_ptr<Once> create(Args&&... args) {
        return std::shared_ptr<Once>{new Once{std::forward<Args>(args)...}};
    }

    template<typename F>
    static void once(F &&f) noexcept {
        using CallbackType = void (*)(void);
        static_assert(std::is_convertible<F, CallbackType>::value, "!");
        CallbackType cb = f;
        uv_once(&guard, cb);
    }

    Loop& loop() const noexcept { return *pLoop; }

private:
    std::shared_ptr<Loop> pLoop;
    static uv_once_t guard;
};

uv_once_t Once::guard = UV_ONCE_INIT;


class Mutex final {
    friend class Condition;

    explicit Mutex(std::shared_ptr<Loop> ref) noexcept
        : pLoop{std::move(ref)}
    {
        uv_mutex_init(&mutex);
    }

public:
    template<typename... Args>
    static std::shared_ptr<Mutex> create(Args&&... args) {
        return std::shared_ptr<Mutex>{new Mutex{std::forward<Args>(args)...}};
    }

    ~Mutex() noexcept {
        uv_mutex_destroy(&mutex);
    }

    void lock() noexcept {<commit_msg>bug fixing<commit_after>#pragma once


#include <type_traits>
#include <utility>
#include <memory>
#include <string>
#include <uv.h>
#include "loop.hpp"


namespace uvw {


class Thread;
class ThreadLocalStorage;
class Once;
class Mutex;
class RWLock;
class Semaphore;
class Condition;
class Barrier;


class Thread final {
    using InternalTask = std::function<void(std::shared_ptr<void>)>;

    static void createCallback(void *arg) {
        Thread &thread = *(static_cast<Thread*>(arg));
        thread.task(thread.data);
    }

    explicit Thread(std::shared_ptr<Loop> ref, InternalTask t, std::shared_ptr<void> d = nullptr) noexcept
        : pLoop{std::move(ref)},
          data{std::move(d)},
          thread{},
          task{std::move(t)}
    { }

public:
    using Task = InternalTask;
    using Type = uv_thread_t;

    template<typename... Args>
    static std::shared_ptr<Thread> create(Args&&... args) {
        return std::shared_ptr<Thread>{new Thread{std::forward<Args>(args)...}};
    }

    static Type self() noexcept {
        return uv_thread_self();
    }

    static bool equal(const Thread &tl, const Thread &tr) noexcept {
        return !(0 == uv_thread_equal(&tl.thread, &tr.thread));
    }

    ~Thread() noexcept {
        uv_thread_join(&thread);
    }

    bool run() noexcept {
        return (0 == uv_thread_create(&thread, &createCallback, this));
    }

    bool join() noexcept {
        return (0 == uv_thread_join(&thread));
    }

    Loop& loop() const noexcept { return *pLoop; }

private:
    std::shared_ptr<Loop> pLoop;
    std::shared_ptr<void> data;
    uv_thread_t thread;
    Task task;
};


class ThreadLocalStorage final {
    explicit ThreadLocalStorage(std::shared_ptr<Loop> ref) noexcept
        : pLoop{std::move(ref)}
    {
        uv_key_create(&key);
    }

public:
    template<typename... Args>
    static std::shared_ptr<ThreadLocalStorage> create(Args&&... args) {
        return std::shared_ptr<ThreadLocalStorage>{new ThreadLocalStorage{std::forward<Args>(args)...}};
    }

    ~ThreadLocalStorage() noexcept {
        uv_key_delete(&key);
    }

    template<typename T>
    T* get() noexcept {
        return static_cast<T*>(uv_key_get(&key));
    }

    template<typename T>
    void set(T *value) noexcept {
        return uv_key_set(&key, value);
    }

    Loop& loop() const noexcept { return *pLoop; }

private:
    std::shared_ptr<Loop> pLoop;
    uv_key_t key;
};


class Once final {
    explicit Once(std::shared_ptr<Loop> ref) noexcept
        : pLoop{std::move(ref)}
    { }

    static uv_once_t* guard() noexcept {
        static uv_once_t once = UV_ONCE_INIT;
        return &once;
    }

public:
    template<typename... Args>
    static std::shared_ptr<Once> create(Args&&... args) {
        return std::shared_ptr<Once>{new Once{std::forward<Args>(args)...}};
    }

    template<typename F>
    static void once(F &&f) noexcept {
        using CallbackType = void (*)(void);
        static_assert(std::is_convertible<F, CallbackType>::value, "!");
        CallbackType cb = f;
        uv_once(guard(), cb);
    }

    Loop& loop() const noexcept { return *pLoop; }

private:
    std::shared_ptr<Loop> pLoop;
};


class Mutex final {
    friend class Condition;

    explicit Mutex(std::shared_ptr<Loop> ref) noexcept
        : pLoop{std::move(ref)}
    {
        uv_mutex_init(&mutex);
    }

public:
    template<typename... Args>
    static std::shared_ptr<Mutex> create(Args&&... args) {
        return std::shared_ptr<Mutex>{new Mutex{std::forward<Args>(args)...}};
    }

    ~Mutex() noexcept {
        uv_mutex_destroy(&mutex);
    }

    void lock() noexcept {
{'fraction_non_alphanumeric': 0.1439966992160638, 'fraction_numerical': 0.000825195984046211, 'mean_word_length': 3.04, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7514117', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2359, 'n_words': 513}