]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
729e4ab9 A |
3 | /******************************************************************** |
4 | * COPYRIGHT: | |
2ca993e8 | 5 | * Copyright (c) 1997-2015, International Business Machines Corporation and |
729e4ab9 A |
6 | * others. All Rights Reserved. |
7 | ********************************************************************/ | |
8 | ||
9 | #ifndef SIMPLETHREAD_H | |
10 | #define SIMPLETHREAD_H | |
11 | ||
3d1f044b A |
12 | #include <thread> |
13 | #include "unicode/utypes.h" | |
729e4ab9 | 14 | |
3d1f044b A |
15 | /* |
16 | * Simple class for creating threads in ICU tests. | |
17 | * Originally created to provide a portable abstraction over platform | |
18 | * (POSIX or Win32) threading interfaces. | |
19 | * | |
20 | * New threaded tests should consider skipping this class and directly using C++ std library | |
21 | * threading functions. SimpleThread is retained primarily to support existing use. | |
22 | */ | |
23 | class SimpleThread | |
729e4ab9 | 24 | { |
2ca993e8 | 25 | public: |
729e4ab9 A |
26 | SimpleThread(); |
27 | virtual ~SimpleThread(); | |
3d1f044b | 28 | int32_t start(); // start the thread. Return 0 if successfull. |
2ca993e8 | 29 | void join(); // A thread must be joined before deleting its SimpleThread. |
729e4ab9 | 30 | |
3d1f044b | 31 | virtual void run() = 0; // Override this to provide the code to run |
729e4ab9 | 32 | // in the thread. |
2ca993e8 | 33 | private: |
3d1f044b | 34 | std::thread fThread = {}; |
2ca993e8 A |
35 | }; |
36 | ||
37 | ||
38 | class IntlTest; | |
39 | ||
40 | // ThreadPool - utililty class to simplify the spawning a group of threads by | |
41 | // a multi-threaded test. | |
42 | // | |
43 | // Usage: from within an intltest test function, | |
44 | // ThreadPool<TestClass> pool( | |
45 | // this, // The current intltest test object, | |
46 | // // of type "TestClass *" | |
47 | // numberOfThreads, // How many threads to spawn. | |
48 | // &TestClass::func); // The function to be run by each thread. | |
49 | // // It takes one int32_t parameter which | |
50 | // // is set to the thread number, 0 to numberOfThreads-1. | |
51 | // | |
52 | // pool.start(); // Start all threads running. | |
53 | // pool.join(); // Wait until all threads have terminated. | |
729e4ab9 | 54 | |
2ca993e8 A |
55 | class ThreadPoolBase { |
56 | public: | |
57 | ThreadPoolBase(IntlTest *test, int32_t numThreads); | |
58 | virtual ~ThreadPoolBase(); | |
59 | ||
60 | void start(); | |
61 | void join(); | |
62 | ||
63 | protected: | |
64 | virtual void callFn(int32_t param) = 0; | |
65 | friend class ThreadPoolThread; | |
66 | ||
67 | IntlTest *fIntlTest; | |
68 | int32_t fNumThreads; | |
69 | SimpleThread **fThreads; | |
729e4ab9 A |
70 | }; |
71 | ||
2ca993e8 A |
72 | |
73 | template<class TestClass> | |
74 | class ThreadPool : public ThreadPoolBase { | |
75 | private: | |
76 | void (TestClass::*fRunFnPtr)(int32_t); | |
77 | public: | |
78 | ThreadPool(TestClass *test, int howMany, void (TestClass::*runFnPtr)(int32_t threadNumber)) : | |
79 | ThreadPoolBase(test, howMany), fRunFnPtr(runFnPtr) {}; | |
80 | virtual ~ThreadPool() {}; | |
81 | private: | |
82 | virtual void callFn(int32_t param) { | |
83 | TestClass *test = dynamic_cast<TestClass *>(fIntlTest); | |
84 | (test->*fRunFnPtr)(param); | |
85 | } | |
86 | }; | |
729e4ab9 | 87 | #endif |