qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: "Longpeng(Mike)" <longpeng2@huawei.com>,
	berrange@redhat.com, mst@redhat.com
Cc: arei.gonglei@huawei.com, qemu-devel@nongnu.org
Subject: Re: [RFC 2/2] sem-posix: use monotonic clock instead
Date: Mon, 21 Feb 2022 12:42:26 +0100	[thread overview]
Message-ID: <5e92a220-eb61-c510-0cdc-bf418b9025bc@redhat.com> (raw)
In-Reply-To: <20220221095617.1974-3-longpeng2@huawei.com>

On 2/21/22 10:56, Longpeng(Mike) via wrote:
> +    long now_nsec;
> +#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
> +    struct timespec now;
> +    clock_gettime(CLOCK_MONOTONIC, &now);
> +    now_sec = now.tv_sec;
> +    now_nsec = now.tv_nsec;
> +#else
>       struct timeval tv;
>       gettimeofday(&tv, NULL);
> -    ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
> -    ts->tv_sec = tv.tv_sec + ms / 1000;
> +    now_sec = tv.tv_sec;
> +    now_nsec = tv.tv_usec * 1000;
> +#endif
> +

Perhaps this might minimize the amount of conditional code, too:

diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index 903fa33965..4743d7b714 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -40,10 +40,14 @@ static void error_exit(int err, const char *msg)
  
  static void compute_abs_deadline(struct timespec *ts, int ms)
  {
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
-    ts->tv_sec = tv.tv_sec + ms / 1000;
+#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
+    clock_gettime(CLOCK_MONOTONIC, ts);
+#else
+    clock_gettime(CLOCK_REALTIME, ts);
+#endif
+
+    ts->tv_nsec += (ms % 1000) * 1000000;
+    ts->tv_sec += ms / 1000;
      if (ts->tv_nsec >= 1000000000) {
          ts->tv_sec++;
          ts->tv_nsec -= 1000000000;


Finally, the conditional variables initialization qemu_cond_init must
also use pthread_condattr_setclock.

Paolo


  reply	other threads:[~2022-02-21 11:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21  9:56 [RFC 0/2] qemu-sem-posix: use monotonic clock instead Longpeng(Mike) via
2022-02-21  9:56 ` [RFC 1/2] sem-posix: remove the posix semaphore support Longpeng(Mike) via
2022-02-21 11:11   ` Daniel P. Berrangé
2022-02-21 14:35     ` longpeng2--- via
2022-02-23  9:42     ` Paolo Bonzini
2022-02-21  9:56 ` [RFC 2/2] sem-posix: use monotonic clock instead Longpeng(Mike) via
2022-02-21 11:42   ` Paolo Bonzini [this message]
2022-02-21 14:39     ` longpeng2--- via
2022-02-21 11:31 ` [RFC 0/2] qemu-sem-posix: " Paolo Bonzini
2022-02-21 14:37   ` longpeng2--- via

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=5e92a220-eb61-c510-0cdc-bf418b9025bc@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=arei.gonglei@huawei.com \
    --cc=berrange@redhat.com \
    --cc=longpeng2@huawei.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).