Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dest.c: Use monotonic time for cups_enum_dest #1084

Open
wants to merge 2 commits into
base: 2.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cups/Dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ auth.o: auth.c cups-private.h string-private.h ../config.h \
pwg.h http-private.h ../cups/language.h ../cups/http.h \
language-private.h ../cups/transcode.h pwg-private.h thread-private.h \
debug-internal.h debug-private.h
clock.o: clock.c cups-private.h string-private.h ../config.h \
../cups/versioning.h debug-internal.h debug-private.h ipp-private.h \
../cups/cups.h ../cups/file.h ../cups/ipp.h ../cups/http.h \
../cups/array.h ../cups/language.h ../cups/pwg.h http-private.h \
language-private.h ../cups/transcode.h pwg-private.h thread-private.h versioning.h \
cups.h
debug.o: debug.c cups-private.h string-private.h ../config.h \
../cups/versioning.h array-private.h ../cups/array.h versioning.h \
ipp-private.h ../cups/cups.h file.h ipp.h http.h array.h language.h \
Expand Down Expand Up @@ -337,6 +343,8 @@ testcache.o: testcache.c ppd-private.h ../cups/cups.h file.h versioning.h \
testclient.o: testclient.c ../config.h ../cups/cups.h file.h versioning.h \
ipp.h http.h array.h language.h pwg.h ../cups/raster.h cups.h \
../cups/string-private.h ../cups/versioning.h ../cups/thread-private.h
testclock.o: testclock.c cups.h file.h versioning.h ipp.h http.h array.h \
language.h pwg.h
testconflicts.o: testconflicts.c cups.h file.h versioning.h ipp.h http.h \
array.h language.h pwg.h ppd.h raster.h string-private.h ../config.h \
../cups/versioning.h
Expand Down
15 changes: 15 additions & 0 deletions cups/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ include ../Makedefs
COREOBJS = \
array.o \
auth.o \
clock.o \
debug.o \
dest.o \
dest-job.o \
Expand Down Expand Up @@ -92,6 +93,7 @@ TESTOBJS = \
testadmin.o \
testarray.o \
testcache.o \
testclock.o \
testclient.o \
testconflicts.o \
testcreds.o \
Expand Down Expand Up @@ -179,6 +181,7 @@ UNITTARGETS = \
testarray \
testcache \
testclient \
testclock \
testconflicts \
testcreds \
testcups \
Expand Down Expand Up @@ -528,6 +531,18 @@ testclient: testclient.o $(LIBCUPSSTATIC)
$(CODE_SIGN) -s "$(CODE_SIGN_IDENTITY)" $@


#
# testclock (dependency on static libraries is intentional)
#

testclock: testclock.o $(LIBCUPSSTATIC)
echo Linking $@...
$(LD_CC) $(ALL_LDFLAGS) -o $@ testclock.o $(LINKCUPSSTATIC)
$(CODE_SIGN) -s "$(CODE_SIGN_IDENTITY)" $@
echo Running clock API tests...
./testclock


#
# testconflicts (dependency on static CUPS library is intentional)
#
Expand Down
112 changes: 112 additions & 0 deletions cups/clock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Monotonic clock API for CUPS.
//
// Copyright © 2024 by OpenPrinting.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//

#include "cups-private.h"


//
// Local globals...
//

static int cups_clock_init = 0;// Clock initialized?
static _cups_mutex_t cups_clock_mutex = _CUPS_MUTEX_INITIALIZER;
// Mutex to control access
#ifdef _WIN32
static ULONGLONG cups_first_tick; // First tick count
#else
# ifdef CLOCK_MONOTONIC_RAW
static struct timespec cups_first_clock;// First clock value
# endif // CLOCK_MONOTONIC_RAW
static struct timeval cups_first_time; // First time value
#endif // _WIN32


//
// '_cupsGetClock()' - Get a monotonic clock value in seconds.
//
// This function returns a monotonically increasing clock value in seconds. The
// first call will always return 0.0. Subsequent calls will return the number
// of seconds that have elapsed since the first call, regardless of system time
// changes, sleep, etc. The sub-second accuracy varies based on the operating
// system and hardware but is typically 10ms or better.
//
// @since CUPS 2.5@
//

