From patchwork Thu Mar 7 02:53:18 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 225711 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 62B542C037E for ; Thu, 7 Mar 2013 13:55:32 +1100 (EST) Received: from localhost ([::1]:37419 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDQzS-0004xR-Mu for incoming@patchwork.ozlabs.org; Wed, 06 Mar 2013 21:55:30 -0500 Received: from eggs.gnu.org ([208.118.235.92]:53123) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDQyl-0004Gi-Tg for qemu-devel@nongnu.org; Wed, 06 Mar 2013 21:54:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UDQyk-0007R6-Pr for qemu-devel@nongnu.org; Wed, 06 Mar 2013 21:54:47 -0500 Received: from mail-pb0-f51.google.com ([209.85.160.51]:60918) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDQyk-0007Qs-8j for qemu-devel@nongnu.org; Wed, 06 Mar 2013 21:54:46 -0500 Received: by mail-pb0-f51.google.com with SMTP id un15so6852096pbc.24 for ; Wed, 06 Mar 2013 18:54:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=e2MF7aP1B/kqM4aI/3JiMmtsLUZ3UFY3kYUADULVr+w=; b=C+UIy8NSRBSqDx5EpiLDn3nMD51GjQQFkqDLscK/FdsiAhCofb1zZVqitE40a0fgQT KljPOoQLegGhbPFXJBpy64nxrlvxRGiRZafU3XWmNrb4feu55xQ6RoNa1nSCcTrJG4fd hFOIA89i5PVnlVQReQD2RQDb8vwACdNYAQhQdSHzzNPmceedRbT1fT1EDrFR4mnGqe0S fZ+EGIjqxhp2gaZhSgaik852HSVFpw9bsiYz4TFcoCbYjVh9sd0gCvDWbM8anqJqqsRX 6vC37G8yG0X1AT5yWaSW38QAnfsYesn5/edu5T0CDi6NIpNQR34h1hsu5pnKutPN0KoM AKfw== X-Received: by 10.68.254.35 with SMTP id af3mr46677877pbd.176.1362624885154; Wed, 06 Mar 2013 18:54:45 -0800 (PST) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPS id wc4sm334508pac.16.2013.03.06.18.54.35 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Wed, 06 Mar 2013 18:54:43 -0800 (PST) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Thu, 7 Mar 2013 10:53:18 +0800 Message-Id: <1362624800-10682-4-git-send-email-qemulist@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1362624800-10682-1-git-send-email-qemulist@gmail.com> References: <1362624800-10682-1-git-send-email-qemulist@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.160.51 Cc: Stefan Hajnoczi , "Michael S. Tsirkin" , mdroth , Anthony Liguori , Paolo Bonzini Subject: [Qemu-devel] [PATCH v2 3/5] net: introduce lock to protect NetQueue X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Liu Ping Fan Signed-off-by: Liu Ping Fan --- net/queue.c | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-) diff --git a/net/queue.c b/net/queue.c index 67959f8..f7ff020 100644 --- a/net/queue.c +++ b/net/queue.c @@ -53,6 +53,7 @@ struct NetQueue { uint32_t nq_maxlen; uint32_t nq_count; + QemuMutex lock; QTAILQ_HEAD(packets, NetPacket) packets; unsigned delivering : 1; @@ -68,6 +69,7 @@ NetQueue *qemu_new_net_queue(void *opaque) queue->nq_maxlen = 10000; queue->nq_count = 0; + qemu_mutex_init(&queue->lock); QTAILQ_INIT(&queue->packets); queue->delivering = 0; @@ -107,7 +109,9 @@ void qemu_net_queue_append(NetQueue *queue, memcpy(packet->data, buf, size); queue->nq_count++; + qemu_mutex_lock(&queue->lock); QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); + qemu_mutex_unlock(&queue->lock); } void qemu_net_queue_append_iov(NetQueue *queue, @@ -142,7 +146,9 @@ void qemu_net_queue_append_iov(NetQueue *queue, } queue->nq_count++; + qemu_mutex_lock(&queue->lock); QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); + qemu_mutex_unlock(&queue->lock); } static ssize_t qemu_net_queue_deliver(NetQueue *queue, @@ -229,6 +235,7 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from) { NetPacket *packet, *next; + qemu_mutex_lock(&queue->lock); QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { if (packet->sender == from) { QTAILQ_REMOVE(&queue->packets, packet, entry); @@ -236,10 +243,12 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from) g_free(packet); } } + qemu_mutex_unlock(&queue->lock); } bool qemu_net_queue_flush(NetQueue *queue) { + qemu_mutex_lock(&queue->lock); while (!QTAILQ_EMPTY(&queue->packets)) { NetPacket *packet; int ret; @@ -256,6 +265,7 @@ bool qemu_net_queue_flush(NetQueue *queue) if (ret == 0) { queue->nq_count++; QTAILQ_INSERT_HEAD(&queue->packets, packet, entry); + qemu_mutex_unlock(&queue->lock); return false; } @@ -265,5 +275,6 @@ bool qemu_net_queue_flush(NetQueue *queue) g_free(packet); } + qemu_mutex_unlock(&queue->lock); return true; }