qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Fam Zheng <fam@euphon.net>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	Wen Congyang <wencongyang2@huawei.com>,
	Xie Changlong <xiechanglong.d@gmail.com>,
	Emanuele Giuseppe Esposito <eesposit@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	qemu-devel@nongnu.org, Hanna Reitz <hreitz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, John Snow <jsnow@redhat.com>
Subject: [RFC PATCH 07/15] job-driver.h: add helper functions
Date: Fri, 29 Oct 2021 12:39:06 -0400	[thread overview]
Message-ID: <20211029163914.4044794-8-eesposit@redhat.com> (raw)
In-Reply-To: <20211029163914.4044794-1-eesposit@redhat.com>

These functions will be useful when job_lock is globally applied,
as they will allow drivers to access the job struct fields
without worrying about the job lock.

Now that we are done with the job API header split, update also
the comments in blockjob.c (and move them in job.c).

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 include/qemu/job-driver.h |  21 +++++++
 blockjob.c                |  20 -------
 job.c                     | 116 +++++++++++++++++++++++++++++++++++++-
 3 files changed, 135 insertions(+), 22 deletions(-)

diff --git a/include/qemu/job-driver.h b/include/qemu/job-driver.h
index 1efd196da8..19ae5ce8f0 100644
--- a/include/qemu/job-driver.h
+++ b/include/qemu/job-driver.h
@@ -149,4 +149,25 @@ void job_early_fail(Job *job);
 /** Moves the @job from RUNNING to READY */
 void job_transition_to_ready(Job *job);
 
+/** Enters the @job if it is not paused */
+void job_enter_not_paused(Job *job);
+
+/** returns @job->ret */
+bool job_has_failed(Job *job);
+
+/** Returns the @job->status */
+JobStatus job_get_status(Job *job);
+
+/** Returns the @job->pause_count */
+int job_get_pause_count(Job *job);
+
+/** Returns @job->paused */
+bool job_get_paused(Job *job);
+
+/** Returns @job->busy */
+bool job_get_busy(Job *job);
+
+/** Return true if @job not paused and not cancelled */
+bool job_not_paused_nor_cancelled(Job *job);
+
 #endif /* JOB_DRIVER_H */
diff --git a/blockjob.c b/blockjob.c
index 4982f6a2b5..53c1e9c406 100644
--- a/blockjob.c
+++ b/blockjob.c
@@ -36,21 +36,6 @@
 #include "qemu/main-loop.h"
 #include "qemu/timer.h"
 
-/*
- * The block job API is composed of two categories of functions.
- *
- * The first includes functions used by the monitor.  The monitor is
- * peculiar in that it accesses the block job list with block_job_get, and
- * therefore needs consistency across block_job_get and the actual operation
- * (e.g. block_job_set_speed).  The consistency is achieved with
- * aio_context_acquire/release.  These functions are declared in blockjob.h.
- *
- * The second includes functions used by the block job drivers and sometimes
- * by the core block layer.  These do not care about locking, because the
- * whole coroutine runs under the AioContext lock, and are declared in
- * blockjob_int.h.
- */
-
 static bool is_block_job(Job *job)
 {
     return job_type(job) == JOB_TYPE_BACKUP ||
@@ -433,11 +418,6 @@ static void block_job_event_ready(Notifier *n, void *opaque)
 }
 
 
-/*
- * API for block job drivers and the block layer.  These functions are
- * declared in blockjob_int.h.
- */
-
 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
                        JobTxn *txn, BlockDriverState *bs, uint64_t perm,
                        uint64_t shared_perm, int64_t speed, int flags,
diff --git a/job.c b/job.c
index b66d59b746..db7ad79745 100644
--- a/job.c
+++ b/job.c
@@ -32,6 +32,23 @@
 #include "trace/trace-root.h"
 #include "qapi/qapi-events-job.h"
 
+/*
+ * The job API is composed of two categories of functions.
+ *
+ * The first includes functions used by the monitor.  The monitor is
+ * peculiar in that it accesses the block job list with job_get, and
+ * therefore needs consistency across job_get and the actual operation
+ * (e.g. job_user_cancel). To achieve this consistency, the caller
+ * calls job_lock/job_unlock itself around the whole operation.
+ * These functions are declared in job-monitor.h.
+ *
+ *
+ * The second includes functions used by the block job drivers and sometimes
+ * by the core block layer. These delegate the locking to the callee instead,
+ * and are declared in job-driver.h.
+ */
+
+
 /* job_mutex protects the jobs list, but also makes the job API thread-safe. */
 static QemuMutex job_mutex;
 
@@ -213,18 +230,94 @@ const char *job_type_str(const Job *job)
     return JobType_str(job_type(job));
 }
 
-bool job_is_cancelled(Job *job)
+JobStatus job_get_status(Job *job)
+{
+    JobStatus status;
+    job_lock();
+    status = job->status;
+    job_unlock();
+    return status;
+}
+
+int job_get_pause_count(Job *job)
+{
+    int ret;
+    job_lock();
+    ret = job->pause_count;
+    job_unlock();
+    return ret;
+}
+
+bool job_get_paused(Job *job)
+{
+    bool ret;
+    job_lock();
+    ret = job->paused;
+    job_unlock();
+    return ret;
+}
+
+bool job_get_busy(Job *job)
+{
+    bool ret;
+    job_lock();
+    ret = job->busy;
+    job_unlock();
+    return ret;
+}
+
+bool job_has_failed(Job *job)
+{
+    bool ret;
+    job_lock();
+    ret = job->ret < 0;
+    job_unlock();
+    return ret;
+}
+
+/* Called with job_mutex held. */
+static bool job_is_cancelled_locked(Job *job)
 {
     /* force_cancel may be true only if cancelled is true, too */
     assert(job->cancelled || !job->force_cancel);
     return job->force_cancel;
 }
 
