aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/source/ThreadSet.cpp
blob: ee3895fe9dee8dc8eb4b47d6e96712667923ca5f (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "G3D/ThreadSet.h"

namespace G3D {

int ThreadSet::size() const {
    ThreadSet* me = const_cast<ThreadSet*>(this);
    me->m_lock.lock();
    int s = m_thread.size();
    me->m_lock.unlock();
    return s;
}


int ThreadSet::numStarted() const {
    ThreadSet* me = const_cast<ThreadSet*>(this);
    me->m_lock.lock();
    int count = 0;
    for (int i = 0; i < m_thread.size(); ++i) {
        if (m_thread[i]->started()) {
            ++count;
        }
    }
    me->m_lock.unlock();
    return count;
}
    
    
void ThreadSet::start(GThread::SpawnBehavior lastBehavior) const {
    ThreadSet* me = const_cast<ThreadSet*>(this);

    Array<GThreadRef> unstarted;
    me->m_lock.lock();
    // Find the unstarted threads
    for (int i = 0; i < m_thread.size(); ++i) {
        if (! m_thread[i]->started()) {
            unstarted.append(m_thread[i]);
        }
    }

    int last = unstarted.size();
    if (lastBehavior == GThread::USE_CURRENT_THREAD) {
        // Save the last unstarted for the current thread
        --last;
    }

    for (int i = 0; i < last; ++i) {
        unstarted[i]->start(GThread::USE_NEW_THREAD);
    }

    me->m_lock.unlock();

    // Start the last one on my thread
    if ((unstarted.size() > 0) && (lastBehavior == GThread::USE_CURRENT_THREAD)) {
        unstarted.last()->start(GThread::USE_CURRENT_THREAD);
    }
}
    

void ThreadSet::terminate() const {
    ThreadSet* me = const_cast<ThreadSet*>(this);
    me->m_lock.lock();
    for (int i = 0; i < m_thread.size(); ++i) {
        if (m_thread[i]->started()) {
            m_thread[i]->terminate();
        }
    }
    me->m_lock.unlock();
}
    

void ThreadSet::waitForCompletion() const {
    ThreadSet* me = const_cast<ThreadSet*>(this);
    me->m_lock.lock();
    for (int i = 0; i < m_thread.size(); ++i) {
        if (m_thread[i]->started()) {
            m_thread[i]->waitForCompletion();
        }
    }
    me->m_lock.unlock();
}
    

int ThreadSet::removeCompleted() {
    m_lock.lock();
    for (int i = 0; i < m_thread.size(); ++i) {
        if (m_thread[i]->completed()) {
            m_thread.fastRemove(i);
            --i;
        }
    }
    
    int s = m_thread.size();
    m_lock.unlock();
    return s;
}


void ThreadSet::clear() {
    m_lock.lock();
    m_thread.clear();
    m_lock.unlock();
}
    

int ThreadSet::insert(const ThreadRef& t) {
    m_lock.lock();
    bool found = false;
    for (int i = 0; i < m_thread.size() && ! found; ++i) {
        found = (m_thread[i] == t);
    }
    if (! found) {
        m_thread.append(t);
    }
    int s = m_thread.size();
    m_lock.unlock();
    return s;
}
    

bool ThreadSet::remove(const ThreadRef& t) {
    m_lock.lock();
    bool found = false;
    for (int i = 0; i < m_thread.size() && ! found; ++i) {
        found = (m_thread[i] == t);
        if (found) {
            m_thread.fastRemove(i);
        }
    }
    m_lock.unlock();
    return found;
}
    

bool ThreadSet::contains(const ThreadRef& t) const {
    ThreadSet* me = const_cast<ThreadSet*>(this);
    me->m_lock.lock();
    bool found = false;
    for (int i = 0; i < m_thread.size() && ! found; ++i) {
        found = (m_thread[i] == t);
    }
    me->m_lock.unlock();
    return found;
}

   
ThreadSet::Iterator ThreadSet::begin() {
    return m_thread.begin();
}
    

ThreadSet::Iterator ThreadSet::end() {
    return m_thread.end();
}
    

ThreadSet::ConstIterator ThreadSet::begin() const {
    return m_thread.begin();
}
    
    
ThreadSet::ConstIterator ThreadSet::end() const {
    return m_thread.end();
}


} // namespace G3D