xpcom/glue/CondVar.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/CondVar.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: sw=4 ts=4 et :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_CondVar_h
    1.11 +#define mozilla_CondVar_h
    1.12 +
    1.13 +#include "prcvar.h"
    1.14 +
    1.15 +#include "mozilla/BlockingResourceBase.h"
    1.16 +#include "mozilla/Mutex.h"
    1.17 +
    1.18 +#ifdef MOZILLA_INTERNAL_API
    1.19 +#include "GeckoProfiler.h"
    1.20 +#endif //MOZILLA_INTERNAL_API
    1.21 +
    1.22 +namespace mozilla {
    1.23 +
    1.24 +
    1.25 +/**
    1.26 + * CondVar
    1.27 + * Vanilla condition variable.  Please don't use this unless you have a 
    1.28 + * compelling reason --- Monitor provides a simpler API.
    1.29 + */
    1.30 +class NS_COM_GLUE CondVar : BlockingResourceBase
    1.31 +{
    1.32 +public:
    1.33 +    /**
    1.34 +     * CondVar
    1.35 +     *
    1.36 +     * The CALLER owns |lock|.
    1.37 +     *
    1.38 +     * @param aLock A Mutex to associate with this condition variable.
    1.39 +     * @param aName A name which can reference this monitor
    1.40 +     * @returns If failure, nullptr.
    1.41 +     *          If success, a valid Monitor* which must be destroyed
    1.42 +     *          by Monitor::DestroyMonitor()
    1.43 +     **/
    1.44 +    CondVar(Mutex& aLock, const char* aName) :
    1.45 +        BlockingResourceBase(aName, eCondVar),
    1.46 +        mLock(&aLock)
    1.47 +    {
    1.48 +        MOZ_COUNT_CTOR(CondVar);
    1.49 +        // |lock| must necessarily already be known to the deadlock detector
    1.50 +        mCvar = PR_NewCondVar(mLock->mLock);
    1.51 +        if (!mCvar)
    1.52 +            NS_RUNTIMEABORT("Can't allocate mozilla::CondVar");
    1.53 +    }
    1.54 +
    1.55 +    /**
    1.56 +     * ~CondVar
    1.57 +     * Clean up after this CondVar, but NOT its associated Mutex.
    1.58 +     **/
    1.59 +    ~CondVar()
    1.60 +    {
    1.61 +        NS_ASSERTION(mCvar && mLock,
    1.62 +                     "improperly constructed CondVar or double free");
    1.63 +        PR_DestroyCondVar(mCvar);
    1.64 +        mCvar = 0;
    1.65 +        mLock = 0;
    1.66 +        MOZ_COUNT_DTOR(CondVar);
    1.67 +    }
    1.68 +
    1.69 +#ifndef DEBUG
    1.70 +    /** 
    1.71 +     * Wait
    1.72 +     * @see prcvar.h 
    1.73 +     **/      
    1.74 +    nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT)
    1.75 +    {
    1.76 +
    1.77 +#ifdef MOZILLA_INTERNAL_API
    1.78 +        GeckoProfilerSleepRAII profiler_sleep;
    1.79 +#endif //MOZILLA_INTERNAL_API
    1.80 +        // NSPR checks for lock ownership
    1.81 +        return PR_WaitCondVar(mCvar, interval) == PR_SUCCESS
    1.82 +            ? NS_OK : NS_ERROR_FAILURE;
    1.83 +    }
    1.84 +#else
    1.85 +    nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT);
    1.86 +#endif // ifndef DEBUG
    1.87 +
    1.88 +    /** 
    1.89 +     * Notify
    1.90 +     * @see prcvar.h 
    1.91 +     **/      
    1.92 +    nsresult Notify()
    1.93 +    {
    1.94 +        // NSPR checks for lock ownership
    1.95 +        return PR_NotifyCondVar(mCvar) == PR_SUCCESS
    1.96 +            ? NS_OK : NS_ERROR_FAILURE;
    1.97 +    }
    1.98 +
    1.99 +    /** 
   1.100 +     * NotifyAll
   1.101 +     * @see prcvar.h 
   1.102 +     **/      
   1.103 +    nsresult NotifyAll()
   1.104 +    {
   1.105 +        // NSPR checks for lock ownership
   1.106 +        return PR_NotifyAllCondVar(mCvar) == PR_SUCCESS
   1.107 +            ? NS_OK : NS_ERROR_FAILURE;
   1.108 +    }
   1.109 +
   1.110 +#ifdef DEBUG
   1.111 +    /**
   1.112 +     * AssertCurrentThreadOwnsMutex
   1.113 +     * @see Mutex::AssertCurrentThreadOwns
   1.114 +     **/
   1.115 +    void AssertCurrentThreadOwnsMutex()
   1.116 +    {
   1.117 +        mLock->AssertCurrentThreadOwns();
   1.118 +    }
   1.119 +
   1.120 +    /**
   1.121 +     * AssertNotCurrentThreadOwnsMutex
   1.122 +     * @see Mutex::AssertNotCurrentThreadOwns
   1.123 +     **/
   1.124 +    void AssertNotCurrentThreadOwnsMutex()
   1.125 +    {
   1.126 +        mLock->AssertNotCurrentThreadOwns();
   1.127 +    }
   1.128 +
   1.129 +#else
   1.130 +    void AssertCurrentThreadOwnsMutex()
   1.131 +    {
   1.132 +    }
   1.133 +    void AssertNotCurrentThreadOwnsMutex()
   1.134 +    {
   1.135 +    }
   1.136 +
   1.137 +#endif  // ifdef DEBUG
   1.138 +
   1.139 +private:
   1.140 +    CondVar();
   1.141 +    CondVar(CondVar&);
   1.142 +    CondVar& operator=(CondVar&);
   1.143 +
   1.144 +    Mutex* mLock;
   1.145 +    PRCondVar* mCvar;
   1.146 +};
   1.147 +
   1.148 +
   1.149 +} // namespace mozilla
   1.150 +
   1.151 +
   1.152 +#endif  // ifndef mozilla_CondVar_h  

mercurial