double // O - Elapsed seconds
_cupsGetClock(void)
{
double secs; // Elapsed seconds
#ifdef _WIN32
ULONGLONG curtick; // Current tick count
#else
# ifdef CLOCK_MONOTONIC_RAW
struct timespec curclock; // Current clock value
# endif // CLOCK_MONOTONIC_RAW
struct timeval curtime; // Current time value
#endif // _WIN32


_cupsMutexLock(&cups_clock_mutex);

#ifdef _WIN32
// Get the current tick count in milliseconds...
curtick = GetTickCount64();

if (!cups_clock_init)
{
// First time through initialize the initial tick count...
cups_clock_init = 1;
cups_first_tick = curtick;
}

// Convert ticks to seconds...
if (curtick < cups_first_tick)
secs = 0.0;
else
secs = 0.001 * (curtick - cups_first_tick);

#else
# ifdef CLOCK_MONOTONIC_RAW
// Get the current tick count in milliseconds...
if (!clock_gettime(CLOCK_MONOTONIC_RAW, &curclock))
{
if (!cups_clock_init)
{
// First time through initialize the initial clock value...
cups_clock_init = 1;
cups_first_clock = curclock;
}

// Convert clock value to seconds...
if ((secs = curclock.tv_sec - cups_first_clock.tv_sec + 0.000000001 * (curclock.tv_nsec - cups_first_clock.tv_nsec)) < 0.0)
secs = 0.0;
}
else
# endif // CLOCK_MONOTONIC_RAW
{
gettimeofday(&curtime, /*tzp*/NULL);

if (!cups_clock_init)
{
// First time through initialize the initial clock value...
cups_clock_init = 1;
cups_first_time = curtime;
}

// Convert time value to seconds...
if ((secs = curtime.tv_sec - cups_first_time.tv_sec + 0.000001 * (curtime.tv_usec - cups_first_time.tv_usec)) < 0.0)
secs = 0.0;
}
#endif // _WIN32

_cupsMutexUnlock(&cups_clock_mutex);

return (secs);
}
1 change: 1 addition & 0 deletions cups/cups-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ extern http_t *_cupsConnect(void) _CUPS_PRIVATE;
extern char *_cupsCreateDest(const char *name, const char *info, const char *device_id, const char *device_uri, char *uri, size_t urisize) _CUPS_PRIVATE;
extern ipp_attribute_t *_cupsEncodeOption(ipp_t *ipp, ipp_tag_t group_tag, _ipp_option_t *map, const char *name, const char *value) _CUPS_PRIVATE;
extern int _cupsGet1284Values(const char *device_id, cups_option_t **values) _CUPS_PRIVATE;
extern double _cupsGetClock() _CUPS_PRIVATE;
extern const char *_cupsGetDestResource(cups_dest_t *dest, unsigned flags, char *resource, size_t resourcesize) _CUPS_PRIVATE;
extern int _cupsGetDests(http_t *http, ipp_op_t op, const char *name, cups_dest_t **dests, cups_ptype_t type, cups_ptype_t mask) _CUPS_PRIVATE;
extern const char *_cupsGetPassword(const char *prompt) _CUPS_PRIVATE;
Expand Down
109 changes: 109 additions & 0 deletions cups/testclock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Monotonic clock test program for CUPS.
//
// Copyright © 2024 by OpenPrinting.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//

#include "cups-private.h"
#include <math.h>


//
// 'main()' - Main entry for clock tests.
//

int // O - Exit status
main(void)
{
double current; // Current time
int status = 0; // statusurn value


// Test clock values at 0, 1, 5, 10, and 30 seconds
fputs("_cupsGetClock(initial):", stdout);
current = _cupsGetClock();
if (current == 0.0)
printf("PASS: %g\n", current);
else
{
printf("FAIL: got %g, expected 0.0\n", current);
status ++;
}

sleep(1);

fputs("_cupsGetClock(1 second):", stdout);
current = _cupsGetClock();
if (fabs(current - 1.0) < 0.1)
printf("PASS: %g\n", current);
else
{
printf("FAIL: got %g, expected 1.0 +/- 0.1\n", current);
status ++;
}

sleep(4);

fputs("_cupsGetClock(5 second):", stdout);
current = _cupsGetClock();
if (fabs(current - 5.0) < 0.1)
printf("PASS: %g\n", current);
else
{
printf("FAIL: got %g, expected 5.0 +/- 0.1\n", current);
status ++;
}

sleep(5);

fputs("_cupsGetClock(10 second):", stdout);
current = _cupsGetClock();
if (fabs(current - 10.0) < 0.1)
printf("PASS: %g\n", current);
else
{
printf("FAIL: got %g, expected 10.0 +/- 0.1\n", current);
status ++;
}

sleep(20);

fputs("_cupsGetClock(30 second):", stdout);
current = _cupsGetClock();
if (fabs(current - 30.0) < 0.1)
printf("PASS: %g\n", current);
else
{
printf("FAIL: got %g, expected 30.0 +/- 0.1\n", current);
status ++;
}

sleep(30);

fputs("_cupsGetClock(60 second):", stdout);
current = _cupsGetClock();
if (fabs(current - 60.0) < 0.1)
printf("PASS: %g\n", current);
else
{
printf("FAIL: got %g, expected 60.0 +/- 0.1\n", current);
status ++;
}

sleep(60);

fputs("_cupsGetClock(120 second):", stdout);
current = _cupsGetClock();
if (fabs(current - 120.0) < 0.1)
printf("PASS: %g\n", current);
else
{
printf("FAIL: got %g, expected 120.0 +/- 0.1\n", current);
status ++;
}

return (status ? 1 : 0);
}
1 change: 1 addition & 0 deletions vcnet/libcups2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<ClCompile Include="..\cups\adminutil.c" />
<ClCompile Include="..\cups\array.c" />
<ClCompile Include="..\cups\auth.c" />
<ClCompile Include="..\cups\clock.c" />
<ClCompile Include="..\cups\debug.c" />
<ClCompile Include="..\cups\dest-job.c" />
<ClCompile Include="..\cups\dest-localization.c" />
Expand Down
Loading
Loading