~ubuntu-branches/ubuntu/vivid/qemu/vivid

« back to all changes in this revision

Viewing changes to util/rfifolock.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-02-25 22:31:43 UTC
  • mfrom: (1.8.5)
  • Revision ID: package-import@ubuntu.com-20140225223143-odhqxfc60wxrjl15
Tags: 2.0.0~rc1+dfsg-0ubuntu1
* Merge 2.0.0-rc1
* debian/rules: consolidate ppc filter entries.
* Move qemu-system-arch64 into qemu-system-arm
* debian/patches/define-trusty-machine-type.patch: define a trusty machine
  type, currently the same as pc-i440fx-2.0, to put is in a better position
  to enable live migrations from trusty onward.  (LP: #1294823)
* debian/control: build-dep on libfdt >= 1.4.0  (LP: #1295072)
* Merge latest upstream git to commit dc9528f
* Debian/rules:
  - remove -enable-uname-release=2.6.32
  - don't make the aarch64 target Ubuntu-specific.
* Remove patches which are now upstream:
  - fix-smb-security-share.patch
  - slirp-smb-redirect-port-445-too.patch 
  - linux-user-Implement-sendmmsg-syscall.patch (better version is upstream)
  - signal-added-a-wrapper-for-sigprocmask-function.patch
  - ubuntu/signal-sigsegv-protection-on-do_sigprocmask.patch
  - ubuntu/Don-t-block-SIGSEGV-at-more-places.patch
  - ubuntu/ppc-force-cpu-threads-count-to-be-power-of-2.patch
* add link for /usr/share/qemu/bios-256k.bin
* Remove all linaro patches.
* Remove all arm64/ patches.  Many but not all are upstream.
* Remove CVE-2013-4377.patch which is upstream.
* debian/control-in: don't make qemu-system-aarch64 ubuntu-specific

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Recursive FIFO lock
 
3
 *
 
4
 * Copyright Red Hat, Inc. 2013
 
5
 *
 
6
 * Authors:
 
7
 *  Stefan Hajnoczi   <stefanha@redhat.com>
 
8
 *
 
9
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
 
10
 * See the COPYING.LIB file in the top-level directory.
 
11
 *
 
12
 */
 
13
 
 
14
#include <assert.h>
 
15
#include "qemu/rfifolock.h"
 
16
 
 
17
void rfifolock_init(RFifoLock *r, void (*cb)(void *), void *opaque)
 
18
{
 
19
    qemu_mutex_init(&r->lock);
 
20
    r->head = 0;
 
21
    r->tail = 0;
 
22
    qemu_cond_init(&r->cond);
 
23
    r->nesting = 0;
 
24
    r->cb = cb;
 
25
    r->cb_opaque = opaque;
 
26
}
 
27
 
 
28
void rfifolock_destroy(RFifoLock *r)
 
29
{
 
30
    qemu_cond_destroy(&r->cond);
 
31
    qemu_mutex_destroy(&r->lock);
 
32
}
 
33
 
 
34
/*
 
35
 * Theory of operation:
 
36
 *
 
37
 * In order to ensure FIFO ordering, implement a ticketlock.  Threads acquiring
 
38
 * the lock enqueue themselves by incrementing the tail index.  When the lock
 
39
 * is unlocked, the head is incremented and waiting threads are notified.
 
40
 *
 
41
 * Recursive locking does not take a ticket since the head is only incremented
 
42
 * when the outermost recursive caller unlocks.
 
43
 */
 
44
void rfifolock_lock(RFifoLock *r)
 
45
{
 
46
    qemu_mutex_lock(&r->lock);
 
47
 
 
48
    /* Take a ticket */
 
49
    unsigned int ticket = r->tail++;
 
50
 
 
51
    if (r->nesting > 0 && qemu_thread_is_self(&r->owner_thread)) {
 
52
        r->tail--; /* put ticket back, we're nesting */
 
53
    } else {
 
54
        while (ticket != r->head) {
 
55
            /* Invoke optional contention callback */
 
56
            if (r->cb) {
 
57
                r->cb(r->cb_opaque);
 
58
            }
 
59
            qemu_cond_wait(&r->cond, &r->lock);
 
60
        }
 
61
    }
 
62
 
 
63
    qemu_thread_get_self(&r->owner_thread);
 
64
    r->nesting++;
 
65
    qemu_mutex_unlock(&r->lock);
 
66
}
 
67
 
 
68
void rfifolock_unlock(RFifoLock *r)
 
69
{
 
70
    qemu_mutex_lock(&r->lock);
 
71
    assert(r->nesting > 0);
 
72
    assert(qemu_thread_is_self(&r->owner_thread));
 
73
    if (--r->nesting == 0) {
 
74
        r->head++;
 
75
        qemu_cond_broadcast(&r->cond);
 
76
    }
 
77
    qemu_mutex_unlock(&r->lock);
 
78
}