nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / src / cplus / rcthread.cpp
blob25a9810fecfa6d70f529a4abde4f4454d9177707
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* RCThread.cpp - C++ wrapper on NSPR */
40 #include "rcthread.h"
41 #include "rcinrval.h"
43 #include <prmem.h>
44 #include <prlog.h>
45 #include <stdio.h>
46 #include <prinit.h>
48 static RCPrimordialThread *primordial = NULL;
50 void nas_Root(void *arg)
52 RCThread *him = (RCThread*)arg;
53 while (RCThread::ex_unstarted == him->execution)
54 (void)PR_Sleep(PR_INTERVAL_NO_TIMEOUT); /* wait for Start() */
55 him->RootFunction(); /* he gets a self reference */
56 if (PR_UNJOINABLE_THREAD == PR_GetThreadState(him->identity))
57 delete him;
58 } /* nas_Root */
60 RCThread::~RCThread() { }
62 RCThread::RCThread(): RCBase() { }
64 RCThread::RCThread(const RCThread&): RCBase()
66 PR_NOT_REACHED("Cannot call thread copy constructor");
67 } /* RCThread::RCThread */
69 RCThread::RCThread(
70 RCThread::Scope scope, RCThread::State join, PRUint32 stackSize):
71 RCBase()
73 execution = ex_unstarted;
74 identity = PR_CreateThread(
75 PR_USER_THREAD, nas_Root, this,
76 PR_GetThreadPriority(PR_GetCurrentThread()),
77 (PRThreadScope)scope, (PRThreadState)join, stackSize);
78 } /* RCThread::RCThread */
80 void RCThread::operator=(const RCThread&)
82 PR_NOT_REACHED("Cannot call thread assignment operator");
83 } /* RCThread::operator= */
86 PRStatus RCThread::Start()
88 PRStatus rv;
89 /* This is an unsafe check, but not too critical */
90 if (RCThread::ex_unstarted == execution)
92 execution = RCThread::ex_started;
93 rv = PR_Interrupt(identity);
94 PR_ASSERT(PR_SUCCESS == rv);
96 else
98 rv = PR_FAILURE;
99 PR_SetError(PR_INVALID_STATE_ERROR, 0);
101 return rv;
102 } /* RCThread::Start */
104 PRStatus RCThread::Join()
106 PRStatus rv;
107 if (RCThread::ex_unstarted == execution)
109 rv = PR_FAILURE;
110 PR_SetError(PR_INVALID_STATE_ERROR, 0);
112 else rv = PR_JoinThread(identity);
113 if (PR_SUCCESS == rv) delete this;
114 return rv;
115 } /* RCThread::Join */
117 PRStatus RCThread::Interrupt()
119 PRStatus rv;
120 if (RCThread::ex_unstarted == execution)
122 rv = PR_FAILURE;
123 PR_SetError(PR_INVALID_STATE_ERROR, 0);
125 else rv = PR_Interrupt(identity);
126 return rv;
127 } /* RCThread::Interrupt */
129 void RCThread::ClearInterrupt() { PR_ClearInterrupt(); }
131 void RCThread::SetPriority(RCThread::Priority new_priority)
132 { PR_SetThreadPriority(identity, (PRThreadPriority)new_priority); }
134 PRThread *RCThread::Self()
135 { return PR_GetCurrentThread(); }
137 RCThread::Scope RCThread::GetScope() const
138 { return (RCThread::Scope)PR_GetThreadScope(identity); }
140 RCThread::State RCThread::GetState() const
141 { return (RCThread::State)PR_GetThreadState(identity); }
143 RCThread::Priority RCThread::GetPriority() const
144 { return (RCThread::Priority)PR_GetThreadPriority(identity); }
146 static void _rc_PDDestructor(RCThreadPrivateData* privateData)
148 PR_ASSERT(NULL != privateData);
149 privateData->Release();
152 static PRThreadPrivateDTOR _tpd_dtor = (PRThreadPrivateDTOR)_rc_PDDestructor;
154 PRStatus RCThread::NewPrivateIndex(PRUintn* index)
155 { return PR_NewThreadPrivateIndex(index, _tpd_dtor); }
157 PRStatus RCThread::SetPrivateData(PRUintn index)
158 { return PR_SetThreadPrivate(index, NULL); }
160 PRStatus RCThread::SetPrivateData(PRUintn index, RCThreadPrivateData* data)
162 return PR_SetThreadPrivate(index, data);
165 RCThreadPrivateData* RCThread::GetPrivateData(PRUintn index)
166 { return (RCThreadPrivateData*)PR_GetThreadPrivate(index); }
168 PRStatus RCThread::Sleep(const RCInterval& ticks)
169 { PRIntervalTime tmo = ticks; return PR_Sleep(tmo); }
171 RCPrimordialThread *RCThread::WrapPrimordialThread()
174 ** This needs to take more care in insuring that the thread
175 ** being wrapped is really the primordial thread. This code
176 ** is assuming that the caller is the primordial thread, and
177 ** there's nothing to insure that.
179 if (NULL == primordial)
181 /* it doesn't have to be perfect */
182 RCPrimordialThread *me = new RCPrimordialThread();
183 PR_ASSERT(NULL != me);
184 if (NULL == primordial)
186 primordial = me;
187 me->execution = RCThread::ex_started;
188 me->identity = PR_GetCurrentThread();
190 else delete me; /* somebody beat us to it */
192 return primordial;
193 } /* RCThread::WrapPrimordialThread */
195 RCPrimordialThread::RCPrimordialThread(): RCThread() { }
197 RCPrimordialThread::~RCPrimordialThread() { }
199 void RCPrimordialThread::RootFunction()
201 PR_NOT_REACHED("Primordial thread calling root function");
202 } /* RCPrimordialThread::RootFunction */
204 PRStatus RCPrimordialThread::Cleanup() { return PR_Cleanup(); }
206 PRStatus RCPrimordialThread::SetVirtualProcessors(PRIntn count)
208 PR_SetConcurrency(count);
209 return PR_SUCCESS;
210 } /* SetVirutalProcessors */
212 RCThreadPrivateData::RCThreadPrivateData() { }
214 RCThreadPrivateData::RCThreadPrivateData(
215 const RCThreadPrivateData& him) { }
217 RCThreadPrivateData::~RCThreadPrivateData() { }
219 /* RCThread.c */