security/manager/ssl/src/nsPSMBackgroundThread.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/manager/ssl/src/nsPSMBackgroundThread.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsPSMBackgroundThread.h"
     1.9 +#include "nsThreadUtils.h"
    1.10 +
    1.11 +using namespace mozilla;
    1.12 +
    1.13 +void nsPSMBackgroundThread::nsThreadRunner(void *arg)
    1.14 +{
    1.15 +  nsPSMBackgroundThread *self = static_cast<nsPSMBackgroundThread *>(arg);
    1.16 +  PR_SetCurrentThreadName(self->mName.BeginReading());
    1.17 +  self->Run();
    1.18 +}
    1.19 +
    1.20 +nsPSMBackgroundThread::nsPSMBackgroundThread()
    1.21 +: mThreadHandle(nullptr),
    1.22 +  mMutex("nsPSMBackgroundThread.mMutex"),
    1.23 +  mCond(mMutex, "nsPSMBackgroundThread.mCond"),
    1.24 +  mExitState(ePSMThreadRunning)
    1.25 +{
    1.26 +}
    1.27 +
    1.28 +nsresult nsPSMBackgroundThread::startThread(const nsCSubstring & name)
    1.29 +{
    1.30 +  mName = name;
    1.31 +
    1.32 +  mThreadHandle = PR_CreateThread(PR_USER_THREAD, nsThreadRunner, static_cast<void*>(this), 
    1.33 +    PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
    1.34 +
    1.35 +  NS_ASSERTION(mThreadHandle, "Could not create nsPSMBackgroundThread\n");
    1.36 +  
    1.37 +  if (!mThreadHandle)
    1.38 +    return NS_ERROR_OUT_OF_MEMORY;
    1.39 +
    1.40 +  return NS_OK;
    1.41 +}
    1.42 +
    1.43 +nsPSMBackgroundThread::~nsPSMBackgroundThread()
    1.44 +{
    1.45 +}
    1.46 +
    1.47 +bool
    1.48 +nsPSMBackgroundThread::exitRequested(const MutexAutoLock & /*proofOfLock*/) const
    1.49 +{
    1.50 +  return exitRequestedNoLock();
    1.51 +}
    1.52 +
    1.53 +nsresult
    1.54 +nsPSMBackgroundThread::postStoppedEventToMainThread(
    1.55 +    MutexAutoLock const & /*proofOfLock*/)
    1.56 +{
    1.57 +  NS_ASSERTION(PR_GetCurrentThread() == mThreadHandle,
    1.58 +               "Background thread stopped from another thread");
    1.59 +
    1.60 +  mExitState = ePSMThreadStopped;
    1.61 +  // requestExit is waiting for an event, so give it one.
    1.62 +  return NS_DispatchToMainThread(new nsRunnable());
    1.63 +}
    1.64 +
    1.65 +void nsPSMBackgroundThread::requestExit()
    1.66 +{
    1.67 +  NS_ASSERTION(NS_IsMainThread(),
    1.68 +               "nsPSMBackgroundThread::requestExit called off main thread.");
    1.69 +
    1.70 +  if (!mThreadHandle)
    1.71 +    return;
    1.72 +
    1.73 +  {
    1.74 +    MutexAutoLock threadLock(mMutex);
    1.75 +    if (mExitState < ePSMThreadStopRequested) {
    1.76 +      mExitState = ePSMThreadStopRequested;
    1.77 +      mCond.NotifyAll();
    1.78 +    }
    1.79 +  }
    1.80 +  
    1.81 +  nsCOMPtr<nsIThread> mainThread = do_GetCurrentThread();
    1.82 +  for (;;) {
    1.83 +    {
    1.84 +      MutexAutoLock threadLock(mMutex);
    1.85 +      if (mExitState == ePSMThreadStopped)
    1.86 +        break;
    1.87 +    }
    1.88 +    NS_ProcessPendingEvents(mainThread, PR_MillisecondsToInterval(50));
    1.89 +  }
    1.90 +
    1.91 +  PR_JoinThread(mThreadHandle);
    1.92 +  mThreadHandle = nullptr;
    1.93 +}

mercurial