From patchwork Thu Jan 28 10:15:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 8148331 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B5F23BEEE5 for ; Thu, 28 Jan 2016 10:15:42 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F3B5120251 for ; Thu, 28 Jan 2016 10:15:41 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3F04220328 for ; Thu, 28 Jan 2016 10:15:41 +0000 (UTC) Received: from localhost ([::1]:55053 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aOjc4-0007ci-FS for patchwork-qemu-devel@patchwork.kernel.org; Thu, 28 Jan 2016 05:15:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42543) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aOjbs-0007b9-Di for qemu-devel@nongnu.org; Thu, 28 Jan 2016 05:15:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aOjbr-0001Xr-Bu for qemu-devel@nongnu.org; Thu, 28 Jan 2016 05:15:28 -0500 Received: from mail-wm0-x22c.google.com ([2a00:1450:400c:c09::22c]:35927) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aOjbr-0001XK-0p for qemu-devel@nongnu.org; Thu, 28 Jan 2016 05:15:27 -0500 Received: by mail-wm0-x22c.google.com with SMTP id p63so17397228wmp.1 for ; Thu, 28 Jan 2016 02:15:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=n3nbxgj1vi5zwdzbPgi35O6u13wHdvGdMEUCypNpHDI=; b=UhTr0g54GmzedqdFCf8vzOzDqVYnCWtZr9JSxo6hDbnvs7keWIG7nK+LIgbmNIy1NR UIc+VK6ENlsFIXwSCd/CCR6kR58g27Y4EJW5lVtnqRENW+pLTaZSoupOKkPK00PGO+Dz B0ANGv6Jrz1LG9X8ZgbpTkKXLMnRZRO+FiUfY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=n3nbxgj1vi5zwdzbPgi35O6u13wHdvGdMEUCypNpHDI=; b=IApnwjdZkeGiJiFhZgy+mDgZe3gi5Z/cTJOnUqC+GZ0GI4TU2sQ7yUjM1XrKvQ8Ajj +TVobSgbw/w2r02uQEd87/h7KecCEEK6JRzDt1krvFszj4xUsmublk50k9qNjjnlecut roD0NaTSSlEngQEHWOrfnc34OaT9zei8M3hYK7azrelbxUFx+V9lx0HHciMFQ2QteSLx 9+X8vczlQv55PAHW4P1Xt4hxrHK3TEiAdjdMvumJd/YRmOBtGmdDb4gRNZkwBrB5yzBY ekxVZFbeuFijyH90YijrKvWmkmhg6cRvQ0gQZ0ZWAZScNWZiPHcj42D3TWV3DzFyPKN9 HrHg== X-Gm-Message-State: AG10YOSdg/V2T10rZMSTGu8mBYhiCIOQ0Tb6IZuD8sZMhxNFPcPFe86u84MYgiPOZvcsLfyT X-Received: by 10.194.21.163 with SMTP id w3mr2181159wje.58.1453976126455; Thu, 28 Jan 2016 02:15:26 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id q75sm2135717wmd.6.2016.01.28.02.15.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Jan 2016 02:15:25 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id D05263E0807; Thu, 28 Jan 2016 10:15:22 +0000 (GMT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Thu, 28 Jan 2016 10:15:19 +0000 Message-Id: <1453976119-24372-6-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1453976119-24372-1-git-send-email-alex.bennee@linaro.org> References: <1453976119-24372-1-git-send-email-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22c Cc: mttcg@listserver.greensocs.com, peter.maydell@linaro.org, mark.burton@greensocs.com, a.rigo@virtualopensystems.com, stefanha@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , fred.konrad@greensocs.com Subject: [Qemu-devel] [PATCH v1 5/5] thread-pool: atomic fixes from tsan 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Mark changes in thread pool state as explicitly atomic. Also in the test-thread-pool code make accesses to data.n explicitly atomic. Signed-off-by: Alex Bennée Acked-by: Paolo Bonzini --- tests/test-thread-pool.c | 8 ++++---- thread-pool.c | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test-thread-pool.c b/tests/test-thread-pool.c index ccdee39..f51e284 100644 --- a/tests/test-thread-pool.c +++ b/tests/test-thread-pool.c @@ -46,10 +46,10 @@ static void test_submit(void) { WorkerTestData data = { .n = 0 }; thread_pool_submit(pool, worker_cb, &data); - while (data.n == 0) { + while (atomic_read(&data.n) == 0) { aio_poll(ctx, true); } - g_assert_cmpint(data.n, ==, 1); + g_assert_cmpint(atomic_read(&data.n), ==, 1); } static void test_submit_aio(void) @@ -128,7 +128,7 @@ static void test_submit_many(void) aio_poll(ctx, true); } for (i = 0; i < 100; i++) { - g_assert_cmpint(data[i].n, ==, 1); + g_assert_cmpint(atomic_read(&data[i].n), ==, 1); g_assert_cmpint(data[i].ret, ==, 0); } } @@ -183,7 +183,7 @@ static void do_test_cancel(bool sync) g_assert_cmpint(num_canceled, <, 100); for (i = 0; i < 100; i++) { - if (data[i].aiocb && data[i].n != 3) { + if (data[i].aiocb && (atomic_read(&data[i].n) != 3)) { if (sync) { /* Canceling the others will be a blocking operation. */ bdrv_aio_cancel(data[i].aiocb); diff --git a/thread-pool.c b/thread-pool.c index 402c778..431a6fb 100644 --- a/thread-pool.c +++ b/thread-pool.c @@ -99,15 +99,15 @@ static void *worker_thread(void *opaque) req = QTAILQ_FIRST(&pool->request_list); QTAILQ_REMOVE(&pool->request_list, req, reqs); - req->state = THREAD_ACTIVE; + atomic_set(&req->state, THREAD_ACTIVE); qemu_mutex_unlock(&pool->lock); ret = req->func(req->arg); - req->ret = ret; + atomic_set(&req->ret, ret); /* Write ret before state. */ smp_wmb(); - req->state = THREAD_DONE; + atomic_set(&req->state, THREAD_DONE); qemu_mutex_lock(&pool->lock); @@ -167,7 +167,7 @@ static void thread_pool_completion_bh(void *opaque) restart: QLIST_FOREACH_SAFE(elem, &pool->head, all, next) { - if (elem->state != THREAD_DONE) { + if (atomic_read(&elem->state) != THREAD_DONE) { continue; } @@ -184,7 +184,7 @@ restart: */ qemu_bh_schedule(pool->completion_bh); - elem->common.cb(elem->common.opaque, elem->ret); + elem->common.cb(elem->common.opaque, atomic_read(&elem->ret)); qemu_aio_unref(elem); goto restart; } else { @@ -201,7 +201,7 @@ static void thread_pool_cancel(BlockAIOCB *acb) trace_thread_pool_cancel(elem, elem->common.opaque); qemu_mutex_lock(&pool->lock); - if (elem->state == THREAD_QUEUED && + if (atomic_read(&elem->state) == THREAD_QUEUED && /* No thread has yet started working on elem. we can try to "steal" * the item from the worker if we can get a signal from the * semaphore. Because this is non-blocking, we can do it with