Line data Source code
1 :
2 : /* Copyright (c) 2006-2014, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #include <test.h>
19 : #include <lunchbox/clock.h>
20 : #include <lunchbox/sleep.h>
21 : #include <lunchbox/thread.h>
22 : #include <iostream>
23 :
24 : #define NTHREADS 256
25 :
26 513 : class LoadThread : public lunchbox::Thread
27 : {
28 : public:
29 513 : virtual ~LoadThread() {}
30 256 : virtual void run() {}
31 : };
32 :
33 : class InitThread : public LoadThread
34 : {
35 : public:
36 257 : InitThread() : initLeft( false ) {}
37 257 : virtual ~InitThread() {}
38 :
39 256 : virtual bool init()
40 : {
41 256 : lunchbox::sleep( 10 );
42 256 : initLeft = true;
43 256 : return true;
44 : }
45 :
46 256 : virtual void run()
47 : {
48 256 : TEST( !join( ));
49 256 : exit();
50 0 : }
51 :
52 : bool initLeft;
53 : };
54 :
55 1 : class FailThread : public InitThread
56 : {
57 : public:
58 1 : virtual ~FailThread() {}
59 :
60 1 : virtual bool init()
61 : {
62 1 : return false;
63 : }
64 : };
65 :
66 1 : int main( int, char** )
67 : {
68 257 : LoadThread loadThreads[NTHREADS];
69 2 : lunchbox::Clock clock;
70 :
71 257 : for( size_t i=0; i<NTHREADS; ++i )
72 256 : TEST( loadThreads[i].start( ));
73 :
74 257 : for( size_t i=0; i<NTHREADS; ++i )
75 256 : TEST( loadThreads[i].join( ));
76 1 : const float time = clock.getTimef();
77 1 : std::cout << "Spawned and joined " << NTHREADS << " loadThreads in "
78 2 : << time << " ms (" << (NTHREADS/time) << " threads/ms)"
79 1 : << std::endl;
80 :
81 257 : for( size_t i=0; i<NTHREADS; ++i )
82 256 : TEST( loadThreads[i].isStopped( ));
83 :
84 2 : InitThread initThreads[NTHREADS];
85 :
86 1 : clock.reset();
87 257 : for( size_t i=0; i<NTHREADS; ++i )
88 : {
89 256 : TEST( initThreads[i].start( ));
90 256 : TEST( initThreads[i].initLeft == true );
91 : }
92 : #ifdef _MSC_VER // resolution of Sleep is not high enough...
93 : TESTINFO( clock.getTimef() + 1.f > NTHREADS * 10, clock.getTimef( ));
94 : #else
95 1 : TESTINFO( clock.getTimef() > NTHREADS * 10, clock.getTimef( ));
96 : #endif
97 :
98 257 : for( size_t i=0; i<NTHREADS; ++i )
99 256 : TEST( initThreads[i].join( ));
100 :
101 2 : FailThread failThread;
102 1 : TEST( !failThread.start( ));
103 1 : TEST( !failThread.isRunning( ));
104 1 : TEST( failThread.isStopped( ));
105 1 : TEST( !failThread.join( ));
106 :
107 258 : return EXIT_SUCCESS;
108 3 : }
|