Revision: 8014
          http://playerstage.svn.sourceforge.net/playerstage/?rev=8014&view=rev
Author:   gbiggs
Date:     2009-07-14 08:01:51 +0000 (Tue, 14 Jul 2009)

Log Message:
-----------
New nanosleep and usleep replacements for Windows with higher resolution (100ns 
instead of 10ms)

Modified Paths:
--------------
    code/player/trunk/replace/nanosleep.c
    code/player/trunk/replace/usleep.c

Modified: code/player/trunk/replace/nanosleep.c
===================================================================
--- code/player/trunk/replace/nanosleep.c       2009-07-14 03:30:41 UTC (rev 
8013)
+++ code/player/trunk/replace/nanosleep.c       2009-07-14 08:01:51 UTC (rev 
8014)
@@ -7,10 +7,48 @@
 // NOTES:
 // The rem argument is never filled. You cannot rely on it.
 // The return value will always be zero. There is no way to tell if the sleep 
was interrupted.
-// The resolution of this is milliseconds. TODO: fix that by using the high 
performance timer.
+// The resolution of this is 100 nanoseconds, despite the argument being 
seconds and nanoseconds.
 int nanosleep (const struct timespec *req, struct timespec *rem)
 {
-       Sleep (req->tv_sec * 1000 + req->tv_nsec / 1000000);
+       HANDLE timer = NULL;
+       LARGE_INTEGER sleepTime;
+
+       sleepTime.QuadPart = req->tv_sec * 1000000000 + req->tv_nsec / 100;
+
+       timer = CreateWaitableTimer (NULL, TRUE, NULL);
+       if (timer == NULL)
+       {
+               LPVOID buffer = NULL;
+               FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
+                                               GetLastError(), 0, 
reinterpret_cast<LPTSTR> (&buffer), 0, NULL);
+               PLAYER_ERROR2 ("nanosleep: CreateWaitableTimer failed: (%d) 
%s\n",
+                                               GetLastError (), 
reinterpret_cast<LPTSTR> (buffer));
+               LocalFree (buffer);
+               return -1;
+       }
+
+       if (!SetWaitableTimer (timer, &sleepTime, 0, NULL, NULL, 0))
+       {
+               LPVOID buffer = NULL;
+               FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
+                                               GetLastError(), 0, 
reinterpret_cast<LPTSTR> (&buffer), 0, NULL);
+               PLAYER_ERROR2 ("nanosleep: SetWaitableTimer failed: (%d) %s\n",
+                                               GetLastError (), 
reinterpret_cast<LPTSTR> (buffer));
+               LocalFree (buffer);
+               return -1;
+       }
+
+       if (WaitForSingleObject (timer, INFINITE) != WAIT_OBJECT_0)
+       {
+               LPVOID buffer = NULL;
+               FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
+                                               GetLastError(), 0, 
reinterpret_cast<LPTSTR> (&buffer), 0, NULL);
+               PLAYER_ERROR2 ("nanosleep: WaitForSingleObject failed: (%d) 
%s\n",
+                                               GetLastError (), 
reinterpret_cast<LPTSTR> (buffer));
+               LocalFree (buffer);
+               return -1;
+       }
+
        return 0;
 }
 

Modified: code/player/trunk/replace/usleep.c
===================================================================
--- code/player/trunk/replace/usleep.c  2009-07-14 03:30:41 UTC (rev 8013)
+++ code/player/trunk/replace/usleep.c  2009-07-14 08:01:51 UTC (rev 8014)
@@ -6,9 +6,47 @@
 // Replacement for usleep on Windows.
 // NOTES:
 // The return value will always be zero. There is no way to tell if the sleep 
was interrupted.
-// The resolution of this is milliseconds. TODO: fix that by using the high 
performance timer.
+// Although the argument is in microseconds, the resolution of this is 100 
nanoseconds.
 int usleep (int usec)
 {
+       HANDLE timer = NULL;
+       LARGE_INTEGER sleepTime;
+
+       sleepTime.QuadPart = usec * 10000;
+
+       timer = CreateWaitableTimer (NULL, TRUE, NULL);
+       if (timer == NULL)
+       {
+               LPVOID buffer = NULL;
+               FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
+                                               GetLastError(), 0, 
reinterpret_cast<LPTSTR> (&buffer), 0, NULL);
+               PLAYER_ERROR2 ("usleep: CreateWaitableTimer failed: (%d) %s\n",
+                                               GetLastError (), 
reinterpret_cast<LPTSTR> (buffer));
+               LocalFree (buffer);
+               return -1;
+       }
+
+       if (!SetWaitableTimer (timer, &sleepTime, 0, NULL, NULL, 0))
+       {
+               LPVOID buffer = NULL;
+               FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
+                                               GetLastError(), 0, 
reinterpret_cast<LPTSTR> (&buffer), 0, NULL);
+               PLAYER_ERROR2 ("usleep: SetWaitableTimer failed: (%d) %s\n",
+                                               GetLastError (), 
reinterpret_cast<LPTSTR> (buffer));
+               LocalFree (buffer);
+               return -1;
+       }
+
+       if (WaitForSingleObject (timer, INFINITE) != WAIT_OBJECT_0)
+       {
+               LPVOID buffer = NULL;
+               FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
+                                               GetLastError(), 0, 
reinterpret_cast<LPTSTR> (&buffer), 0, NULL);
+               PLAYER_ERROR2 ("usleep: WaitForSingleObject failed: (%d) %s\n",
+                                               GetLastError (), 
reinterpret_cast<LPTSTR> (buffer));
+               LocalFree (buffer);
+               return -1;
+       }
        Sleep (usec / 1000);
        return 0;
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to