On Fri, Sep 2, 2011 at 14:22, JonY <jo...@users.sourceforge.net> wrote:
> Why not, the more code the merrier :)
>
[
2011-09-02 Dongsheng Song <dongsh...@users.sourceforge.net>:
* misc/nanosleep.c: New.
* testcases/t_nanosleep.c: New.
]
New:
mingw-w64-crt/misc/nanosleep.c
mingw-w64-crt/testcases/t_nanosleep.c
Please update mingw-w64-crt/Makefile.am and mingw-w64-headers/crt/time.h
when you want to add these files to mingw-w64-crt.
--
Dongsheng
#include <stdio.h>
#include <time.h>
#define POW10_3 1000
#define POW10_6 1000000
extern int __cdecl getntptimeofday(struct timespec *tp, struct timezone *tz);
__int64 timespec_diff_as_ms(struct timespec *__old, struct timespec *__new)
{
return (__new->tv_sec - __old->tv_sec) * POW10_3
+ (__new->tv_nsec - __old->tv_nsec) / POW10_6;
}
int main(int argc, char *argv[])
{
int rc;
struct timespec tp, tp2, request = { 1, 0 }, remain;
getntptimeofday(&tp, NULL);
rc = nanosleep(&request, &remain);
getntptimeofday(&tp2, NULL);
if (rc != 0) {
printf("remain: %d.%09d\n", (int) remain.tv_sec, (int) remain.tv_nsec);
}
printf("%d.%09d\n", (int) tp.tv_sec, (int) tp.tv_nsec);
printf("%d.%09d\n", (int) tp2.tv_sec, (int) tp2.tv_nsec);
printf("sleep %d ms\n", (int) timespec_diff_as_ms(&tp, &tp2));
return 0;
}
/**
* This file has no copyright assigned and is placed in the Public Domain.
* This file is part of the w64 mingw-runtime package.
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/
#include <errno.h>
#include <time.h>
#include <windows.h>
#define POW10_3 1000
#define POW10_6 1000000
#define POW10_9 1000000000
#define MAX_SLEEP_IN_MS 4294967294UL
/**
* Sleep for the specified time.
* @param request The desired amount of time to sleep.
* @param remain The remain amount of time to sleep.
* @return If the function succeeds, the return value is 0.
* If the function fails, the return value is -1,
* with errno set to indicate the error.
*/
int nanosleep(const struct timespec *request, struct timespec *remain)
{
unsigned long ms, rc = 0;
unsigned __int64 u64;
if (request->tv_sec < 0 || request->tv_nsec < 0 || request->tv_nsec >= POW10_9) {
errno = EINVAL;
return -1;
}
u64 = request->tv_sec * POW10_3 + request->tv_nsec / POW10_6;
while (u64 > 0) {
if (u64 >= MAX_SLEEP_IN_MS) ms = MAX_SLEEP_IN_MS;
else ms = (unsigned long) u64;
u64 -= ms;
rc = SleepEx(ms, TRUE);
}
if (rc != 0) { /* WAIT_IO_COMPLETION */
if (remain != NULL) {
remain->tv_sec = u64 / POW10_3;
remain->tv_nsec = (long) (u64 % POW10_3) * POW10_6;
}
errno = EINTR;
return -1;
}
return 0;
}
------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public