| 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) { |
| 32 |
mydata = count; |
| 33 |
} |
| 34 |
|
| 35 |
void TestThread::RootFunction() |
| 36 |
{ |
| 37 |
SetPriority(RCThread::high); |
| 38 |
printf("TestThread::RootFunction %d did it\n", mydata); |
| 39 |
} /* TestThread::RootFunction */ |
| 40 |
|
| 41 |
class Foo1 |
| 42 |
{ |
| 43 |
public: |
| 44 |
Foo1(); |
| 45 |
virtual ~Foo1(); |
| 46 |
|
| 47 |
TestThread *thread; |
| 48 |
PRIntn data; |
| 49 |
}; |
| 50 |
|
| 51 |
Foo1::Foo1() |
| 52 |
{ |
| 53 |
data = 0xafaf; |
| 54 |
thread = new TestThread(RCThread::joinable, 0xafaf); |
| 55 |
thread->Start(); |
| 56 |
} |
| 57 |
|
| 58 |
Foo1::~Foo1() |
| 59 |
{ |
| 60 |
PRStatus rv = thread->Join(); |
| 61 |
PR_ASSERT(PR_SUCCESS == rv); |
| 62 |
} /* Foo1::~Foo1 */ |
| 63 |
|
| 64 |
PRIntn main(PRIntn argc, char **agrv) |
| 65 |
{ |
| 66 |
PRStatus status; |
| 67 |
PRIntn count = 100; |
| 68 |
RCThread *thread[10]; |
| 69 |
while (--count > 0) |
| 70 |
{ |
| 71 |
TestThread *thread = new TestThread(RCThread::joinable, count); |
| 72 |
status = thread->Start(); /* have to remember to start it */ |
| 73 |
PR_ASSERT(PR_SUCCESS == status); |
| 74 |
status = thread->Join(); /* this should work */ |
| 75 |
PR_ASSERT(PR_SUCCESS == status); |
| 76 |
} |
| 77 |
while (++count < 100) |
| 78 |
{ |
| 79 |
TestThread *thread = new TestThread(RCThread::unjoinable, count); |
| 80 |
status = thread->Start(); /* have to remember to start it */ |
| 81 |
PR_ASSERT(PR_SUCCESS == status); |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
Foo1 *foo1 = new Foo1(); |
| 86 |
PR_ASSERT(NULL != foo1); |
| 87 |
delete foo1; |
| 88 |
} |
| 89 |
|
| 90 |
{ |
| 91 |
for (count = 0; count < 10; ++count) |
| 92 |
{ |
| 93 |
thread[count] = new TestThread( RCThread::joinable, count); |
| 94 |
status = thread[count]->Start(); /* have to remember to start it */ |
| 95 |
PR_ASSERT(PR_SUCCESS == status); |
| 96 |
} |
| 97 |
for (count = 0; count < 10; ++count) |
| 98 |
{ |
| 99 |
PRStatus rv = thread[count]->Join(); |
| 100 |
PR_ASSERT(PR_SUCCESS == rv); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
(void)RCPrimordialThread::Cleanup(); |
| 105 |
|
| 106 |
return 0; |
| 107 |
} /* main */ |
| 108 |
|
| 109 |
/* thread.cpp */ |
| 110 |
|