/**************************************************************************
 *
 * Copyright 2011-2021 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 **************************************************************************/


#pragma once


#include_next <thread>

#include <assert.h>
#include <process.h>
#include <windows.h>

#include <functional>
#include <chrono>


namespace std {


    class thread {
    public:
        typedef HANDLE native_handle_type;

        inline
        thread() :
            _native_handle(nullptr)
        {
        }

        inline
        thread(thread &&other) :
            _native_handle(other._native_handle)
        {
            other._native_handle = nullptr;
        }

        thread(const thread &other) = delete;

        inline
        ~thread() {
        }

        static unsigned
        hardware_concurrency(void) {
            SYSTEM_INFO si;
            GetSystemInfo(&si);
            return si.dwNumberOfProcessors;
        }

        template< class Function, class... Args >
        explicit thread(Function &&f, Args&&... args) {
            auto bound = std::bind(std::forward<Function>(f), std::forward<Args>(args)...);
            auto data = new decltype(bound) (std::move(bound));
            _native_handle = _create(data);
        }

        inline thread &
        operator =(thread &&other) {
            assert(_native_handle == nullptr);
            _native_handle = other._native_handle;
            other._native_handle = nullptr;
            return *this;
        }

        inline bool
        joinable(void) const {
            return _native_handle != nullptr;
        }

        inline void
        join() {
            WaitForSingleObject(_native_handle, INFINITE);
        }

        inline void
        detach() {
            _native_handle = nullptr;
        }

    private:
        native_handle_type _native_handle;

        template< typename Param >
        static
        unsigned __stdcall
        _callback(void *lpParameter) {
            Param *pParam = static_cast<Param *>(lpParameter);
            (*pParam)();
            delete pParam;
            return 0;
        }

        template< typename Param >
        static inline native_handle_type
        _create(Param *function) {
            uintptr_t handle =_beginthreadex(NULL, 0, &_callback<Param>, function, 0, NULL);
            return reinterpret_cast<HANDLE>(handle);
        }
    };

    namespace this_thread {

        template< class Rep, class Period >
        inline void
        sleep_for(const std::chrono::duration<Rep, Period> &sleep_duration) {
            DWORD dwMiliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(sleep_duration).count();
            Sleep(dwMiliseconds);
        }

    }

} /* namespace std */
