emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob bb4d0b191d72f69978177242f9c01dda25244f93 2726 bytes (raw)
name: lib/gettime-res.c 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 
/* Get the system clock resolution.

   Copyright 2021-2022 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Paul Eggert.  */

#include <config.h>

#include "timespec.h"

static long int _GL_ATTRIBUTE_CONST
gcd (long int a, long int b)
{
  while (b != 0)
    {
      long int r = a % b;
      a = b;
      b = r;
    }
  return a;
}

/* Return the system time resolution in nanoseconds.  */

long int
gettime_res (void)
{
  struct timespec res;
#if defined CLOCK_REALTIME && HAVE_CLOCK_GETRES
  clock_getres (CLOCK_REALTIME, &res);
#elif defined HAVE_TIMESPEC_GETRES
  timespec_getres (&res, TIME_UTC);
#else
  /* Guess high and let the later code deduce better.  */
  res.tv_sec = 1;
  res.tv_nsec = 0;
#endif

  /* On all Gnulib platforms the following calculations do not overflow.  */

  long int hz = TIMESPEC_HZ;
  long int r = hz * res.tv_sec + res.tv_nsec;
  struct timespec earlier;
  earlier.tv_nsec = -1;

  /* On some platforms, clock_getres (CLOCK_REALTIME, ...) yields a
     too-large resolution, under the mistaken theory that it should
     return the timer interval.  For example, on AIX 7.2 POWER8
     clock_getres yields 10 ms even though clock_gettime yields 1 µs
     resolution.  Work around the problem with high probability by
     trying clock_gettime several times and observing the resulting
     bounds on resolution.  */
  int nsamples = 32;
  for (int i = 0; 1 < r && i < nsamples; i++)
    {
      /* If successive timestamps disagree the clock resolution must
         be small, so exit the inner loop to check this sample.
         Otherwise, arrange for the outer loop to exit but continue
         the inner-loop search for a differing timestamp sample.  */
      struct timespec now;
      for (;; i = nsamples)
        {
          now = current_timespec ();
          if (earlier.tv_nsec != now.tv_nsec || earlier.tv_sec != now.tv_sec)
            break;
        }
      earlier = now;

      r = gcd (r, now.tv_nsec ? now.tv_nsec : hz);
    }

  return r;
}

/*
 * Hey Emacs!
 * Local Variables:
 * coding: utf-8
 * End:
 */

debug log:

solving bb4d0b191d ...
found bb4d0b191d in https://list.orgmode.org/orgmode/9e4781b2-2ffa-b1ce-09b4-ead82cad9038@cs.ucla.edu/
found 611f83ad27 in https://list.orgmode.org/orgmode/f9d9d26c-1974-b52a-8f4d-49167676c102@cs.ucla.edu/

applying [1/2] https://list.orgmode.org/orgmode/f9d9d26c-1974-b52a-8f4d-49167676c102@cs.ucla.edu/
diff --git a/lib/gettime-res.c b/lib/gettime-res.c
new file mode 100644
index 0000000000..611f83ad27


applying [2/2] https://list.orgmode.org/orgmode/9e4781b2-2ffa-b1ce-09b4-ead82cad9038@cs.ucla.edu/
diff --git a/lib/gettime-res.c b/lib/gettime-res.c
index 611f83ad27..bb4d0b191d 100644

Checking patch lib/gettime-res.c...
Applied patch lib/gettime-res.c cleanly.
Checking patch lib/gettime-res.c...
Applied patch lib/gettime-res.c cleanly.

index at:
100644 bb4d0b191d72f69978177242f9c01dda25244f93	lib/gettime-res.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).