+2017-12-15 Keith Marshall <keith@users.osdn.me>
+
+ Implement a subset of the POSIX.1b-1993 clock API.
+
+ * include/time.h [_POSIX_C_SOURCE >= 199309L]
+ (clockid_t): New structured data type; define it opaquely, and...
+ (CLOCK_REALTIME, CLOCK_MONOTONIC): ...declare these extern instances.
+ [_POSIX_C_SOURCE >= 200809L] (gettimeofday): Mark it "deprecated".
+ [_POSIX_C_SOURCE >= 199309L] (clock_getres, clock_gettime)
+ (clock_settime): New functions; declare prototypes.
+
+ * mingwex/clockapi.h mingwex/clockapi.c mingwex/clockres.c
+ * mingwex/clockset.c mingwex/clocktime.c: New files; they implement
+ the preceding clock instances, and associated API functions.
+
2017-12-14 Keith Marshall <keith@users.osdn.me>
Add support for POSIX.1 "obsolescence" warnings.
#
libmingwex.a: $(addsuffix .$(OBJEXT), mingw-aligned-malloc mingw-fseek)
libmingwex.a: $(addsuffix .$(OBJEXT), glob getopt basename dirname nsleep)
+libmingwex.a: $(addsuffix .$(OBJEXT), clockapi clockres clockset clocktime)
libmingwex.a: $(addsuffix .$(OBJEXT), mkstemp mkdtemp cryptnam setenv)
libmingwex.a: $(addsuffix .$(OBJEXT), tdelete tfind tsearch twalk)
* designated as IEEE Std 1003.1, 2016 Edition; the struct timezone pointer
* argument of some Unix implementations (declared as a pointer to void in
* POSIX.1) is ignored, as are all error conditions.
- *
- * FIXME: Deprecate this for _POSIX_C_SOURCE >= 200809L
*/
-int __cdecl __MINGW_NOTHROW gettimeofday
+int __cdecl __MINGW_NOTHROW __POSIX_2008_DEPRECATED gettimeofday
(struct timeval *__restrict__, void *__restrict__ /* tzp (unused) */);
_END_C_DECLS
? (unsigned)(-1) : (unsigned)(period->tv_nsec));
}
#endif /* !__NO_INLINE__ */
+
+#if _POSIX_C_SOURCE >= 199309L
+/* POSIX.1b-1993 introduced the optional POSIX clocks API; it
+ * was subsequently moved to "base", as of POSIX.1-2008, to the
+ * extent required to support the CLOCK_REALTIME feature, with
+ * the remainder of its features remaining optional. We choose
+ * to provide a subset, supporting CLOCK_MONOTONIC in addition
+ * to the aforementioned CLOCK_REALTIME feature.
+ *
+ * We define the POSIX clockid_t type as a pointer to an opaque
+ * structure; user code should never need to know details of the
+ * internal layout of this structure.
+ */
+typedef struct __clockid__ *clockid_t;
+
+/* The standard clockid_t entities which we choose to support.
+ */
+extern clockid_t CLOCK_REALTIME;
+extern clockid_t CLOCK_MONOTONIC;
+
+/* Prototypes for the standard POSIX functions which provide the
+ * API to these standard clockid_t entities.
+ */
+int clock_getres (clockid_t, struct timespec *);
+int clock_gettime (clockid_t, struct timespec *);
+int clock_settime (clockid_t, const struct timespec *);
+
+#endif /* _POSIX_C_SOURCE >= 199309L */
#endif /* _POSIX_C_SOURCE */
_END_C_DECLS
--- /dev/null
+/*
+ * clockapi.c
+ *
+ * Implementation of the common elements of the POSIX clock API.
+ *
+ * $Id$
+ *
+ * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
+ * Copyright (C) 2017, MinGW.org Project
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+#include "clockapi.h"
+
+static struct __clockid__ clock_api[] =
+{ { /* CLOCK_REALTIME pre-initialization implementation reference data.
+ */
+ CLOCK_TYPE_REALTIME, /* Fixed category index */
+ CLOCK_REALTIME_FREQUENCY, /* Always updated at this frequency */
+ 0LL, /* Resolution in ns; needs initialization */
+ UNIX_EPOCH_AS_FILETIME /* Fixed timebase reference */
+ },
+ { /* CLOCK_MONOTONIC pre-initialization implementation reference data.
+ */
+ CLOCK_TYPE_MONOTONIC, /* Fixed category index */
+ NANOSECONDS_PER_SECOND, /* Update frequency; needs initialization */
+ 0LL, /* Resolution in ns; needs initialization */
+ 0LL /* Fixed timebase reference */
+ }
+};
+
+/* Publicly visible references to the preceding (opaque) clock definitions.
+ */
+clockid_t CLOCK_REALTIME = &clock_api[CLOCK_TYPE_REALTIME];
+clockid_t CLOCK_MONOTONIC = &clock_api[CLOCK_TYPE_MONOTONIC];
+
+CLOCK_INLINE
+int64_t clock_api_getres_interval( clockid_t clock_api )
+{
+ /* Initialization helper for POSIX clock implementation; called ONLY
+ * by __clock_api_is_valid(), and triggered by an uninitialized value
+ * of zero (0LL) for the "resolution" field within the implementation
+ * reference data structure; returns the initialized value thereof.
+ */
+ if( clock_api->resolution > 0LL )
+ /* Already initialized: immediately return initialized value.
+ */
+ return clock_api->resolution;
+
+ else if( clock_api->resolution == 0LL )
+ /* Not yet initialized: perform type specific initialization.
+ */
+ switch( clock_api->type )
+ {
+ /* We must be prepared to retrieve clock frequencies from Windows
+ * APIs, which report either LARGE_INTEGER or FILETIME values, but
+ * we prefer to interpret them as scalar int64_t values.
+ */
+ union { int64_t value; LARGE_INTEGER qpc_value; FILETIME rtc_value; }
+ freq;
+
+ case CLOCK_TYPE_REALTIME:
+ /* Clocks in this category use the GetSystemTimeAsFileTime() API
+ * to read the system clock; although this is nominally updated at
+ * 100ns intervals, it is unrealistic to expect resolution at such
+ * a high frequency. Thus, we read it once, then again in a busy
+ * wait loop, until we detect a change, and compute the actual
+ * update interval between the two reported values.
+ */
+ GetSystemTimeAsFileTime( &freq.rtc_value );
+ clock_api->resolution = freq.value;
+ do { GetSystemTimeAsFileTime( &freq.rtc_value );
+ } while( freq.value == clock_api->resolution );
+ freq.value -= clock_api->resolution;
+
+ /* Ultimately, we initialize the effective resolution, converting
+ * from this indicated count of 100ns intervals, to nanoseconds.
+ */
+ return clock_api->resolution = freq.value * 100LL;
+
+ case CLOCK_TYPE_MONOTONIC:
+ /* Clocks in this category use the QueryPerformanceCounter() API
+ * to count arbitrarily scaled time slices, relative to an equally
+ * arbitrary timebase; we must use the QueryPerformanceFrequency()
+ * call to verify availability of this API, and to establish its
+ * update frequency and resolution in nanoseconds.
+ */
+ if( QueryPerformanceFrequency( &freq.qpc_value ) && (freq.value > 0LL) )
+ return clock_api->resolution = NANOSECONDS_PER_SECOND
+ / (clock_api->frequency = freq.value);
+ }
+
+ /* If we get to here, initialization of the specified clock failed; set
+ * its "resolution" to -1LL, thus marking it as unavailable.
+ */
+ return clock_api->resolution = ((uint64_t)(clock_api_invalid_error()));
+}
+
+int __clock_api_is_valid( clockid_t clock_id )
+{
+ /* Helper function, called by any of clock_getres(), clock_gettime(),
+ * or clock_settime(), to check availability of the specified clock,
+ * initializing it if necessary, returning...
+ */
+ if( (clock_id != NULL)
+ && ((unsigned)(clock_id->type) < CLOCK_TYPE_UNIMPLEMENTED)
+ && (clock_api_getres_interval( clock_id ) > 0LL) )
+ /*
+ * ...a nominally TRUE value of 1, in the case of a valid clock...
+ */
+ return 1;
+
+ /* ...or FALSE (zero), and setting "errno", otherwise.
+ */
+ errno = EINVAL;
+ return 0;
+}
+
+/* $RCSfile$: end of file */
--- /dev/null
+/*
+ * clockapi.h
+ *
+ * Private header, declaring the common elements of the POSIX clock API.
+ *
+ * $Id$
+ *
+ * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
+ * Copyright (C) 2017, MinGW.org Project
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+#include <time.h>
+#include <stdint.h>
+#include <winbase.h>
+#include <errno.h>
+
+typedef enum
+{ /* The categories of clock, for which we provide support.
+ */
+ CLOCK_TYPE_REALTIME, /* CLOCK_REALTIME and derivatives */
+ CLOCK_TYPE_MONOTONIC, /* CLOCK_MONOTONIC and derivatives */
+
+ /* Only the above represent valid clock categories; we end the
+ * enumeration with this comparator reference value, to which,
+ * when compared as unsigned, all unsupported category values
+ * will be equal or greater.
+ */
+ CLOCK_TYPE_UNIMPLEMENTED
+} CLOCK_TYPE;
+
+/* POSIX specifies that CLOCK_REALTIME shall report time elapsed, in
+ * seconds and nanoseconds, since the start of the Unix epoch, (i.e.
+ * since 1970-01-01 00:00:00), but we count it, in 100ns increments,
+ * since the start of the Windows FILETIME epoch, (1601-01-01 00:00).
+ * The following manifest constants facilitate the conversion from
+ * Windows FILETIME to Unix epoch; (11644473600LL seconds of the
+ * Windows FILETIME epoch have elapsed, when the Unix epoch begins).
+ */
+#define NANOSECONDS_PER_SECOND 1000000000LL
+#define CLOCK_REALTIME_FREQUENCY (NANOSECONDS_PER_SECOND / 100LL)
+#define UNIX_EPOCH_AS_FILETIME (11644473600LL * CLOCK_REALTIME_FREQUENCY)
+
+typedef struct __clockid__
+{ /* Formal definition of the clockid_t structure; (opaque to
+ * user application code).
+ */
+ CLOCK_TYPE type;
+ int64_t frequency;
+ int64_t resolution;
+ int64_t timebase;
+} *clockid_t;
+
+/* Prototype for clockid_t validation function; considered private
+ * within the scope of the implementation, (so not declared publicly),
+ * this also provides initialization support.
+ */
+extern int __clock_api_is_valid( clockid_t );
+
+#define CLOCK_INLINE static __inline__ __attribute__((__always_inline__))
+
+CLOCK_INLINE
+int clock_api_invalid_error( void )
+{ /* Inline helper function, to facilitate return from any of the
+ * standard clock API functions, while setting both "errno", and
+ * the return status, to report an invalid operation.
+ */
+ errno = EINVAL; return -1;
+}
+
+/* $RCSfile$: end of file */
--- /dev/null
+/*
+ * clockres.c
+ *
+ * Implementation of the clock_getres() element of the POSIX clock API.
+ *
+ * $Id$
+ *
+ * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
+ * Copyright (C) 2017, MinGW.org Project
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+#include "clockapi.h"
+
+int clock_getres( clockid_t clock_id, struct timespec *counter )
+{
+ /* Standard API function, first implemented in POSIX.1b-1993, to
+ * query the resolution of a specified clock; it first initializes
+ * the clock, if necessary, then returns the resolution value, as
+ * recorded within the associated implementation data structure,
+ * together with a "validity check" status code.
+ */
+ if( __clock_api_is_valid( clock_id ) )
+ {
+ /* The clock is valid; its resolution must be broken down into
+ * separate seconds and nanoseconds components, but only if the
+ * caller provided a struct timespec to receive them.
+ */
+ if( counter != NULL )
+ { counter->tv_nsec = clock_id->resolution % NANOSECONDS_PER_SECOND;
+ counter->tv_sec = clock_id->resolution / NANOSECONDS_PER_SECOND;
+ }
+ /* In any case, we return zero to indicate a valid clock...
+ */
+ return 0;
+ }
+ /* ...or -1 otherwise.
+ */
+ return -1;
+}
+
+/* $RCSfile$: end of file */
--- /dev/null
+/*
+ * clockset.c
+ *
+ * Implementation of the clock_settime() element of the POSIX clock API.
+ *
+ * $Id$
+ *
+ * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
+ * Copyright (C) 2017, MinGW.org Project
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+#include "clockapi.h"
+
+int clock_settime( clockid_t clock_id, const struct timespec *timeval )
+{
+ /* Standard API function, first implemented in POSIX.1b-1993, to
+ * set (or reset) the time value to be indicated by the specified
+ * clockid_t entity, if permissable.
+ *
+ * FIXME: Decide which clockid_t entities should support any such
+ * adjustment; for the time being, deny all such requests.
+ */
+ errno = ((timeval != NULL) && __clock_api_is_valid( clock_id ))
+ ? EPERM : EINVAL;
+ return -1;
+}
+
+/* $RCSfile$: end of file */
--- /dev/null
+/*
+ * clocktime.c
+ *
+ * Implementation of the clock_gettime() element of the POSIX clock API.
+ *
+ * $Id$
+ *
+ * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
+ * Copyright (C) 2017, MinGW.org Project
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+#include "clockapi.h"
+
+int clock_gettime( clockid_t clock_id, struct timespec *current )
+{
+ /* Standard API function, first implemented in POSIX.1b-1993, to
+ * read the current time indicated by a specified clock; it first
+ * initializes the clock, if necessary, then reads its currently
+ * indicated time, and encodes it for return, in the specified
+ * "current" timespec structure, together with a "validity check"
+ * status code.
+ *
+ * Note that, unlike the clock_getres() API, it is NOT permitted
+ * to specify NULL for the timespec parameter here; doing so will
+ * cause the call to fail, irrespective of the clock validity.
+ */
+ if( current == NULL )
+ return clock_api_invalid_error();
+
+ if( __clock_api_is_valid( clock_id ) )
+ {
+ /* We must be prepared to retrieve clock frequencies from Windows
+ * APIs, which report either LARGE_INTEGER or FILETIME values, but
+ * we prefer to interpret them as scalar int64_t values.
+ */
+ union { uint64_t value; LARGE_INTEGER qpc_value; FILETIME rtc_value; } ct;
+ switch( clock_id->type )
+ {
+ case CLOCK_TYPE_REALTIME:
+ /* The counter for CLOCK_REALTIME, and any derived clock, is
+ * represented by system time, expressed as a count of 100ns
+ * intervals since the start of the Windows FILETIME epoch.
+ */
+ GetSystemTimeAsFileTime( &ct.rtc_value );
+ break;
+
+ case CLOCK_TYPE_MONOTONIC:
+ /* Conversely, the counter for CLOCK_MONOTIME and derivatives
+ * is obtained from the Windows QPC API, if supported...
+ */
+ if( QueryPerformanceCounter( &ct.qpc_value ) == 0 )
+ /*
+ * ...or forces an "invalid status" return, otherwise.
+ */
+ return clock_api_invalid_error();
+ }
+ /* In either case, once we have a valid count of clock ticks, we
+ * must adjust it, relative to the timebase for the clock, (which
+ * is recorded within the clock's implementation data structure),
+ * then scale it, and break it down into seconds and nanoseconds
+ * components, (again based on scaling factors which are similarly
+ * recorded within the implementation data)...
+ */
+ ct.value -= clock_id->timebase;
+ current->tv_nsec = (ct.value % clock_id->frequency)
+ * NANOSECONDS_PER_SECOND / clock_id->frequency;
+ current->tv_sec = ct.value / clock_id->frequency;
+
+ /* ...before returning zero, as "successful completion" status...
+ */
+ return 0;
+ }
+ /* ...or -1, indicating failure.
+ */
+ return -1;
+}
+
+/* $RCSfile$: end of file */