nsprpub/pr/src/threads/prmon.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/threads/prmon.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,346 @@
     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 +#include "primpl.h"
    1.10 +
    1.11 +/************************************************************************/
    1.12 +
    1.13 +/*
    1.14 + * Notifies just get posted to the monitor. The actual notification is done
    1.15 + * when the monitor is fully exited so that MP systems don't contend for a
    1.16 + * monitor that they can't enter.
    1.17 + */
    1.18 +static void _PR_PostNotifyToMonitor(PRMonitor *mon, PRBool broadcast)
    1.19 +{
    1.20 +    PR_ASSERT(mon != NULL);
    1.21 +    PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mon);
    1.22 +
    1.23 +    /* mon->notifyTimes is protected by the monitor, so we don't need to
    1.24 +     * acquire mon->lock.
    1.25 +     */
    1.26 +    if (broadcast)
    1.27 +        mon->notifyTimes = -1;
    1.28 +    else if (mon->notifyTimes != -1)
    1.29 +        mon->notifyTimes += 1;
    1.30 +}
    1.31 +
    1.32 +static void _PR_PostNotifiesFromMonitor(PRCondVar *cv, PRIntn times)
    1.33 +{
    1.34 +    PRStatus rv;
    1.35 +
    1.36 +    /*
    1.37 +     * Time to actually notify any waits that were affected while the monitor
    1.38 +     * was entered.
    1.39 +     */
    1.40 +    PR_ASSERT(cv != NULL);
    1.41 +    PR_ASSERT(times != 0);
    1.42 +    if (times == -1) {
    1.43 +        rv = PR_NotifyAllCondVar(cv);
    1.44 +        PR_ASSERT(rv == PR_SUCCESS);
    1.45 +    } else {
    1.46 +        while (times-- > 0) {
    1.47 +            rv = PR_NotifyCondVar(cv);
    1.48 +            PR_ASSERT(rv == PR_SUCCESS);
    1.49 +        }
    1.50 +    }
    1.51 +}
    1.52 +
    1.53 +/*
    1.54 +** Create a new monitor.
    1.55 +*/
    1.56 +PR_IMPLEMENT(PRMonitor*) PR_NewMonitor()
    1.57 +{
    1.58 +    PRMonitor *mon;
    1.59 +    PRStatus rv;
    1.60 +
    1.61 +    if (!_pr_initialized) _PR_ImplicitInitialization();
    1.62 +
    1.63 +    mon = PR_NEWZAP(PRMonitor);
    1.64 +    if (mon == NULL) {
    1.65 +        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
    1.66 +        return NULL;
    1.67 +    }
    1.68 +
    1.69 +    rv = _PR_InitLock(&mon->lock);
    1.70 +    PR_ASSERT(rv == PR_SUCCESS);
    1.71 +    if (rv != PR_SUCCESS)
    1.72 +        goto error1;
    1.73 +
    1.74 +    mon->owner = NULL;
    1.75 +
    1.76 +    rv = _PR_InitCondVar(&mon->entryCV, &mon->lock);
    1.77 +    PR_ASSERT(rv == PR_SUCCESS);
    1.78 +    if (rv != PR_SUCCESS)
    1.79 +        goto error2;
    1.80 +
    1.81 +    rv = _PR_InitCondVar(&mon->waitCV, &mon->lock);
    1.82 +    PR_ASSERT(rv == PR_SUCCESS);
    1.83 +    if (rv != PR_SUCCESS)
    1.84 +        goto error3;
    1.85 +
    1.86 +    mon->notifyTimes = 0;
    1.87 +    mon->entryCount = 0;
    1.88 +    mon->name = NULL;
    1.89 +    return mon;
    1.90 +
    1.91 +error3:
    1.92 +    _PR_FreeCondVar(&mon->entryCV);
    1.93 +error2:
    1.94 +    _PR_FreeLock(&mon->lock);
    1.95 +error1:
    1.96 +    PR_Free(mon);
    1.97 +    return NULL;
    1.98 +}
    1.99 +
   1.100 +PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name)
   1.101 +{
   1.102 +    PRMonitor* mon = PR_NewMonitor();
   1.103 +    if (mon)
   1.104 +        mon->name = name;
   1.105 +    return mon;
   1.106 +}
   1.107 +
   1.108 +/*
   1.109 +** Destroy a monitor. There must be no thread waiting on the monitor's
   1.110 +** condition variable. The caller is responsible for guaranteeing that the
   1.111 +** monitor is no longer in use.
   1.112 +*/
   1.113 +PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon)
   1.114 +{
   1.115 +    PR_ASSERT(mon != NULL);
   1.116 +    _PR_FreeCondVar(&mon->waitCV);
   1.117 +    _PR_FreeCondVar(&mon->entryCV);
   1.118 +    _PR_FreeLock(&mon->lock);
   1.119 +#if defined(DEBUG)
   1.120 +    memset(mon, 0xaf, sizeof(PRMonitor));
   1.121 +#endif
   1.122 +    PR_Free(mon);
   1.123 +}
   1.124 +
   1.125 +/*
   1.126 +** Enter the lock associated with the monitor.
   1.127 +*/
   1.128 +PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon)
   1.129 +{
   1.130 +    PRThread *me = _PR_MD_CURRENT_THREAD();
   1.131 +    PRStatus rv;
   1.132 +
   1.133 +    PR_ASSERT(mon != NULL);
   1.134 +    PR_Lock(&mon->lock);
   1.135 +    if (mon->entryCount != 0) {
   1.136 +        if (mon->owner == me)
   1.137 +            goto done;
   1.138 +        while (mon->entryCount != 0) {
   1.139 +            rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT);
   1.140 +            PR_ASSERT(rv == PR_SUCCESS);
   1.141 +        }
   1.142 +    }
   1.143 +    /* and now I have the monitor */
   1.144 +    PR_ASSERT(mon->notifyTimes == 0);
   1.145 +    PR_ASSERT(mon->owner == NULL);
   1.146 +    mon->owner = me;
   1.147 +
   1.148 +done:
   1.149 +    mon->entryCount += 1;
   1.150 +    rv = PR_Unlock(&mon->lock);
   1.151 +    PR_ASSERT(rv == PR_SUCCESS);
   1.152 +}
   1.153 +
   1.154 +/*
   1.155 +** Test and then enter the lock associated with the monitor if it's not
   1.156 +** already entered by some other thread. Return PR_FALSE if some other
   1.157 +** thread owned the lock at the time of the call.
   1.158 +*/
   1.159 +PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon)
   1.160 +{
   1.161 +    PRThread *me = _PR_MD_CURRENT_THREAD();
   1.162 +    PRStatus rv;
   1.163 +
   1.164 +    PR_ASSERT(mon != NULL);
   1.165 +    PR_Lock(&mon->lock);
   1.166 +    if (mon->entryCount != 0) {
   1.167 +        if (mon->owner == me)
   1.168 +            goto done;
   1.169 +        rv = PR_Unlock(&mon->lock);
   1.170 +        PR_ASSERT(rv == PR_SUCCESS);
   1.171 +        return PR_FALSE;
   1.172 +    }
   1.173 +    /* and now I have the monitor */
   1.174 +    PR_ASSERT(mon->notifyTimes == 0);
   1.175 +    PR_ASSERT(mon->owner == NULL);
   1.176 +    mon->owner = me;
   1.177 +
   1.178 +done:
   1.179 +    mon->entryCount += 1;
   1.180 +    rv = PR_Unlock(&mon->lock);
   1.181 +    PR_ASSERT(rv == PR_SUCCESS);
   1.182 +    return PR_TRUE;
   1.183 +}
   1.184 +
   1.185 +/*
   1.186 +** Exit the lock associated with the monitor once.
   1.187 +*/
   1.188 +PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon)
   1.189 +{
   1.190 +    PRThread *me = _PR_MD_CURRENT_THREAD();
   1.191 +    PRStatus rv;
   1.192 +
   1.193 +    PR_ASSERT(mon != NULL);
   1.194 +    PR_Lock(&mon->lock);
   1.195 +    /* the entries should be > 0 and we'd better be the owner */
   1.196 +    PR_ASSERT(mon->entryCount > 0);
   1.197 +    PR_ASSERT(mon->owner == me);
   1.198 +    if (mon->entryCount == 0 || mon->owner != me)
   1.199 +    {
   1.200 +        rv = PR_Unlock(&mon->lock);
   1.201 +        PR_ASSERT(rv == PR_SUCCESS);
   1.202 +        return PR_FAILURE;
   1.203 +    }
   1.204 +
   1.205 +    mon->entryCount -= 1;  /* reduce by one */
   1.206 +    if (mon->entryCount == 0)
   1.207 +    {
   1.208 +        /* and if it transitioned to zero - notify an entry waiter */
   1.209 +        /* make the owner unknown */
   1.210 +        mon->owner = NULL;
   1.211 +        if (mon->notifyTimes != 0) {
   1.212 +            _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes);
   1.213 +            mon->notifyTimes = 0;
   1.214 +        }
   1.215 +        rv = PR_NotifyCondVar(&mon->entryCV);
   1.216 +        PR_ASSERT(rv == PR_SUCCESS);
   1.217 +    }
   1.218 +    rv = PR_Unlock(&mon->lock);
   1.219 +    PR_ASSERT(rv == PR_SUCCESS);
   1.220 +    return PR_SUCCESS;
   1.221 +}
   1.222 +
   1.223 +/*
   1.224 +** Return the number of times that the current thread has entered the
   1.225 +** lock. Returns zero if the current thread has not entered the lock.
   1.226 +*/
   1.227 +PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon)
   1.228 +{
   1.229 +    PRThread *me = _PR_MD_CURRENT_THREAD();
   1.230 +    PRStatus rv;
   1.231 +    PRIntn count = 0;
   1.232 +
   1.233 +    PR_Lock(&mon->lock);
   1.234 +    if (mon->owner == me)
   1.235 +        count = mon->entryCount;
   1.236 +    rv = PR_Unlock(&mon->lock);
   1.237 +    PR_ASSERT(rv == PR_SUCCESS);
   1.238 +    return count;
   1.239 +}
   1.240 +
   1.241 +PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon)
   1.242 +{
   1.243 +#if defined(DEBUG) || defined(FORCE_PR_ASSERT)
   1.244 +    PRStatus rv;
   1.245 +
   1.246 +    PR_Lock(&mon->lock);
   1.247 +    PR_ASSERT(mon->entryCount != 0 &&
   1.248 +              mon->owner == _PR_MD_CURRENT_THREAD());
   1.249 +    rv = PR_Unlock(&mon->lock);
   1.250 +    PR_ASSERT(rv == PR_SUCCESS);
   1.251 +#endif
   1.252 +}
   1.253 +
   1.254 +/*
   1.255 +** Wait for a notify on the condition variable. Sleep for "ticks" amount
   1.256 +** of time (if "tick" is 0 then the sleep is indefinite). While
   1.257 +** the thread is waiting it exits the monitors lock (as if it called
   1.258 +** PR_ExitMonitor as many times as it had called PR_EnterMonitor).  When
   1.259 +** the wait has finished the thread regains control of the monitors lock
   1.260 +** with the same entry count as before the wait began.
   1.261 +**
   1.262 +** The thread waiting on the monitor will be resumed when the monitor is
   1.263 +** notified (assuming the thread is the next in line to receive the
   1.264 +** notify) or when the "ticks" elapses.
   1.265 +**
   1.266 +** Returns PR_FAILURE if the caller has not locked the lock associated
   1.267 +** with the condition variable.
   1.268 +** This routine can return PR_PENDING_INTERRUPT_ERROR if the waiting thread
   1.269 +** has been interrupted.
   1.270 +*/
   1.271 +PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks)
   1.272 +{
   1.273 +    PRStatus rv;
   1.274 +    PRUint32 saved_entries;
   1.275 +    PRThread *saved_owner;
   1.276 +
   1.277 +    PR_ASSERT(mon != NULL);
   1.278 +    PR_Lock(&mon->lock);
   1.279 +    /* the entries better be positive */
   1.280 +    PR_ASSERT(mon->entryCount > 0);
   1.281 +    /* and it better be owned by us */
   1.282 +    PR_ASSERT(mon->owner == _PR_MD_CURRENT_THREAD());  /* XXX return failure */
   1.283 +
   1.284 +    /* tuck these away 'till later */
   1.285 +    saved_entries = mon->entryCount;
   1.286 +    mon->entryCount = 0;
   1.287 +    saved_owner = mon->owner;
   1.288 +    mon->owner = NULL;
   1.289 +    /* If we have pending notifies, post them now. */
   1.290 +    if (mon->notifyTimes != 0) {
   1.291 +        _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes);
   1.292 +        mon->notifyTimes = 0;
   1.293 +    }
   1.294 +    rv = PR_NotifyCondVar(&mon->entryCV);
   1.295 +    PR_ASSERT(rv == PR_SUCCESS);
   1.296 +
   1.297 +    rv = PR_WaitCondVar(&mon->waitCV, ticks);
   1.298 +    PR_ASSERT(rv == PR_SUCCESS);
   1.299 +
   1.300 +    while (mon->entryCount != 0) {
   1.301 +        rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT);
   1.302 +        PR_ASSERT(rv == PR_SUCCESS);
   1.303 +    }
   1.304 +    PR_ASSERT(mon->notifyTimes == 0);
   1.305 +    /* reinstate the interesting information */
   1.306 +    mon->entryCount = saved_entries;
   1.307 +    mon->owner = saved_owner;
   1.308 +
   1.309 +    rv = PR_Unlock(&mon->lock);
   1.310 +    PR_ASSERT(rv == PR_SUCCESS);
   1.311 +    return rv;
   1.312 +}
   1.313 +
   1.314 +/*
   1.315 +** Notify the highest priority thread waiting on the condition
   1.316 +** variable. If a thread is waiting on the condition variable (using
   1.317 +** PR_Wait) then it is awakened and begins waiting on the monitor's lock.
   1.318 +*/
   1.319 +PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon)
   1.320 +{
   1.321 +    _PR_PostNotifyToMonitor(mon, PR_FALSE);
   1.322 +    return PR_SUCCESS;
   1.323 +}
   1.324 +
   1.325 +/*
   1.326 +** Notify all of the threads waiting on the condition variable. All of
   1.327 +** threads are notified in turn. The highest priority thread will
   1.328 +** probably acquire the monitor first when the monitor is exited.
   1.329 +*/
   1.330 +PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon)
   1.331 +{
   1.332 +    _PR_PostNotifyToMonitor(mon, PR_TRUE);
   1.333 +    return PR_SUCCESS;
   1.334 +}
   1.335 +
   1.336 +/************************************************************************/
   1.337 +
   1.338 +PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen)
   1.339 +{
   1.340 +    PRUint32 nb;
   1.341 +
   1.342 +    if (mon->owner) {
   1.343 +	nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld",
   1.344 +			 mon, mon->owner->id, mon->owner, mon->entryCount);
   1.345 +    } else {
   1.346 +	nb = PR_snprintf(buf, buflen, "[%p]", mon);
   1.347 +    }
   1.348 +    return nb;
   1.349 +}

mercurial