From patchwork Sat Jan 27 02:05:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 866677 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zSzfJ1hnvz9s75 for ; Sat, 27 Jan 2018 13:06:30 +1100 (AEDT) Received: from localhost ([::1]:43643 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1efFsw-0000tL-6h for incoming@patchwork.ozlabs.org; Fri, 26 Jan 2018 21:06:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37170) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1efFs1-0000jP-TP for qemu-devel@nongnu.org; Fri, 26 Jan 2018 21:05:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1efFs0-0008Li-Ma for qemu-devel@nongnu.org; Fri, 26 Jan 2018 21:05:29 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48060) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1efFrx-0008Il-TJ; Fri, 26 Jan 2018 21:05:26 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2FC3315561; Sat, 27 Jan 2018 02:05:25 +0000 (UTC) Received: from probe.bos.redhat.com (dhcp-17-231.bos.redhat.com [10.18.17.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id 750745457C; Sat, 27 Jan 2018 02:05:24 +0000 (UTC) From: John Snow To: qemu-block@nongnu.org Date: Fri, 26 Jan 2018 21:05:04 -0500 Message-Id: <20180127020515.27137-4-jsnow@redhat.com> In-Reply-To: <20180127020515.27137-1-jsnow@redhat.com> References: <20180127020515.27137-1-jsnow@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Sat, 27 Jan 2018 02:05:25 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v3 03/14] blockjobs: add state transition table X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, John Snow , pkrempa@redhat.com, jtc@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" The state transition table has mostly been implied. We're about to make it a bit more complex, so let's make the STM explicit instead. Perform state transitions with a function that for now just asserts the transition is appropriate. undefined: May only transition to 'created'. created: May only transition to 'running'. It cannot transition to pause directly, but if a created job is requested to pause prior to starting, it will transition synchronously to 'running' and then to 'paused'. running: May transition either to 'paused' or 'ready'. paused: May transition to either 'running' or 'ready', but only in terms of returning to that prior state. p->r->y is not possible, but this is not encapsulated by the STM table. ready: May transition to paused. Signed-off-by: John Snow --- blockjob.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/blockjob.c b/blockjob.c index 6eb783a354..d084a1e318 100644 --- a/blockjob.c +++ b/blockjob.c @@ -42,6 +42,35 @@ * block_job_enter. */ static QemuMutex block_job_mutex; +/* BlockJob State Transition Table */ +bool BlockJobSTT[BLOCK_JOB_STATUS__MAX][BLOCK_JOB_STATUS__MAX] = { + /* U, C, R, P, Y */ + /* U: */ [BLOCK_JOB_STATUS_UNDEFINED] = {0, 1, 0, 0, 0}, + /* C: */ [BLOCK_JOB_STATUS_CREATED] = {0, 0, 1, 0, 0}, + /* R: */ [BLOCK_JOB_STATUS_RUNNING] = {0, 0, 0, 1, 1}, + /* P: */ [BLOCK_JOB_STATUS_PAUSED] = {0, 0, 1, 0, 1}, + /* Y: */ [BLOCK_JOB_STATUS_READY] = {0, 0, 0, 1, 0}, +}; + +static void block_job_state_transition(BlockJob *job, BlockJobStatus s1) +{ + BlockJobStatus s0 = job->status; + if (s0 == s1) { + return; + } + assert(s1 >= 0 && s1 <= BLOCK_JOB_STATUS__MAX); + assert(BlockJobSTT[s0][s1]); + switch (s1) { + case BLOCK_JOB_STATUS_WAITING: + case BLOCK_JOB_STATUS_PENDING: + case BLOCK_JOB_STATUS_CONCLUDED: + assert(job->manual); + default: + break; + } + job->status = s1; +} + static void block_job_lock(void) { qemu_mutex_lock(&block_job_mutex); @@ -321,7 +350,7 @@ void block_job_start(BlockJob *job) job->pause_count--; job->busy = true; job->paused = false; - job->status = BLOCK_JOB_STATUS_RUNNING; + block_job_state_transition(job, BLOCK_JOB_STATUS_RUNNING); bdrv_coroutine_enter(blk_bs(job->blk), job->co); } @@ -709,7 +738,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, job->pause_count = 1; job->refcnt = 1; job->manual = manual; - job->status = BLOCK_JOB_STATUS_CREATED; + block_job_state_transition(job, BLOCK_JOB_STATUS_CREATED); aio_timer_init(qemu_get_aio_context(), &job->sleep_timer, QEMU_CLOCK_REALTIME, SCALE_NS, block_job_sleep_timer_cb, job); @@ -815,11 +844,11 @@ void coroutine_fn block_job_pause_point(BlockJob *job) if (block_job_should_pause(job) && !block_job_is_cancelled(job)) { BlockJobStatus status = job->status; - job->status = BLOCK_JOB_STATUS_PAUSED; + block_job_state_transition(job, BLOCK_JOB_STATUS_PAUSED); job->paused = true; block_job_do_yield(job, -1); job->paused = false; - job->status = status; + block_job_state_transition(job, status); } if (job->driver->resume) { @@ -925,7 +954,7 @@ void block_job_iostatus_reset(BlockJob *job) void block_job_event_ready(BlockJob *job) { - job->status = BLOCK_JOB_STATUS_READY; + block_job_state_transition(job, BLOCK_JOB_STATUS_READY); job->ready = true; if (block_job_is_internal(job)) {