Skip to content

Commit 392c715

Browse files
committed
Use cupsGetClock for cupsEnumDests (Issue #1084)
1 parent 005b460 commit 392c715

2 files changed

Lines changed: 28 additions & 36 deletions

File tree

cups/clock.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Monotonic clock API for CUPS.
33
//
4-
// Copyright © 2024 by OpenPrinting.
4+
// Copyright © 2024-2025 by OpenPrinting.
55
//
66
// Licensed under Apache License v2.0. See the file "LICENSE" for more
77
// information.
@@ -20,9 +20,9 @@ static cups_mutex_t cups_clock_mutex = CUPS_MUTEX_INITIALIZER;
2020
#ifdef _WIN32
2121
static ULONGLONG cups_first_tick; // First tick count
2222
#else
23-
# ifdef CLOCK_MONOTONIC
23+
# if defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW)
2424
static struct timespec cups_first_clock;// First clock value
25-
# endif // CLOCK_MONOTONIC
25+
# endif // CLOCK_MONOTONIC || CLOCK_MONOTONIC_RAW
2626
static struct timeval cups_first_time; // First time value
2727
#endif // _WIN32
2828

@@ -73,9 +73,13 @@ cupsGetClock(void)
7373
secs = 0.001 * (curtick - cups_first_tick);
7474

7575
#else
76-
# ifdef CLOCK_MONOTONIC
76+
# if defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW)
7777
// Get the current tick count in milliseconds...
78+
# ifdef CLOCK_MONOTONIC_RAW
79+
if (!clock_gettime(CLOCK_MONOTONIC_RAW, &curclock))
80+
# else
7881
if (!clock_gettime(CLOCK_MONOTONIC, &curclock))
82+
# endif // CLOCK_MONOTONIC_RAW
7983
{
8084
if (!cups_clock_init)
8185
{
@@ -89,7 +93,7 @@ cupsGetClock(void)
8993
secs = 0.0;
9094
}
9195
else
92-
# endif // CLOCK_MONOTONIC
96+
# endif // CLOCK_MONOTONIC || CLOCK_MONOTONIC_RAW
9397
{
9498
gettimeofday(&curtime, /*tzp*/NULL);
9599

cups/dest.c

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ typedef struct _cups_dnssd_device_s // Enumerated device
8787
typedef struct _cups_dnssd_resdata_s // Data for resolving URI
8888
{
8989
int *cancel; // Pointer to "cancel" variable
90-
struct timeval end_time; // Ending time
90+
double end_time; // Ending time
9191
} _cups_dnssd_resdata_t;
9292

9393
typedef struct _cups_getdata_s
@@ -132,7 +132,7 @@ static void cups_dest_query_cb(cups_dnssd_query_t *query, void *cb_data, cups_d
132132
static const char *cups_dest_resolve(cups_dest_t *dest, const char *uri, int msec, int *cancel, cups_dest_cb_t cb, void *user_data);
133133
static bool cups_dest_resolve_cb(void *context);
134134
static void cups_dnssd_unquote(char *dst, const char *src, size_t dstsize);
135-
static int cups_elapsed(struct timeval *t);
135+
static int cups_elapsed(double *t);
136136
static int cups_enum_dests(http_t *http, unsigned flags, int msec, int *cancel, cups_ptype_t type, cups_ptype_t mask, cups_dest_cb_t cb, void *user_data);
137137
static int cups_find_dest(const char *name, const char *instance,
138138
int num_dests, cups_dest_t *dests, int prev,
@@ -2963,23 +2963,13 @@ cups_dest_resolve(
29632963

29642964

29652965
// Resolve the URI...
2966-
resolve.cancel = cancel;
2967-
gettimeofday(&resolve.end_time, NULL);
2968-
if (msec > 0)
2969-
{
2970-
resolve.end_time.tv_sec += msec / 1000;
2971-
resolve.end_time.tv_usec += (msec % 1000) * 1000;
2966+
resolve.cancel = cancel;
2967+
resolve.end_time = cupsGetClock();
29722968

2973-
while (resolve.end_time.tv_usec >= 1000000)
2974-
{
2975-
resolve.end_time.tv_sec ++;
2976-
resolve.end_time.tv_usec -= 1000000;
2977-
}
2978-
}
2969+
if (msec > 0)
2970+
resolve.end_time += 0.001 * msec;
29792971
else
2980-
{
2981-
resolve.end_time.tv_sec += 75;
2982-
}
2972+
resolve.end_time += 75;
29832973

29842974
if (cb)
29852975
(*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, dest);
@@ -3010,7 +3000,7 @@ cups_dest_resolve_cb(void *context) // I - Resolve data
30103000
{
30113001
_cups_dnssd_resdata_t *resolve = (_cups_dnssd_resdata_t *)context;
30123002
// Resolve data
3013-
struct timeval curtime; // Current time
3003+
double curtime; // Current time
30143004

30153005

30163006
// If the cancel variable is set, return immediately.
@@ -3021,11 +3011,11 @@ cups_dest_resolve_cb(void *context) // I - Resolve data
30213011
}
30223012

30233013
// Otherwise check the end time...
3024-
gettimeofday(&curtime, NULL);
3014+
curtime = cupsGetClock();
30253015

3026-
DEBUG_printf("4cups_dest_resolve_cb: curtime=%d.%06d, end_time=%d.%06d", (int)curtime.tv_sec, (int)curtime.tv_usec, (int)resolve->end_time.tv_sec, (int)resolve->end_time.tv_usec);
3016+
DEBUG_printf("4cups_dest_resolve_cb: curtime=%.6f, end_time=%.6f", curtime, resolve->end_time);
30273017

3028-
return (curtime.tv_sec < resolve->end_time.tv_sec || (curtime.tv_sec == resolve->end_time.tv_sec && curtime.tv_usec < resolve->end_time.tv_usec));
3018+
return (curtime < resolve->end_time);
30293019
}
30303020

30313021

@@ -3071,17 +3061,15 @@ cups_dnssd_unquote(char *dst, // I - Destination buffer
30713061
//
30723062

30733063
static int // O - Elapsed time in milliseconds
3074-
cups_elapsed(struct timeval *t) // IO - Previous time
3064+
cups_elapsed(double *t) // IO - Previous time
30753065
{
3076-
int msecs; // Milliseconds
3077-
struct timeval nt; // New time
3078-
3079-
3080-
gettimeofday(&nt, NULL);
3066+
int msecs; // Milliseconds
3067+
double nt; // New time
30813068

3082-
msecs = (int)(1000 * (nt.tv_sec - t->tv_sec) + (nt.tv_usec - t->tv_usec) / 1000);
30833069

3084-
*t = nt;
3070+
nt = cupsGetClock();
3071+
msecs = (int)(1000.0 * (nt - *t));
3072+
*t = nt;
30853073

30863074
return (msecs);
30873075
}
@@ -3113,7 +3101,7 @@ cups_enum_dests(
31133101
int count, // Number of queries started
31143102
completed, // Number of completed queries
31153103
remaining; // Remainder of timeout
3116-
struct timeval curtime; // Current time
3104+
double curtime; // Current time
31173105
_cups_dnssd_data_t data; // Data for callback
31183106
_cups_dnssd_device_t *device; // Current device
31193107
cups_dnssd_t *dnssd = NULL; // DNS-SD context
@@ -3351,7 +3339,7 @@ cups_enum_dests(
33513339
else
33523340
remaining = msec;
33533341

3354-
gettimeofday(&curtime, NULL);
3342+
curtime = cupsGetClock();
33553343

33563344
while (remaining > 0 && (!cancel || !*cancel))
33573345
{

0 commit comments

Comments
 (0)