1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 1999-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8
9
10 #include "simplethread.h"
11
12 #include <thread>
13 #include "unicode/utypes.h"
14 #include "intltest.h"
15
16
SimpleThread()17 SimpleThread::SimpleThread() {
18 }
19
~SimpleThread()20 SimpleThread::~SimpleThread() {
21 this->join(); // Avoid crashes if user neglected to join().
22 }
23
start()24 int SimpleThread::start() {
25 fThread = std::thread(&SimpleThread::run, this);
26 return fThread.joinable() ? 0 : 1;
27 }
28
join()29 void SimpleThread::join() {
30 if (fThread.joinable()) {
31 fThread.join();
32 }
33 }
34
35
36
37 class ThreadPoolThread: public SimpleThread {
38 public:
ThreadPoolThread(ThreadPoolBase * pool,int32_t threadNum)39 ThreadPoolThread(ThreadPoolBase *pool, int32_t threadNum) : fPool(pool), fNum(threadNum) {}
run()40 virtual void run() override { fPool->callFn(fNum); }
41 ThreadPoolBase *fPool;
42 int32_t fNum;
43 };
44
45
ThreadPoolBase(IntlTest * test,int32_t howMany)46 ThreadPoolBase::ThreadPoolBase(IntlTest *test, int32_t howMany) :
47 fIntlTest(test), fNumThreads(howMany), fThreads(NULL) {
48 fThreads = new SimpleThread *[fNumThreads];
49 if (fThreads == NULL) {
50 fIntlTest->errln("%s:%d memory allocation failure.", __FILE__, __LINE__);
51 return;
52 }
53
54 for (int i=0; i<fNumThreads; i++) {
55 fThreads[i] = new ThreadPoolThread(this, i);
56 if (fThreads[i] == NULL) {
57 fIntlTest->errln("%s:%d memory allocation failure.", __FILE__, __LINE__);
58 }
59 }
60 }
61
start()62 void ThreadPoolBase::start() {
63 for (int i=0; i<fNumThreads; i++) {
64 if (fThreads && fThreads[i]) {
65 fThreads[i]->start();
66 }
67 }
68 }
69
join()70 void ThreadPoolBase::join() {
71 for (int i=0; i<fNumThreads; i++) {
72 if (fThreads && fThreads[i]) {
73 fThreads[i]->join();
74 }
75 }
76 }
77
~ThreadPoolBase()78 ThreadPoolBase::~ThreadPoolBase() {
79 if (fThreads) {
80 for (int i=0; i<fNumThreads; i++) {
81 delete fThreads[i];
82 fThreads[i] = NULL;
83 }
84 delete[] fThreads;
85 fThreads = NULL;
86 }
87 }
88