|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* thread.cpp - a test program */ |
|
7 |
|
8 #include "rcthread.h" |
|
9 |
|
10 #include <prlog.h> |
|
11 |
|
12 #include <stdio.h> |
|
13 |
|
14 class TestThread: public RCThread |
|
15 { |
|
16 public: |
|
17 TestThread(RCThread::State state, PRIntn count); |
|
18 |
|
19 virtual void RootFunction(); |
|
20 |
|
21 protected: |
|
22 virtual ~TestThread(); |
|
23 |
|
24 private: |
|
25 PRUint32 mydata; |
|
26 }; |
|
27 |
|
28 TestThread::~TestThread() { } |
|
29 |
|
30 TestThread::TestThread(RCThread::State state, PRIntn count): |
|
31 RCThread(RCThread::global, state, 0) { mydata = count; } |
|
32 |
|
33 void TestThread::RootFunction() |
|
34 { |
|
35 SetPriority(RCThread::high); |
|
36 printf("TestThread::RootFunction %d did it\n", mydata); |
|
37 } /* TestThread::RootFunction */ |
|
38 |
|
39 class Foo1 |
|
40 { |
|
41 public: |
|
42 Foo1(); |
|
43 virtual ~Foo1(); |
|
44 |
|
45 TestThread *thread; |
|
46 PRIntn data; |
|
47 }; |
|
48 |
|
49 Foo1::Foo1() |
|
50 { |
|
51 data = 0xafaf; |
|
52 thread = new TestThread(RCThread::joinable, 0xafaf); |
|
53 thread->Start(); |
|
54 } |
|
55 |
|
56 Foo1::~Foo1() |
|
57 { |
|
58 PRStatus rv = thread->Join(); |
|
59 PR_ASSERT(PR_SUCCESS == rv); |
|
60 } /* Foo1::~Foo1 */ |
|
61 |
|
62 PRIntn main(PRIntn argc, char **agrv) |
|
63 { |
|
64 PRStatus status; |
|
65 PRIntn count = 100; |
|
66 RCThread *thread[10]; |
|
67 while (--count > 0) |
|
68 { |
|
69 TestThread *thread = new TestThread(RCThread::joinable, count); |
|
70 status = thread->Start(); /* have to remember to start it */ |
|
71 PR_ASSERT(PR_SUCCESS == status); |
|
72 status = thread->Join(); /* this should work */ |
|
73 PR_ASSERT(PR_SUCCESS == status); |
|
74 } |
|
75 while (++count < 100) |
|
76 { |
|
77 TestThread *thread = new TestThread(RCThread::unjoinable, count); |
|
78 status = thread->Start(); /* have to remember to start it */ |
|
79 PR_ASSERT(PR_SUCCESS == status); |
|
80 } |
|
81 |
|
82 { |
|
83 Foo1 *foo1 = new Foo1(); |
|
84 PR_ASSERT(NULL != foo1); |
|
85 delete foo1; |
|
86 } |
|
87 |
|
88 { |
|
89 for (count = 0; count < 10; ++count) |
|
90 { |
|
91 thread[count] = new TestThread( RCThread::joinable, count); |
|
92 status = thread[count]->Start(); /* have to remember to start it */ |
|
93 PR_ASSERT(PR_SUCCESS == status); |
|
94 } |
|
95 for (count = 0; count < 10; ++count) |
|
96 { |
|
97 PRStatus rv = thread[count]->Join(); |
|
98 PR_ASSERT(PR_SUCCESS == rv); |
|
99 } |
|
100 } |
|
101 |
|
102 (void)RCPrimordialThread::Cleanup(); |
|
103 |
|
104 return 0; |
|
105 } /* main */ |
|
106 |
|
107 /* thread.cpp */ |
|
108 |