-bool job_cancel_requested(Job *job)
+/* Called with job_mutex *not* held. */
+bool job_is_cancelled(Job *job)
+{
+    bool ret;
+    job_lock();
+    ret = job_is_cancelled_locked(job);
+    job_unlock();
+    return ret;
+}
+
+bool job_not_paused_nor_cancelled(Job *job)
+{
+    bool ret;
+    job_lock();
+    ret = !job->paused && !job_is_cancelled_locked(job);
+    job_unlock();
+    return ret;
+}
+
+/* Called with job_mutex held. */
+static bool job_cancel_requested_locked(Job *job)
 {
     return job->cancelled;
 }
 
+/* Called with job_mutex *not* held. */
+bool job_cancel_requested(Job *job)
+{
+    bool ret;
+    job_lock();
+    ret = job_cancel_requested_locked(job);
+    job_unlock();
+    return ret;
+}
+
 /* Called with job_mutex held. */
 bool job_is_ready_locked(Job *job)
 {
@@ -280,6 +373,16 @@ bool job_is_completed(Job *job)
     return false;
 }
 
+/* Called with job_mutex lock *not* held */
+static bool job_is_completed_unlocked(Job *job)
+{
+    bool res;
+    job_lock();
+    res = job_is_completed(job);
+    job_unlock();
+    return res;
+}
+
 static bool job_started(Job *job)
 {
     return job->co;
@@ -579,6 +682,15 @@ void job_pause(Job *job)
     }
 }
 
+void job_enter_not_paused(Job *job)
+{
+    job_lock();
+    if (!job->paused) {
+        job_enter_cond(job, NULL);
+    }
+    job_unlock();
+}
+
 void job_resume(Job *job)
 {
     assert(job->pause_count > 0);
-- 
2.27.0



  parent reply	other threads:[~2021-10-29 16:53 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-29 16:38 [RFC PATCH 00/15] job: replace AioContext lock with job_mutex Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 01/15] jobs: add job-common.h Emanuele Giuseppe Esposito
2021-11-02 10:07   ` Stefan Hajnoczi
2021-10-29 16:39 ` [RFC PATCH 02/15] job.c: make job_lock/unlock public Emanuele Giuseppe Esposito
2021-11-02 10:10   ` Stefan Hajnoczi
2021-10-29 16:39 ` [RFC PATCH 03/15] job-common.h: categorize fields in struct Job Emanuele Giuseppe Esposito
2021-11-02 10:19   ` Stefan Hajnoczi
2021-10-29 16:39 ` [RFC PATCH 04/15] jobs: add job-monitor.h Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 05/15] job-monitor.h: define the job monitor API Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 06/15] jobs: add job-driver.h Emanuele Giuseppe Esposito
2021-10-29 16:39 ` Emanuele Giuseppe Esposito [this message]
2021-11-02 10:54   ` [RFC PATCH 07/15] job-driver.h: add helper functions Vladimir Sementsov-Ogievskiy
2021-10-29 16:39 ` [RFC PATCH 08/15] job.c: minor adjustments in preparation to job-driver Emanuele Giuseppe Esposito
2021-11-02 10:51   ` Vladimir Sementsov-Ogievskiy
2021-10-29 16:39 ` [RFC PATCH 09/15] job.c: move inner aiocontext lock in callbacks Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 10/15] aio-wait.h: introduce AIO_WAIT_WHILE_UNLOCKED Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 11/15] jobs: remove aiocontext locks since the functions are under BQL Emanuele Giuseppe Esposito
2021-11-02 12:41   ` Vladimir Sementsov-Ogievskiy
2021-11-03 15:56     ` Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 12/15] jobs: protect jobs with job_lock/unlock Emanuele Giuseppe Esposito
2021-11-02 12:53   ` Vladimir Sementsov-Ogievskiy
2021-10-29 16:39 ` [RFC PATCH 13/15] jobs: use job locks and helpers also in the unit tests Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 14/15] jobs: add missing job locks to replace aiocontext lock Emanuele Giuseppe Esposito
2021-10-29 16:39 ` [RFC PATCH 15/15] jobs: remove all unnecessary AioContext locks Emanuele Giuseppe Esposito
2021-11-02 10:06 ` [RFC PATCH 00/15] job: replace AioContext lock with job_mutex Stefan Hajnoczi
2021-11-02 13:08 ` Vladimir Sementsov-Ogievskiy
2021-11-02 14:13   ` Emanuele Giuseppe Esposito
2021-11-02 14:58     ` Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211029163914.4044794-8-eesposit@redhat.com \
    --to=eesposit@redhat.com \
    --cc=armbru@redhat.com \
    --cc=fam@euphon.net \
    --cc=hreitz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    --cc=wencongyang2@huawei.com \
    --cc=xiechanglong.d@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).