1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/cplus/tests/thread.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* thread.cpp - a test program */ 1.10 + 1.11 +#include "rcthread.h" 1.12 + 1.13 +#include <prlog.h> 1.14 + 1.15 +#include <stdio.h> 1.16 + 1.17 +class TestThread: public RCThread 1.18 +{ 1.19 +public: 1.20 + TestThread(RCThread::State state, PRIntn count); 1.21 + 1.22 + virtual void RootFunction(); 1.23 + 1.24 +protected: 1.25 + virtual ~TestThread(); 1.26 + 1.27 +private: 1.28 + PRUint32 mydata; 1.29 +}; 1.30 + 1.31 +TestThread::~TestThread() { } 1.32 + 1.33 +TestThread::TestThread(RCThread::State state, PRIntn count): 1.34 + RCThread(RCThread::global, state, 0) { mydata = count; } 1.35 + 1.36 +void TestThread::RootFunction() 1.37 +{ 1.38 + SetPriority(RCThread::high); 1.39 + printf("TestThread::RootFunction %d did it\n", mydata); 1.40 +} /* TestThread::RootFunction */ 1.41 + 1.42 +class Foo1 1.43 +{ 1.44 +public: 1.45 + Foo1(); 1.46 + virtual ~Foo1(); 1.47 + 1.48 + TestThread *thread; 1.49 + PRIntn data; 1.50 +}; 1.51 + 1.52 +Foo1::Foo1() 1.53 +{ 1.54 + data = 0xafaf; 1.55 + thread = new TestThread(RCThread::joinable, 0xafaf); 1.56 + thread->Start(); 1.57 +} 1.58 + 1.59 +Foo1::~Foo1() 1.60 +{ 1.61 + PRStatus rv = thread->Join(); 1.62 + PR_ASSERT(PR_SUCCESS == rv); 1.63 +} /* Foo1::~Foo1 */ 1.64 + 1.65 +PRIntn main(PRIntn argc, char **agrv) 1.66 +{ 1.67 + PRStatus status; 1.68 + PRIntn count = 100; 1.69 + RCThread *thread[10]; 1.70 + while (--count > 0) 1.71 + { 1.72 + TestThread *thread = new TestThread(RCThread::joinable, count); 1.73 + status = thread->Start(); /* have to remember to start it */ 1.74 + PR_ASSERT(PR_SUCCESS == status); 1.75 + status = thread->Join(); /* this should work */ 1.76 + PR_ASSERT(PR_SUCCESS == status); 1.77 + } 1.78 + while (++count < 100) 1.79 + { 1.80 + TestThread *thread = new TestThread(RCThread::unjoinable, count); 1.81 + status = thread->Start(); /* have to remember to start it */ 1.82 + PR_ASSERT(PR_SUCCESS == status); 1.83 + } 1.84 + 1.85 + { 1.86 + Foo1 *foo1 = new Foo1(); 1.87 + PR_ASSERT(NULL != foo1); 1.88 + delete foo1; 1.89 + } 1.90 + 1.91 + { 1.92 + for (count = 0; count < 10; ++count) 1.93 + { 1.94 + thread[count] = new TestThread( RCThread::joinable, count); 1.95 + status = thread[count]->Start(); /* have to remember to start it */ 1.96 + PR_ASSERT(PR_SUCCESS == status); 1.97 + } 1.98 + for (count = 0; count < 10; ++count) 1.99 + { 1.100 + PRStatus rv = thread[count]->Join(); 1.101 + PR_ASSERT(PR_SUCCESS == rv); 1.102 + } 1.103 + } 1.104 + 1.105 + (void)RCPrimordialThread::Cleanup(); 1.106 + 1.107 + return 0; 1.108 +} /* main */ 1.109 + 1.110 +/* thread.cpp */ 1.111 +