Switch to unified view

a/sc2src/workqueue.h b/sc2src/workqueue.h
1
/*   Copyright (C) 2012-2016 J.F.Dockes
1
/* Copyright (C) 2006-2016 J.F.Dockes
2
 *
2
 *   This program is free software; you can redistribute it and/or modify
3
 * This library is free software; you can redistribute it and/or
3
 *   it under the terms of the GNU General Public License as published by
4
 * modify it under the terms of the GNU Lesser General Public
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 * License as published by the Free Software Foundation; either
5
 *   (at your option) any later version.
6
 * version 2.1 of the License, or (at your option) any later version.
6
 *
7
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 * This library is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 *   GNU General Public License for more details.
11
 * Lesser General Public License for more details.
11
 *
12
 *
12
 *   You should have received a copy of the GNU General Public License
13
 * You should have received a copy of the GNU Lesser General Public
13
 *   along with this program; if not, write to the
14
 * License along with this library; if not, write to the Free Software
14
 *   Free Software Foundation, Inc.,
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 *   02110-1301 USA
16
 */
17
 */
17
#ifndef _WORKQUEUE_H_INCLUDED_
18
#ifndef _WORKQUEUE_H_INCLUDED_
18
#define _WORKQUEUE_H_INCLUDED_
19
#define _WORKQUEUE_H_INCLUDED_
19
20
20
#include <thread>
21
#include <thread>
22
#if HAVE_STD_FUTURE
21
#include <future>
23
#include <future>
24
#endif
22
#include <string>
25
#include <string>
23
#include <queue>
26
#include <queue>
24
#include <list>
27
#include <list>
25
#include <mutex>
28
#include <mutex>
26
#include <condition_variable>
29
#include <condition_variable>
...
...
74
     * @return true if ok.
77
     * @return true if ok.
75
     */
78
     */
76
    bool start(int nworkers, void *(workproc)(void *), void *arg) {
79
    bool start(int nworkers, void *(workproc)(void *), void *arg) {
77
        std::unique_lock<std::mutex> lock(m_mutex);
80
        std::unique_lock<std::mutex> lock(m_mutex);
78
        for (int i = 0; i < nworkers; i++) {
81
        for (int i = 0; i < nworkers; i++) {
82
            Worker w;
83
#if HAVE_STD_FUTURE
79
            std::packaged_task<void *(void *)> task(workproc);
84
            std::packaged_task<void *(void *)> task(workproc);
80
            Worker w;
81
            w.res = task.get_future();
85
            w.res = task.get_future();
82
            w.thr = std::thread(std::move(task), arg);
86
            w.thr = std::thread(std::move(task), arg);
87
#else
88
            w.thr = std::thread(workproc, arg);
89
#endif
83
            m_worker_threads.push_back(std::move(w));
90
            m_worker_threads.push_back(std::move(w));
84
        }
91
        }
85
        return true;
92
        return true;
86
    }
93
    }
87
94
...
...
187
                m_clientsleeps << "\n");
194
                m_clientsleeps << "\n");
188
        // Perform the thread joins and compute overall status
195
        // Perform the thread joins and compute overall status
189
        // Workers return (void*)1 if ok
196
        // Workers return (void*)1 if ok
190
        void *statusall = (void*)1;
197
        void *statusall = (void*)1;
191
        while (!m_worker_threads.empty()) {
198
        while (!m_worker_threads.empty()) {
199
#if HAVE_STD_FUTURE
192
            void *status = m_worker_threads.front().res.get();
200
            void *status = m_worker_threads.front().res.get();
201
#else
202
            void *status = (void*) 1;
203
#endif
193
            m_worker_threads.front().thr.join();
204
            m_worker_threads.front().thr.join();
194
            if (status == (void *)0) {
205
            if (status == (void *)0) {
195
                statusall = status;
206
                statusall = status;
196
            }
207
            }
197
            m_worker_threads.pop_front();
208
            m_worker_threads.pop_front();
...
...
303
        return isok;
314
        return isok;
304
    }
315
    }
305
316
306
    struct Worker {
317
    struct Worker {
307
        std::thread         thr;
318
        std::thread         thr;
319
#if HAVE_STD_FUTURE
308
        std::future<void *> res;
320
        std::future<void *> res;
321
#endif
309
    };
322
    };
310
    
323
    
311
    // Configuration
324
    // Configuration
312
    std::string m_name;
325
    std::string m_name;
313
    size_t m_high;
326
    size_t m_high;