2000-02-17 20:38:33 +01:00
|
|
|
/* times.cc
|
|
|
|
|
2005-02-02 10:46:52 +01:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
2010-01-12 11:14:59 +01:00
|
|
|
2005, 2006, 2007, 2008, 2009, 2010 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2007-07-04 12:43:29 +02:00
|
|
|
#define __timezonefunc__
|
2009-01-03 06:12:22 +01:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <sys/times.h>
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#include <stdlib.h>
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2005-02-20 12:44:32 +01:00
|
|
|
#include "dtable.h"
|
2005-10-19 18:50:43 +02:00
|
|
|
#include "cygheap.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "pinfo.h"
|
2003-12-23 17:26:31 +01:00
|
|
|
#include "cygtls.h"
|
2007-08-14 16:48:52 +02:00
|
|
|
#include "ntdll.h"
|
2000-03-19 05:14:07 +01:00
|
|
|
|
2000-08-31 00:37:24 +02:00
|
|
|
#define FACTOR (0x19db1ded53e8000LL)
|
2000-02-17 20:38:33 +01:00
|
|
|
#define NSPERSEC 10000000LL
|
|
|
|
|
2010-06-12 18:34:26 +02:00
|
|
|
/* TODO: Putting this variable in the shared cygwin region partially solves
|
|
|
|
the problem of cygwin processes not recognizing date changes when other
|
|
|
|
cygwin processes set the date. There is still an additional problem of
|
|
|
|
long-running cygwin processes becoming confused when a non-cygwin process
|
|
|
|
sets the date. Unfortunately, it looks like a minor redesign is required
|
|
|
|
to handle that case. */
|
|
|
|
hires_ms gtod __attribute__((section (".cygwin_dll_common"), shared));
|
2010-05-26 16:48:17 +02:00
|
|
|
|
2010-08-09 18:47:48 +02:00
|
|
|
hires_ns NO_COPY ntod;
|
|
|
|
|
2005-12-07 23:28:49 +01:00
|
|
|
static inline LONGLONG
|
2009-10-13 04:26:33 +02:00
|
|
|
systime_ns ()
|
2005-12-07 23:28:49 +01:00
|
|
|
{
|
|
|
|
LARGE_INTEGER x;
|
|
|
|
FILETIME ft;
|
|
|
|
GetSystemTimeAsFileTime (&ft);
|
|
|
|
x.HighPart = ft.dwHighDateTime;
|
|
|
|
x.LowPart = ft.dwLowDateTime;
|
|
|
|
x.QuadPart -= FACTOR; /* Add conversion factor for UNIX vs. Windows base time */
|
|
|
|
return x.QuadPart;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Cygwin internal */
|
|
|
|
static unsigned long long __stdcall
|
2005-12-07 23:28:49 +01:00
|
|
|
__to_clock_t (FILETIME *src, int flag)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
unsigned long long total = ((unsigned long long) src->dwHighDateTime << 32) + ((unsigned)src->dwLowDateTime);
|
|
|
|
syscall_printf ("dwHighDateTime %u, dwLowDateTime %u", src->dwHighDateTime, src->dwLowDateTime);
|
|
|
|
|
|
|
|
/* Convert into clock ticks - the total is in 10ths of a usec. */
|
|
|
|
if (flag)
|
|
|
|
total -= FACTOR;
|
|
|
|
|
|
|
|
total /= (unsigned long long) (NSPERSEC / CLOCKS_PER_SEC);
|
2005-12-07 23:28:49 +01:00
|
|
|
syscall_printf ("total %08x %08x", (unsigned) (total>>32), (unsigned) (total));
|
2000-02-17 20:38:33 +01:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* times: POSIX 4.5.2.1 */
|
|
|
|
extern "C" clock_t
|
2002-10-21 03:00:58 +02:00
|
|
|
times (struct tms *buf)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
FILETIME creation_time, exit_time, kernel_time, user_time;
|
|
|
|
|
2005-07-03 04:40:30 +02:00
|
|
|
myfault efault;
|
|
|
|
if (efault.faulted (EFAULT))
|
2001-09-16 16:26:11 +02:00
|
|
|
return ((clock_t) -1);
|
|
|
|
|
2005-12-07 23:28:49 +01:00
|
|
|
LONGLONG ticks = gtod.uptime ();
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Ticks is in milliseconds, convert to our ticks. Use long long to prevent
|
|
|
|
overflow. */
|
2005-12-07 23:28:49 +01:00
|
|
|
clock_t tc = (clock_t) (ticks * CLOCKS_PER_SEC / 1000);
|
2007-02-22 17:22:38 +01:00
|
|
|
|
2009-12-18 21:32:04 +01:00
|
|
|
GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
|
2007-02-22 17:22:38 +01:00
|
|
|
&kernel_time, &user_time);
|
|
|
|
|
|
|
|
syscall_printf ("ticks %d, CLOCKS_PER_SEC %d", ticks, CLOCKS_PER_SEC);
|
|
|
|
syscall_printf ("user_time %d, kernel_time %d, creation_time %d, exit_time %d",
|
|
|
|
user_time, kernel_time, creation_time, exit_time);
|
|
|
|
buf->tms_stime = __to_clock_t (&kernel_time, 0);
|
|
|
|
buf->tms_utime = __to_clock_t (&user_time, 0);
|
|
|
|
timeval_to_filetime (&myself->rusage_children.ru_stime, &kernel_time);
|
|
|
|
buf->tms_cstime = __to_clock_t (&kernel_time, 1);
|
|
|
|
timeval_to_filetime (&myself->rusage_children.ru_utime, &user_time);
|
|
|
|
buf->tms_cutime = __to_clock_t (&user_time, 1);
|
|
|
|
|
|
|
|
return tc;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-09-10 10:30:51 +02:00
|
|
|
EXPORT_ALIAS (times, _times)
|
2002-10-21 03:00:58 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* settimeofday: BSD */
|
|
|
|
extern "C" int
|
2001-02-26 23:36:09 +01:00
|
|
|
settimeofday (const struct timeval *tv, const struct timezone *tz)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-02-26 23:36:09 +01:00
|
|
|
SYSTEMTIME st;
|
|
|
|
struct tm *ptm;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
tz = tz; /* silence warning about unused variable */
|
|
|
|
|
2002-09-22 05:38:57 +02:00
|
|
|
ptm = gmtime (&tv->tv_sec);
|
2001-09-07 23:32:07 +02:00
|
|
|
st.wYear = ptm->tm_year + 1900;
|
|
|
|
st.wMonth = ptm->tm_mon + 1;
|
2001-02-26 23:36:09 +01:00
|
|
|
st.wDayOfWeek = ptm->tm_wday;
|
2001-09-07 23:32:07 +02:00
|
|
|
st.wDay = ptm->tm_mday;
|
|
|
|
st.wHour = ptm->tm_hour;
|
2001-02-26 23:36:09 +01:00
|
|
|
st.wMinute = ptm->tm_min;
|
|
|
|
st.wSecond = ptm->tm_sec;
|
|
|
|
st.wMilliseconds = tv->tv_usec / 1000;
|
|
|
|
|
2002-09-22 05:38:57 +02:00
|
|
|
res = !SetSystemTime (&st);
|
2010-05-26 16:48:17 +02:00
|
|
|
gtod.reset ();
|
2001-02-26 23:36:09 +01:00
|
|
|
|
|
|
|
syscall_printf ("%d = settimeofday (%x, %x)", res, tv, tz);
|
|
|
|
|
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* timezone: standards? */
|
|
|
|
extern "C" char *
|
2005-11-18 16:57:16 +01:00
|
|
|
timezone (void)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-12-23 17:26:31 +01:00
|
|
|
char *b = _my_tls.locals.timezone_buf;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-09-22 05:38:57 +02:00
|
|
|
tzset ();
|
|
|
|
__small_sprintf (b,"GMT%+d:%02d", (int) (-_timezone / 3600), (int) (abs (_timezone / 60) % 60));
|
2000-02-17 20:38:33 +01:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
void __stdcall
|
|
|
|
totimeval (struct timeval *dst, FILETIME *src, int sub, int flag)
|
|
|
|
{
|
|
|
|
long long x = __to_clock_t (src, flag);
|
|
|
|
|
|
|
|
x *= (int) (1e6) / CLOCKS_PER_SEC; /* Turn x into usecs */
|
|
|
|
x -= (long long) sub * (int) (1e6);
|
|
|
|
|
|
|
|
dst->tv_usec = x % (long long) (1e6); /* And split */
|
|
|
|
dst->tv_sec = x / (long long) (1e6);
|
|
|
|
}
|
|
|
|
|
2002-02-15 08:05:05 +01:00
|
|
|
/* FIXME: Make thread safe */
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C" int
|
2007-07-06 22:09:23 +02:00
|
|
|
gettimeofday (struct timeval *tv, void *tzvp)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2007-07-06 22:09:23 +02:00
|
|
|
struct timezone *tz = (struct timezone *) tzvp;
|
2002-03-28 06:20:53 +01:00
|
|
|
static bool tzflag;
|
2005-11-11 17:42:15 +01:00
|
|
|
LONGLONG now = gtod.usecs ();
|
2005-11-03 03:41:51 +01:00
|
|
|
|
2002-02-15 08:05:05 +01:00
|
|
|
if (now == (LONGLONG) -1)
|
|
|
|
return -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-02-15 08:05:05 +01:00
|
|
|
tv->tv_sec = now / 1000000;
|
|
|
|
tv->tv_usec = now % 1000000;
|
2002-03-28 05:56:06 +01:00
|
|
|
|
|
|
|
if (tz != NULL)
|
|
|
|
{
|
2002-05-12 03:50:38 +02:00
|
|
|
if (!tzflag)
|
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
tzset ();
|
2002-05-12 03:50:38 +02:00
|
|
|
tzflag = true;
|
|
|
|
}
|
2002-03-28 05:56:06 +01:00
|
|
|
tz->tz_minuteswest = _timezone / 60;
|
|
|
|
tz->tz_dsttime = _daylight;
|
|
|
|
}
|
|
|
|
|
2002-02-15 08:05:05 +01:00
|
|
|
return 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2004-09-10 10:30:51 +02:00
|
|
|
EXPORT_ALIAS (gettimeofday, _gettimeofday)
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
void
|
|
|
|
time_t_to_filetime (time_t time_in, FILETIME *out)
|
|
|
|
{
|
|
|
|
long long x = time_in * NSPERSEC + FACTOR;
|
|
|
|
out->dwHighDateTime = x >> 32;
|
|
|
|
out->dwLowDateTime = x;
|
|
|
|
}
|
|
|
|
|
2008-04-24 11:59:54 +02:00
|
|
|
/* Cygwin internal */
|
|
|
|
void __stdcall
|
|
|
|
timespec_to_filetime (const struct timespec *time_in, FILETIME *out)
|
|
|
|
{
|
|
|
|
if (time_in->tv_nsec == UTIME_OMIT)
|
|
|
|
out->dwHighDateTime = out->dwLowDateTime = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long long x = time_in->tv_sec * NSPERSEC +
|
2009-10-13 04:26:33 +02:00
|
|
|
time_in->tv_nsec / (1000000000/NSPERSEC) + FACTOR;
|
2008-04-24 11:59:54 +02:00
|
|
|
out->dwHighDateTime = x >> 32;
|
|
|
|
out->dwLowDateTime = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Cygwin internal */
|
2005-02-20 12:44:32 +01:00
|
|
|
void __stdcall
|
2005-02-08 21:59:41 +01:00
|
|
|
timeval_to_filetime (const struct timeval *time_in, FILETIME *out)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
long long x = time_in->tv_sec * NSPERSEC +
|
|
|
|
time_in->tv_usec * (NSPERSEC/1000000) + FACTOR;
|
|
|
|
out->dwHighDateTime = x >> 32;
|
|
|
|
out->dwLowDateTime = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
static timeval __stdcall
|
|
|
|
time_t_to_timeval (time_t in)
|
|
|
|
{
|
|
|
|
timeval res;
|
|
|
|
res.tv_sec = in;
|
|
|
|
res.tv_usec = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-04-24 11:59:54 +02:00
|
|
|
/* Cygwin internal */
|
|
|
|
static const struct timespec *
|
|
|
|
timeval_to_timespec (const struct timeval *tvp, struct timespec *tmp)
|
|
|
|
{
|
|
|
|
if (!tvp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tmp[0].tv_sec = tvp[0].tv_sec;
|
|
|
|
tmp[0].tv_nsec = tvp[0].tv_usec * 1000;
|
|
|
|
if (tmp[0].tv_nsec < 0)
|
|
|
|
tmp[0].tv_nsec = 0;
|
|
|
|
else if (tmp[0].tv_nsec > 999999999)
|
|
|
|
tmp[0].tv_nsec = 999999999;
|
|
|
|
|
|
|
|
tmp[1].tv_sec = tvp[1].tv_sec;
|
|
|
|
tmp[1].tv_nsec = tvp[1].tv_usec * 1000;
|
|
|
|
if (tmp[1].tv_nsec < 0)
|
|
|
|
tmp[1].tv_nsec = 0;
|
|
|
|
else if (tmp[1].tv_nsec > 999999999)
|
|
|
|
tmp[1].tv_nsec = 999999999;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Cygwin internal */
|
|
|
|
/* Convert a Win32 time to "UNIX" format. */
|
|
|
|
long __stdcall
|
|
|
|
to_time_t (FILETIME *ptr)
|
|
|
|
{
|
|
|
|
/* A file time is the number of 100ns since jan 1 1601
|
|
|
|
stuffed into two long words.
|
|
|
|
A time_t is the number of seconds since jan 1 1970. */
|
|
|
|
|
|
|
|
long long x = ((long long) ptr->dwHighDateTime << 32) + ((unsigned)ptr->dwLowDateTime);
|
2000-10-20 23:45:50 +02:00
|
|
|
|
|
|
|
/* pass "no time" as epoch */
|
|
|
|
if (x == 0)
|
|
|
|
return 0;
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
x -= FACTOR; /* number of 100ns between 1601 and 1970 */
|
|
|
|
x /= (long long) NSPERSEC; /* number of 100ns in a second */
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2002-06-06 17:35:09 +02:00
|
|
|
/* Cygwin internal */
|
|
|
|
/* Convert a Win32 time to "UNIX" timestruc_t format. */
|
|
|
|
void __stdcall
|
|
|
|
to_timestruc_t (FILETIME *ptr, timestruc_t *out)
|
|
|
|
{
|
|
|
|
/* A file time is the number of 100ns since jan 1 1601
|
|
|
|
stuffed into two long words.
|
|
|
|
A timestruc_t is the number of seconds and microseconds since jan 1 1970
|
|
|
|
stuffed into a time_t and a long. */
|
|
|
|
|
|
|
|
long rem;
|
|
|
|
long long x = ((long long) ptr->dwHighDateTime << 32) + ((unsigned)ptr->dwLowDateTime);
|
|
|
|
|
|
|
|
/* pass "no time" as epoch */
|
|
|
|
if (x == 0)
|
|
|
|
{
|
|
|
|
out->tv_sec = 0;
|
|
|
|
out->tv_nsec = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x -= FACTOR; /* number of 100ns between 1601 and 1970 */
|
|
|
|
rem = x % ((long long)NSPERSEC);
|
|
|
|
x /= (long long) NSPERSEC; /* number of 100ns in a second */
|
|
|
|
out->tv_nsec = rem * 100; /* as tv_nsec is in nanoseconds */
|
|
|
|
out->tv_sec = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
/* Get the current time as a "UNIX" timestruc_t format. */
|
|
|
|
void __stdcall
|
|
|
|
time_as_timestruc_t (timestruc_t * out)
|
|
|
|
{
|
|
|
|
FILETIME filetime;
|
|
|
|
|
2005-03-02 09:28:54 +01:00
|
|
|
GetSystemTimeAsFileTime (&filetime);
|
2002-06-06 17:35:09 +02:00
|
|
|
to_timestruc_t (&filetime, out);
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* time: POSIX 4.5.1.1, C 4.12.2.4 */
|
|
|
|
/* Return number of seconds since 00:00 UTC on jan 1, 1970 */
|
2002-10-21 03:00:58 +02:00
|
|
|
extern "C" time_t
|
2000-02-17 20:38:33 +01:00
|
|
|
time (time_t * ptr)
|
|
|
|
{
|
|
|
|
time_t res;
|
|
|
|
FILETIME filetime;
|
|
|
|
|
2005-03-02 09:28:54 +01:00
|
|
|
GetSystemTimeAsFileTime (&filetime);
|
2000-02-17 20:38:33 +01:00
|
|
|
res = to_time_t (&filetime);
|
|
|
|
if (ptr)
|
|
|
|
*ptr = res;
|
|
|
|
|
|
|
|
syscall_printf ("%d = time (%x)", res, ptr);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* localtime_r.c
|
|
|
|
* Original Author: Adapted from tzcode maintained by Arthur David Olson.
|
|
|
|
*
|
|
|
|
* Converts the calendar time pointed to by tim_p into a broken-down time
|
|
|
|
* expressed as local time. Returns a pointer to a structure containing the
|
|
|
|
* broken-down time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SECSPERMIN 60
|
|
|
|
#define MINSPERHOUR 60
|
|
|
|
#define HOURSPERDAY 24
|
|
|
|
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
|
|
|
|
#define SECSPERDAY (SECSPERHOUR * HOURSPERDAY)
|
|
|
|
#define DAYSPERWEEK 7
|
|
|
|
#define MONSPERYEAR 12
|
|
|
|
|
|
|
|
#define YEAR_BASE 1900
|
|
|
|
#define EPOCH_YEAR 1970
|
|
|
|
#define EPOCH_WDAY 4
|
|
|
|
|
|
|
|
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
|
|
|
|
|
|
|
|
#if 0 /* POSIX_LOCALTIME */
|
|
|
|
|
|
|
|
static _CONST int mon_lengths[2][MONSPERYEAR] = {
|
|
|
|
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
|
|
|
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
|
|
|
};
|
|
|
|
|
|
|
|
static _CONST int year_lengths[2] = {
|
|
|
|
365,
|
|
|
|
366
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a time_t into a struct tm *.
|
|
|
|
* Does NO timezone conversion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
static struct tm * __stdcall
|
|
|
|
corelocaltime (const time_t * tim_p)
|
|
|
|
{
|
|
|
|
long days, rem;
|
|
|
|
int y;
|
|
|
|
int yleap;
|
|
|
|
_CONST int *ip;
|
2003-12-23 17:26:31 +01:00
|
|
|
struct tm &localtime_buf=_my_tls.locals.localtime_buf;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
time_t tim = *tim_p;
|
|
|
|
struct tm *res = &localtime_buf;
|
|
|
|
|
|
|
|
days = ((long) tim) / SECSPERDAY;
|
|
|
|
rem = ((long) tim) % SECSPERDAY;
|
|
|
|
|
|
|
|
while (rem < 0)
|
|
|
|
{
|
|
|
|
rem += SECSPERDAY;
|
|
|
|
--days;
|
|
|
|
}
|
|
|
|
while (rem >= SECSPERDAY)
|
|
|
|
{
|
|
|
|
rem -= SECSPERDAY;
|
|
|
|
++days;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute hour, min, and sec */
|
|
|
|
res->tm_hour = (int) (rem / SECSPERHOUR);
|
|
|
|
rem %= SECSPERHOUR;
|
|
|
|
res->tm_min = (int) (rem / SECSPERMIN);
|
|
|
|
res->tm_sec = (int) (rem % SECSPERMIN);
|
|
|
|
|
|
|
|
/* compute day of week */
|
|
|
|
if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
|
|
|
|
res->tm_wday += DAYSPERWEEK;
|
|
|
|
|
|
|
|
/* compute year & day of year */
|
|
|
|
y = EPOCH_YEAR;
|
|
|
|
if (days >= 0)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
yleap = isleap (y);
|
|
|
|
if (days < year_lengths[yleap])
|
|
|
|
break;
|
|
|
|
y++;
|
|
|
|
days -= year_lengths[yleap];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
--y;
|
|
|
|
yleap = isleap (y);
|
|
|
|
days += year_lengths[yleap];
|
|
|
|
} while (days < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
res->tm_year = y - YEAR_BASE;
|
|
|
|
res->tm_yday = days;
|
|
|
|
ip = mon_lengths[yleap];
|
|
|
|
for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
|
|
|
|
days -= ip[res->tm_mon];
|
|
|
|
res->tm_mday = days + 1;
|
|
|
|
|
|
|
|
/* set daylight saving time flag */
|
|
|
|
res->tm_isdst = -1;
|
|
|
|
|
|
|
|
syscall_printf ("%d = corelocaltime (%x)", res, tim_p);
|
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* localtime: POSIX 8.1.1, C 4.12.3.4 */
|
|
|
|
/*
|
|
|
|
* localtime takes a time_t (which is in UTC)
|
|
|
|
* and formats it into a struct tm as a local time.
|
|
|
|
*/
|
2002-10-21 03:00:58 +02:00
|
|
|
extern "C" struct tm *
|
2000-02-17 20:38:33 +01:00
|
|
|
localtime (const time_t *tim_p)
|
|
|
|
{
|
|
|
|
time_t tim = *tim_p;
|
|
|
|
struct tm *rtm;
|
|
|
|
|
2002-09-23 02:31:31 +02:00
|
|
|
tzset ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
tim -= _timezone;
|
|
|
|
|
|
|
|
rtm = corelocaltime (&tim);
|
|
|
|
|
|
|
|
rtm->tm_isdst = _daylight;
|
|
|
|
|
|
|
|
syscall_printf ("%x = localtime (%x)", rtm, tim_p);
|
|
|
|
|
|
|
|
return rtm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* gmtime: C 4.12.3.3 */
|
|
|
|
/*
|
|
|
|
* gmtime takes a time_t (which is already in UTC)
|
|
|
|
* and just puts it into a struct tm.
|
|
|
|
*/
|
2002-10-21 03:00:58 +02:00
|
|
|
extern "C" struct tm *
|
2000-02-17 20:38:33 +01:00
|
|
|
gmtime (const time_t *tim_p)
|
|
|
|
{
|
|
|
|
time_t tim = *tim_p;
|
|
|
|
|
|
|
|
struct tm *rtm = corelocaltime (&tim);
|
|
|
|
/* UTC has no daylight savings time */
|
|
|
|
rtm->tm_isdst = 0;
|
|
|
|
|
|
|
|
syscall_printf ("%x = gmtime (%x)", rtm, tim_p);
|
|
|
|
|
|
|
|
return rtm;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* POSIX_LOCALTIME */
|
|
|
|
|
2008-04-24 11:59:54 +02:00
|
|
|
int
|
|
|
|
utimens_worker (path_conv &win32, const struct timespec *tvp)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-02-20 12:44:32 +01:00
|
|
|
int res = -1;
|
2005-10-24 01:47:45 +02:00
|
|
|
|
|
|
|
if (win32.error)
|
|
|
|
set_errno (win32.error);
|
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-10-24 01:47:45 +02:00
|
|
|
fhandler_base *fh = NULL;
|
|
|
|
bool fromfd = false;
|
|
|
|
|
2005-10-24 17:09:07 +02:00
|
|
|
cygheap_fdenum cfd (true);
|
2005-10-24 01:47:45 +02:00
|
|
|
while (cfd.next () >= 0)
|
|
|
|
if (cfd->get_access () & (FILE_WRITE_ATTRIBUTES | GENERIC_WRITE)
|
2007-08-14 16:48:52 +02:00
|
|
|
&& RtlEqualUnicodeString (cfd->pc.get_nt_native_path (),
|
|
|
|
win32.get_nt_native_path (),
|
2008-07-16 22:20:45 +02:00
|
|
|
cfd->pc.objcaseinsensitive ()))
|
2005-10-24 01:47:45 +02:00
|
|
|
{
|
|
|
|
fh = cfd;
|
|
|
|
fromfd = true;
|
|
|
|
break;
|
|
|
|
}
|
2005-10-19 18:50:43 +02:00
|
|
|
|
2005-10-24 01:47:45 +02:00
|
|
|
if (!fh)
|
|
|
|
{
|
|
|
|
if (!(fh = build_fh_pc (win32)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (fh->error ())
|
|
|
|
{
|
2010-01-12 11:14:59 +01:00
|
|
|
debug_printf ("got %d error from build_fh_pc", fh->error ());
|
2005-10-24 01:47:45 +02:00
|
|
|
set_errno (fh->error ());
|
2007-08-14 16:48:52 +02:00
|
|
|
}
|
2005-10-24 01:47:45 +02:00
|
|
|
}
|
2005-10-19 18:50:43 +02:00
|
|
|
|
2008-04-24 11:59:54 +02:00
|
|
|
res = fh->utimens (tvp);
|
2005-10-24 01:47:45 +02:00
|
|
|
|
|
|
|
if (!fromfd)
|
|
|
|
delete fh;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-10-19 18:50:43 +02:00
|
|
|
error:
|
2007-08-14 16:48:52 +02:00
|
|
|
syscall_printf ("%d = utimes (%S, %p)",
|
|
|
|
res, win32.get_nt_native_path (), tvp);
|
2000-02-17 20:38:33 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-10-20 16:26:23 +02:00
|
|
|
/* utimes: POSIX/SUSv3 */
|
|
|
|
extern "C" int
|
|
|
|
utimes (const char *path, const struct timeval *tvp)
|
|
|
|
{
|
2007-08-14 16:48:52 +02:00
|
|
|
path_conv win32 (path, PC_POSIX | PC_SYM_FOLLOW, stat_suffixes);
|
2008-04-24 11:59:54 +02:00
|
|
|
struct timespec tmp[2];
|
|
|
|
return utimens_worker (win32, timeval_to_timespec (tvp, tmp));
|
2005-10-20 16:26:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BSD */
|
|
|
|
extern "C" int
|
|
|
|
lutimes (const char *path, const struct timeval *tvp)
|
|
|
|
{
|
2007-08-14 16:48:52 +02:00
|
|
|
path_conv win32 (path, PC_POSIX | PC_SYM_NOFOLLOW, stat_suffixes);
|
2008-04-24 11:59:54 +02:00
|
|
|
struct timespec tmp[2];
|
|
|
|
return utimens_worker (win32, timeval_to_timespec (tvp, tmp));
|
2005-10-20 16:26:23 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 11:59:54 +02:00
|
|
|
/* futimens: POSIX/SUSv4 */
|
2005-10-20 16:26:23 +02:00
|
|
|
extern "C" int
|
2008-04-24 11:59:54 +02:00
|
|
|
futimens (int fd, const struct timespec *tvp)
|
2005-10-20 16:26:23 +02:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd < 0)
|
|
|
|
res = -1;
|
2005-11-07 18:08:08 +01:00
|
|
|
else if (cfd->get_access () & (FILE_WRITE_ATTRIBUTES | GENERIC_WRITE))
|
2008-04-24 11:59:54 +02:00
|
|
|
res = cfd->utimens (tvp);
|
2005-11-07 18:08:08 +01:00
|
|
|
else
|
2008-04-24 11:59:54 +02:00
|
|
|
res = utimens_worker (cfd->pc, tvp);
|
|
|
|
syscall_printf ("%d = futimens (%d, %p)", res, fd, tvp);
|
2005-10-20 16:26:23 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-04-24 11:59:54 +02:00
|
|
|
/* BSD */
|
|
|
|
extern "C" int
|
|
|
|
futimes (int fd, const struct timeval *tvp)
|
|
|
|
{
|
|
|
|
struct timespec tmp[2];
|
|
|
|
return futimens (fd, timeval_to_timespec (tvp, tmp));
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* utime: POSIX 5.6.6.1 */
|
2002-10-21 03:00:58 +02:00
|
|
|
extern "C" int
|
2005-02-08 21:59:41 +01:00
|
|
|
utime (const char *path, const struct utimbuf *buf)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
struct timeval tmp[2];
|
|
|
|
|
|
|
|
if (buf == 0)
|
|
|
|
return utimes (path, 0);
|
|
|
|
|
|
|
|
debug_printf ("incoming utime act %x", buf->actime);
|
|
|
|
tmp[0] = time_t_to_timeval (buf->actime);
|
|
|
|
tmp[1] = time_t_to_timeval (buf->modtime);
|
|
|
|
|
|
|
|
return utimes (path, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ftime: standards? */
|
2002-10-21 03:00:58 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
ftime (struct timeb *tp)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
if (gettimeofday (&tv, &tz) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tp->time = tv.tv_sec;
|
|
|
|
tp->millitm = tv.tv_usec / 1000;
|
|
|
|
tp->timezone = tz.tz_minuteswest;
|
|
|
|
tp->dstflag = tz.tz_dsttime;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* obsolete, changed to cygwin_tzset when localtime.c was added - dj */
|
2002-02-28 20:06:42 +01:00
|
|
|
extern "C" void
|
2000-02-17 20:38:33 +01:00
|
|
|
cygwin_tzset ()
|
|
|
|
{
|
|
|
|
}
|
2002-02-15 08:05:05 +01:00
|
|
|
|
2005-11-04 03:01:45 +01:00
|
|
|
#define stupid_printf if (cygwin_finished_initializing) debug_printf
|
2002-02-15 08:05:05 +01:00
|
|
|
void
|
2010-08-09 18:47:48 +02:00
|
|
|
hires_ns::prime ()
|
2002-02-15 08:05:05 +01:00
|
|
|
{
|
|
|
|
LARGE_INTEGER ifreq;
|
|
|
|
if (!QueryPerformanceFrequency (&ifreq))
|
|
|
|
{
|
|
|
|
inited = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int priority = GetThreadPriority (GetCurrentThread ());
|
2005-11-03 03:41:51 +01:00
|
|
|
|
2002-02-15 08:05:05 +01:00
|
|
|
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL);
|
|
|
|
if (!QueryPerformanceCounter (&primed_pc))
|
|
|
|
{
|
2002-02-17 05:59:55 +01:00
|
|
|
SetThreadPriority (GetCurrentThread (), priority);
|
2002-02-15 08:05:05 +01:00
|
|
|
inited = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-09 18:47:48 +02:00
|
|
|
freq = (double) ((double) 1000000000. / (double) ifreq.QuadPart);
|
2005-12-07 23:28:49 +01:00
|
|
|
inited = true;
|
|
|
|
SetThreadPriority (GetCurrentThread (), priority);
|
2002-02-15 08:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LONGLONG
|
2010-08-09 18:47:48 +02:00
|
|
|
hires_ns::nsecs ()
|
2002-02-15 08:05:05 +01:00
|
|
|
{
|
|
|
|
if (!inited)
|
|
|
|
prime ();
|
|
|
|
if (inited < 0)
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
return (long long) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER now;
|
|
|
|
if (!QueryPerformanceCounter (&now))
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Use round() here?
|
2002-02-15 18:06:40 +01:00
|
|
|
now.QuadPart = (LONGLONG) (freq * (double) (now.QuadPart - primed_pc.QuadPart));
|
2010-08-09 18:47:48 +02:00
|
|
|
return now.QuadPart;
|
2002-02-15 08:05:05 +01:00
|
|
|
}
|
2002-06-07 05:44:33 +02:00
|
|
|
|
2011-03-29 12:21:30 +02:00
|
|
|
LONGLONG
|
|
|
|
hires_ms::timeGetTime_ns ()
|
|
|
|
{
|
|
|
|
LARGE_INTEGER t;
|
|
|
|
|
|
|
|
/* This is how timeGetTime is implemented in winmm.dll.
|
|
|
|
The real timeGetTime subtracts and adds some values which are constant
|
|
|
|
over the lifetime of the process. Since we don't need absolute accuracy
|
|
|
|
of the value returned by timeGetTime, only relative accuracy, we can skip
|
|
|
|
this step. However, if we ever find out that we need absolute accuracy,
|
|
|
|
here's how it works in it's full beauty:
|
|
|
|
|
|
|
|
- At process startup, winmm initializes two calibration values:
|
|
|
|
|
|
|
|
DWORD tick_count_start;
|
|
|
|
LARGE_INTEGER int_time_start;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
tick_count_start = GetTickCount ()
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int_time_start.HighPart = SharedUserData.InterruptTime.High1Time;
|
|
|
|
int_time_start.LowPart = SharedUserData.InterruptTime.LowPart;
|
|
|
|
}
|
|
|
|
while (int_time_start.HighPart
|
|
|
|
!= SharedUserData.InterruptTime.High2Time);
|
|
|
|
}
|
|
|
|
while (tick_count_start != GetTickCount ();
|
|
|
|
|
|
|
|
- timeGetTime computes its return value in the loop as below, and then:
|
|
|
|
|
|
|
|
t.QuadPart -= int_time_start.QuadPart;
|
|
|
|
t.QuadPart /= 10000;
|
|
|
|
t.LowPart += tick_count_start;
|
|
|
|
return t.LowPart;
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
t.HighPart = SharedUserData.InterruptTime.High1Time;
|
|
|
|
t.LowPart = SharedUserData.InterruptTime.LowPart;
|
|
|
|
}
|
|
|
|
while (t.HighPart != SharedUserData.InterruptTime.High2Time);
|
|
|
|
/* We use the value in full 100ns resolution in the calling functions
|
|
|
|
anyway, so we can skip dividing by 10000 here. */
|
|
|
|
return t.QuadPart;
|
|
|
|
}
|
|
|
|
|
2005-11-11 17:42:15 +01:00
|
|
|
void
|
2002-06-07 05:44:33 +02:00
|
|
|
hires_ms::prime ()
|
|
|
|
{
|
2003-09-07 07:18:01 +02:00
|
|
|
if (!inited)
|
|
|
|
{
|
2005-11-30 19:14:07 +01:00
|
|
|
int priority = GetThreadPriority (GetCurrentThread ());
|
|
|
|
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL);
|
2011-03-29 12:21:30 +02:00
|
|
|
initime_ns = systime_ns () - timeGetTime_ns ();
|
2005-12-07 23:28:49 +01:00
|
|
|
inited = true;
|
2005-11-30 19:14:07 +01:00
|
|
|
SetThreadPriority (GetCurrentThread (), priority);
|
2003-09-07 07:18:01 +02:00
|
|
|
}
|
2005-11-11 17:42:15 +01:00
|
|
|
return;
|
2002-06-07 05:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LONGLONG
|
2009-10-13 04:26:33 +02:00
|
|
|
hires_ms::nsecs ()
|
2002-06-07 05:44:33 +02:00
|
|
|
{
|
2005-11-11 17:42:15 +01:00
|
|
|
if (!inited)
|
2002-06-07 05:44:33 +02:00
|
|
|
prime ();
|
2005-11-04 18:38:32 +01:00
|
|
|
|
2009-10-13 04:26:33 +02:00
|
|
|
LONGLONG t = systime_ns ();
|
2011-03-29 12:21:30 +02:00
|
|
|
LONGLONG res = initime_ns + timeGetTime_ns ();
|
2009-10-13 04:26:33 +02:00
|
|
|
if (res < (t - 40 * 10000LL))
|
2004-06-30 17:50:08 +02:00
|
|
|
{
|
2005-12-07 23:28:49 +01:00
|
|
|
inited = false;
|
2004-06-30 17:50:08 +02:00
|
|
|
prime ();
|
2011-03-29 12:21:30 +02:00
|
|
|
res = initime_ns + timeGetTime_ns ();
|
2004-06-30 17:50:08 +02:00
|
|
|
}
|
2002-06-07 05:44:33 +02:00
|
|
|
return res;
|
|
|
|
}
|
2004-01-19 06:46:54 +01:00
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
clock_gettime (clockid_t clk_id, struct timespec *tp)
|
|
|
|
{
|
2010-08-09 18:47:48 +02:00
|
|
|
switch (clk_id)
|
2004-01-19 06:46:54 +01:00
|
|
|
{
|
2010-08-09 18:47:48 +02:00
|
|
|
case CLOCK_REALTIME:
|
|
|
|
{
|
|
|
|
LONGLONG now = gtod.nsecs ();
|
|
|
|
if (now == (LONGLONG) -1)
|
|
|
|
return -1;
|
|
|
|
tp->tv_sec = now / NSPERSEC;
|
|
|
|
tp->tv_nsec = (now % NSPERSEC) * (1000000000 / NSPERSEC);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CLOCK_MONOTONIC:
|
|
|
|
{
|
|
|
|
LONGLONG now = ntod.nsecs ();
|
|
|
|
if (now == (LONGLONG) -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tp->tv_sec = now / 1000000000;
|
|
|
|
tp->tv_nsec = (now % 1000000000);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
2004-01-19 06:46:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-11-11 17:42:15 +01:00
|
|
|
|
|
|
|
static DWORD minperiod; // FIXME: Maintain period after a fork.
|
|
|
|
|
2010-08-09 18:47:48 +02:00
|
|
|
LONGLONG
|
2011-03-29 12:21:30 +02:00
|
|
|
hires_ns::resolution ()
|
2010-08-09 18:47:48 +02:00
|
|
|
{
|
|
|
|
if (!inited)
|
|
|
|
prime ();
|
|
|
|
if (inited < 0)
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
return (long long) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (LONGLONG) freq;
|
|
|
|
}
|
|
|
|
|
2005-11-11 17:42:15 +01:00
|
|
|
UINT
|
|
|
|
hires_ms::resolution ()
|
|
|
|
{
|
|
|
|
if (!minperiod)
|
|
|
|
{
|
2011-03-29 12:21:30 +02:00
|
|
|
NTSTATUS status;
|
|
|
|
ULONG coarsest, finest, actual;
|
|
|
|
|
|
|
|
status = NtQueryTimerResolution (&coarsest, &finest, &actual);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
minperiod = (DWORD) actual / 10000L;
|
|
|
|
else
|
2005-11-11 17:42:15 +01:00
|
|
|
{
|
2011-03-29 12:21:30 +02:00
|
|
|
/* Try to empirically determine current timer resolution */
|
|
|
|
int priority = GetThreadPriority (GetCurrentThread ());
|
|
|
|
SetThreadPriority (GetCurrentThread (),
|
|
|
|
THREAD_PRIORITY_TIME_CRITICAL);
|
|
|
|
LONGLONG period = 0;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
LONGLONG now;
|
|
|
|
LONGLONG then = timeGetTime_ns ();
|
|
|
|
while ((now = timeGetTime_ns ()) == then)
|
|
|
|
continue;
|
|
|
|
then = now;
|
|
|
|
while ((now = timeGetTime_ns ()) == then)
|
|
|
|
continue;
|
|
|
|
period += now - then;
|
|
|
|
}
|
|
|
|
SetThreadPriority (GetCurrentThread (), priority);
|
|
|
|
period /= 40000L;
|
|
|
|
minperiod = (DWORD) period;
|
2005-11-11 17:42:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return minperiod;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
clock_getres (clockid_t clk_id, struct timespec *tp)
|
|
|
|
{
|
2010-08-09 18:47:48 +02:00
|
|
|
switch (clk_id)
|
2005-11-11 17:42:15 +01:00
|
|
|
{
|
2010-08-09 18:47:48 +02:00
|
|
|
case CLOCK_REALTIME:
|
|
|
|
{
|
|
|
|
DWORD period = gtod.resolution ();
|
|
|
|
tp->tv_sec = period / 1000;
|
|
|
|
tp->tv_nsec = (period % 1000) * 1000000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CLOCK_MONOTONIC:
|
|
|
|
{
|
|
|
|
LONGLONG period = ntod.resolution ();
|
|
|
|
tp->tv_sec = period / 1000000000;
|
|
|
|
tp->tv_nsec = period % 1000000000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
2005-11-11 17:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
clock_setres (clockid_t clk_id, struct timespec *tp)
|
|
|
|
{
|
|
|
|
static NO_COPY bool period_set;
|
2011-03-29 12:21:30 +02:00
|
|
|
int status;
|
|
|
|
|
2005-11-11 17:42:15 +01:00
|
|
|
if (clk_id != CLOCK_REALTIME)
|
|
|
|
{
|
2010-08-09 18:47:48 +02:00
|
|
|
set_errno (EINVAL);
|
2005-11-11 17:42:15 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-29 12:21:30 +02:00
|
|
|
/* Convert to 100ns to match OS resolution. The OS uses ULONG values
|
|
|
|
to express resolution in 100ns units, so the coarsest timer resolution
|
|
|
|
is < 430 secs. Actually the coarsest timer resolution is only slightly
|
|
|
|
beyond 15ms, but this might change in future OS versions, so we play nice
|
|
|
|
here. */
|
|
|
|
ULONGLONG period = (tp->tv_sec * 10000000ULL) + ((tp->tv_nsec) / 100ULL);
|
|
|
|
|
|
|
|
/* clock_setres is non-POSIX/non-Linux. On QNX, the function always
|
|
|
|
rounds the incoming value to the nearest supported value. */
|
|
|
|
ULONG coarsest, finest, actual;
|
|
|
|
if (NT_SUCCESS (NtQueryTimerResolution (&coarsest, &finest, &actual)))
|
2005-11-11 17:42:15 +01:00
|
|
|
{
|
2011-03-29 12:21:30 +02:00
|
|
|
if (period > coarsest)
|
|
|
|
period = coarsest;
|
|
|
|
else if (finest > period)
|
|
|
|
period = finest;
|
2005-11-11 17:42:15 +01:00
|
|
|
}
|
2011-03-29 12:21:30 +02:00
|
|
|
|
|
|
|
if (period_set
|
|
|
|
&& NT_SUCCESS (NtSetTimerResolution (minperiod * 10000L, FALSE, &actual)))
|
|
|
|
period_set = false;
|
|
|
|
|
|
|
|
status = NtSetTimerResolution (period, TRUE, &actual);
|
|
|
|
if (!NT_SUCCESS (status))
|
2005-11-11 17:42:15 +01:00
|
|
|
{
|
2011-03-29 12:21:30 +02:00
|
|
|
__seterrno_from_nt_status (status);
|
2005-11-11 17:42:15 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-03-29 12:21:30 +02:00
|
|
|
minperiod = actual / 10000L;
|
|
|
|
period_set = true;
|
2005-11-11 17:42:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|