summaryrefslogtreecommitdiff
path: root/thirdparty/etcpak/TaskDispatch.cpp
blob: b1ba17953bc4945fe765974b2ddc69dc9a4b7e01 (plain)
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
#include <assert.h>
#include <stdio.h>
#ifndef _MSC_VER
#include <pthread.h>
#endif

#include "Debug.hpp"
#include "System.hpp"
#include "TaskDispatch.hpp"

static TaskDispatch* s_instance = nullptr;

TaskDispatch::TaskDispatch( size_t workers )
    : m_exit( false )
    , m_jobs( 0 )
{
    assert( !s_instance );
    s_instance = this;

    assert( workers >= 1 );
    workers--;

    m_workers.reserve( workers );
    for( size_t i=0; i<workers; i++ )
    {
        char tmp[16];
        sprintf( tmp, "Worker %zu", i );
#ifdef _MSC_VER
        auto worker = std::thread( [this]{ Worker(); } );
        System::SetThreadName( worker, tmp );
#else // Using pthread.
        auto worker = std::thread( [this, tmp]{
#ifdef __APPLE__
            pthread_setname_np( tmp );
#else // Linux or MinGW.
            pthread_setname_np( pthread_self(), tmp );
#endif
            Worker();
        } );
#endif
        m_workers.emplace_back( std::move( worker ) );
    }

    DBGPRINT( "Task dispatcher with " << m_workers.size() + 1 << " workers" );
}

TaskDispatch::~TaskDispatch()
{
    m_exit = true;
    m_queueLock.lock();
    m_cvWork.notify_all();
    m_queueLock.unlock();

    for( auto& worker : m_workers )
    {
        worker.join();
    }

    assert( s_instance );
    s_instance = nullptr;
}

void TaskDispatch::Queue( const std::function<void(void)>& f )
{
    std::unique_lock<std::mutex> lock( s_instance->m_queueLock );
    s_instance->m_queue.emplace_back( f );
    const auto size = s_instance->m_queue.size();
    lock.unlock();
    if( size > 1 )
    {
        s_instance->m_cvWork.notify_one();
    }
}

void TaskDispatch::Queue( std::function<void(void)>&& f )
{
    std::unique_lock<std::mutex> lock( s_instance->m_queueLock );
    s_instance->m_queue.emplace_back( std::move( f ) );
    const auto size = s_instance->m_queue.size();
    lock.unlock();
    if( size > 1 )
    {
        s_instance->m_cvWork.notify_one();
    }
}

void TaskDispatch::Sync()
{
    std::unique_lock<std::mutex> lock( s_instance->m_queueLock );
    while( !s_instance->m_queue.empty() )
    {
        auto f = s_instance->m_queue.back();
        s_instance->m_queue.pop_back();
        lock.unlock();
        f();
        lock.lock();
    }
    s_instance->m_cvJobs.wait( lock, []{ return s_instance->m_jobs == 0; } );
}

void TaskDispatch::Worker()
{
    for(;;)
    {
        std::unique_lock<std::mutex> lock( m_queueLock );
        m_cvWork.wait( lock, [this]{ return !m_queue.empty() || m_exit; } );
        if( m_exit ) return;
        auto f = m_queue.back();
        m_queue.pop_back();
        m_jobs++;
        lock.unlock();
        f();
        lock.lock();
        m_jobs--;
        bool notify = m_jobs == 0 && m_queue.empty();
        lock.unlock();
        if( notify )
        {
            m_cvJobs.notify_all();
        }
    }
}