Discussion:
Sleep miliseconds
Igor Medeiros
2011-08-15 13:25:41 UTC
Permalink
Hi,
There is a library that provides a sleep function in milliseconds? I tried
using the posix.sleep() function but it only wait seconds instead
miliseconds.

Thanks.
Luiz Henrique de Figueiredo
2011-08-15 13:35:57 UTC
Permalink
Post by Igor Medeiros
There is a library that provides a sleep function in milliseconds? I tried
using the posix.sleep() function but it only wait seconds instead
miliseconds.
You want to bind to usleep.
See http://lua-users.org/wiki/SleepFunction .

Perhaps posix.sleep should accept fractional values and call usleep.
Steve Litt
2011-08-15 17:01:26 UTC
Permalink
Post by Igor Medeiros
Hi,
There is a library that provides a sleep function in milliseconds?
I tried using the posix.sleep() function but it only wait seconds
instead miliseconds.
Thanks.
http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm#_Make_an_msleep_Function

SteveT

Steve Litt
Author: The Key to Everyday Excellence
http://www.troubleshooters.com/bookstore/key_excellence.htm
Twitter: http://www.twitter.com/stevelitt
Michal Kottman
2011-08-15 17:15:57 UTC
Permalink
Post by Igor Medeiros
There is a library that provides a sleep function in milliseconds? I tried
using the posix.sleep() function but it only wait seconds instead
miliseconds.
You could use `socket.sleep(time)` from the luasocket library [1]. The
parameter `time` is in seconds, but it has millisecond precision, i.e.
to sleep 1 millisecond, call:

socket.sleep(0.001)

It works also on Windows, but it uses the Sleep function from Windows
API [2], which is not precise down to 1 millisecond (AFAIK the minimum
it sleeps is ~15ms [3]).

[1] http://w3.impa.br/~diego/software/luasocket/socket.html#sleep
[2] http://msdn.microsoft.com/en-us/library/ms686298.aspx
[3] http://social.msdn.microsoft.com/Forums/en/clr/thread/facc2b57-9a27-4049-bb32-ef093fbf4c29
startx
2011-08-15 17:57:01 UTC
Permalink
On Mon, 15 Aug 2011 10:25:41 -0300
Post by Igor Medeiros
Hi,
There is a library that provides a sleep function in milliseconds? I
tried using the posix.sleep() function but it only wait seconds
instead miliseconds.
Thanks.
you can also use

http://projects.plentyfact.org/wiki/lua-sleep/Introduction

startx
Fernando Paredes García
2011-08-15 18:12:04 UTC
Permalink
Lua socket can count milliseconds, check the function microtime() from Nutria Seawolf:

https://gitorious.org/nutria/seawolf/blobs/master/calendar/init.lua

Blessings!

--
Fernando Paredes García, http://www.develcuy.com
+51 1 9 8991 7871, Calle Santa Catalina Ancha #377, Cusco - Perú

** Before printing this message, please consider your commitment with the environment, taking care of it depends on you.
** Antes de imprimir este mensaje piensa en tu compromiso con el medio ambiente, protegerlo depende de tí.


---- On Mon, 15 Aug 2011 12:57:01 -0500 startx<***@plentyfact.org> wrote ----


On Mon, 15 Aug 2011 10:25:41 -0300
Igor Medeiros <***@gmail.com> wrote:

> Hi,
> There is a library that provides a sleep function in milliseconds? I
> tried using the posix.sleep() function but it only wait seconds
> instead miliseconds.
>
> Thanks.

you can also use

http://projects.plentyfact.org/wiki/lua-sleep/Introduction

startx
Stephen Irons
2011-08-15 20:24:54 UTC
Permalink
If you are happy to recompile luaposix, it is trivial to copy the posix
sleep() function and create a similar binding to posix usleep().

Perhaps a nicer solution would be to extend luaposix sleep to accept
fractional values and call first posix sleep() for the integer part, then
posix usleep() for the fractional part. However, you have to take care of
how the new sleep function responds to signals.

Stephen Irona
Post by Igor Medeiros
Hi,
There is a library that provides a sleep function in milliseconds? I tried
using the posix.sleep() function but it only wait seconds instead
miliseconds.
Thanks.
--
Stephen Irons
Senior Designer
Tait Radio Communications
175 Roydvale Ave, Christchurch, New Zealand
DDI: +64 - 3 - 357-0713
www.taitworld.com
***@taitradio.com
TAIT: THE RIGHT FIT

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
Luiz Henrique de Figueiredo
2011-08-15 20:57:33 UTC
Permalink
Post by Stephen Irons
Perhaps a nicer solution would be to extend luaposix sleep to accept
fractional values and call first posix sleep() for the integer part, then
posix usleep() for the fractional part. However, you have to take care of
how the new sleep function responds to signals.
Why not simply call usleep with t*1e6 ?
Peter Harris
2011-08-15 23:32:03 UTC
Permalink
Post by Luiz Henrique de Figueiredo
Post by Stephen Irons
Perhaps a nicer solution would be to extend luaposix sleep to accept
fractional values and call first posix sleep() for the integer part, then
posix usleep() for the fractional part. However, you have to take care of
how the new sleep function responds to signals.
Why not simply call usleep with t*1e6 ?
Because POSIX allows usleep to return EINVAL if the argument is >=
1e6, and some OSs are silly enough to actually enforce this limit (eg.
HP-UX).

Peter Harris
Luiz Henrique de Figueiredo
2011-08-15 23:45:07 UTC
Permalink
Post by Luiz Henrique de Figueiredo
Why not simply call usleep with t*1e6 ?
Because POSIX allows usleep to return EINVAL if the argument is >= 1e6
Ah, I missed "The useconds argument shall be less than one million." in
http://pubs.opengroup.org/onlinepubs/009695399/functions/usleep.html

Sorry for the noise.
Gé Weijers
2011-08-15 23:53:27 UTC
Permalink
Post by Peter Harris
Post by Luiz Henrique de Figueiredo
Why not simply call usleep with t*1e6 ?
Because POSIX allows usleep to return EINVAL if the argument is >=
1e6, and some OSs are silly enough to actually enforce this limit (eg.
HP-UX).
The joys of POSIX. Alternatives are:

'nanosleep', which takes a 'struct timespec' as an argument, so the delay
is split up in seconds and nanoseconds.

'select', using NULL pointers for all the file descriptor sets, and a
'struct timeval' to specify the delay, which splits the delay into seconds
and microseconds.


Tony Finch
2011-08-16 10:17:24 UTC
Permalink
Post by Peter Harris
Because POSIX allows usleep to return EINVAL if the argument is >=
1e6, and some OSs are silly enough to actually enforce this limit (eg.
HP-UX).
Yow. I note that usleep() has been removed from POSIX 2008
http://pubs.opengroup.org/onlinepubs/9699919799/idx/iu.html
and that POSIX 2004 recommends nanosleep() instead.

Tony.
--
f.anthony.n.finch <***@dotat.at> http://dotat.at/
East Sole, Lundy, Fastnet: Southwesterly, veering northeasterly 4 or 5,
occasionally 6 at first, becoming variable 3 for a time. Moderate or rough.
Occasional rain, fair for a time. Good, occasionally poor.
Axel Kittenberger
2011-08-16 06:48:38 UTC
Permalink
Use select() with 3 empty filedescriptor sets. It has seconds and
microseconds as parameter and is as syscall available virtually
everywhere.

On Mon, Aug 15, 2011 at 10:24 PM, Stephen Irons
Post by Stephen Irons
If you are happy to recompile luaposix, it is trivial to copy the posix
sleep() function and create a similar binding to posix usleep().
Perhaps a nicer solution would be to extend luaposix sleep to accept
fractional values and call first posix sleep() for the integer part, then
posix usleep() for the fractional part. However, you have to take care of
how the new sleep function responds to signals.
Stephen Irona
Post by Igor Medeiros
Hi,
There is a library that provides a sleep function in milliseconds? I tried
using the posix.sleep() function but it only wait seconds instead
miliseconds.
Thanks.
--
Stephen Irons
Senior Designer
Tait Radio Communications
175 Roydvale Ave, Christchurch, New Zealand
DDI: +64 - 3 - 357-0713
www.taitworld.com
TAIT: THE RIGHT FIT
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
Lorenzo Donati
2011-08-16 08:54:24 UTC
Permalink
Post by Igor Medeiros
Hi,
There is a library that provides a sleep function in milliseconds? I
tried using the posix.sleep() function but it only wait seconds instead
miliseconds.
Thanks.
Warning: slighlty OT question.

Out of curiosity, and relating to modern PC architectures, are there
hardware limits to the availability of an high resolution timer at
software level, or all this diversity is just a problem of operating
system or API idiosyncrasies?

In other words: have OSs a real problem in providing an high resolution
(milli/micro/nano second) sleep function because of hardware lacking the
required functionality?

TIA
-- Lorenzo
Enrico Colombini
2011-08-16 09:22:01 UTC
Permalink
Post by Lorenzo Donati
In other words: have OSs a real problem in providing an high resolution
(milli/micro/nano second) sleep function because of hardware lacking the
required functionality?
I guess they just find more convenient and efficient to use the
multitasking slice time... especially because we're talking about
putting a task to sleep.
--
Enrico
Sean Conner
2011-08-16 10:04:22 UTC
Permalink
Post by Lorenzo Donati
Warning: slighlty OT question.
Out of curiosity, and relating to modern PC architectures, are there
hardware limits to the availability of an high resolution timer at
software level, or all this diversity is just a problem of operating
system or API idiosyncrasies?
It's system dependent. When I wrote my own Lua sleep() function (based
upon nanosleep()), I found that the minimum "sleep quantum" on Solaris to be
0.01 while on Linux, it was more like .0005. I found this out empirically,
but the clock_getres() function (available on Linux and Solaris) confirmed
this (when I found out about the function).

-spc (Ah, the joys of portable code ... )
Lorenzo Donati
2011-08-16 10:17:36 UTC
Permalink
On 16/08/2011 10.54, Lorenzo Donati wrote:

Enrico and Sean: thanks for the reply.

I already knew some of those info, anyway. Sorry, probably I wasn't
clear enough: my curiosity wasn't for the idiosyncrasies in themselves,
but for a possible hardware-related reason beneath (that's why I marked
OT the message).

To be clearer: I'd like to know (whether or not an OS chooses to provide
an high resolution sleep function) if the modern PC hardware is capable
of providing such a timing resolution to the upper sw layer by default
(using some standard BIOS call or standard I/O port or whatever), or if
it depends on some peculiar hw characteristic such as CPU setup, chipset
or similar things.

TIA
-- Lorenzo
Enrico Colombini
2011-08-16 10:39:06 UTC
Permalink
Post by Lorenzo Donati
To be clearer: I'd like to know (whether or not an OS chooses to provide
an high resolution sleep function) if the modern PC hardware is capable
of providing such a timing resolution to the upper sw layer by default
(using some standard BIOS call or standard I/O port or whatever), or if
it depends on some peculiar hw characteristic such as CPU setup, chipset
or similar things.
As far as I know, most (all?) modern CPUs have a high-resolution timer
in the nanosecond range; I don't think there's a portable call to access
it across different OSs, though (I may be wrong).

Of course it would be unwise to implement a true 'sleep' function with
such a short time duration, because of complications such as
interference with the task scheduler and unpredictable interrupt latency
(assuming interrupt could be used; I don't think so in the general
case); it would have to be a busy loop, with all that this implies, or
if possible a periodic check while doing some other processing.
--
Enrico
Lorenzo Donati
2011-08-16 11:19:29 UTC
Permalink
Post by Enrico Colombini
Post by Lorenzo Donati
To be clearer: I'd like to know (whether or not an OS chooses to provide
an high resolution sleep function) if the modern PC hardware is capable
of providing such a timing resolution to the upper sw layer by default
(using some standard BIOS call or standard I/O port or whatever), or if
it depends on some peculiar hw characteristic such as CPU setup, chipset
or similar things.
As far as I know, most (all?) modern CPUs have a high-resolution timer
in the nanosecond range; I don't think there's a portable call to access
it across different OSs, though (I may be wrong).
Thanks! That really hit the spot!
Post by Enrico Colombini
Of course it would be unwise to implement a true 'sleep' function with
such a short time duration, because of complications such as
interference with the task scheduler and unpredictable interrupt latency
(assuming interrupt could be used; I don't think so in the general
case); it would have to be a busy loop, with all that this implies, or
if possible a periodic check while doing some other processing.
Mmmh. Yes, if the allowed minimum sleep interval weren't a multiple of
the scheduler slice interval I suspect it would be a synchronization
nightmare at the kernel level (no expert here - just guessing).

-- Lorenzo
Axel Kittenberger
2011-08-16 16:42:09 UTC
Permalink
Its less a CPU think, than an OS-Kernel/scheduler thing. Traditionally
most OSes use(d) "ticks" to seperate time. For example on Linux the
100Hz interrupt timer was standard, fireing very 10ms and thus
interrupting a process and giving control to the next one. Some people
who wanted more responsive interactiveness set it to 1000Z, many
server admins (me included) set it to 20 HZ without any problems. Less
interrupts means more performance overall for the price for a process
waiting a little longer to react on an event. Nowadays most linux
configurations are "tickless", the kernel sets the next tick on the
fly, depending on current needs. I'm not into the exact mechanics of
these.

Anyway, for high precession sleeps the basic problem is, what use is
there to provide a nanosleep, if the scheduler will anyway give the
next process a few milliseconds to have the CPU? On any Desktop-OS
today, you have to life with the fact if you call sleep, the kernel
might actually give you control back anytime later than that, just as
soon as it can do for you.

There are operating systems that can give you a guaranteed response
time - that are "real time" OSes. Everything gets more complicated so
the kernel can guarantee you timeliness in microseconds or fewer. Some
embedded devices need to do that. Most PCs do not. Most variants of
"Real-Time-Linux" is actually another micro operating system running
above Linux, in which the Linux kernel as we know it, is just one of
its processes.

On Tue, Aug 16, 2011 at 12:17 PM, Lorenzo Donati
Post by Lorenzo Donati
Enrico and Sean: thanks for the reply.
I already knew some of those info, anyway. Sorry, probably I wasn't clear
enough: my curiosity wasn't for the idiosyncrasies in themselves, but for a
possible hardware-related reason beneath (that's why I marked OT the
message).
To be clearer: I'd like to know (whether or not an OS chooses to provide an
high resolution sleep function) if the modern PC hardware is capable of
providing such a timing resolution to the upper sw layer by default (using
some standard BIOS call or standard I/O port or whatever), or if it depends
on some peculiar hw characteristic such as CPU setup, chipset or similar
things.
TIA
-- Lorenzo
Igor Medeiros
2011-08-16 17:15:38 UTC
Permalink
thanks for all replies. (:

Igor
Post by Axel Kittenberger
Its less a CPU think, than an OS-Kernel/scheduler thing. Traditionally
most OSes use(d) "ticks" to seperate time. For example on Linux the
100Hz interrupt timer was standard, fireing very 10ms and thus
interrupting a process and giving control to the next one. Some people
who wanted more responsive interactiveness set it to 1000Z, many
server admins (me included) set it to 20 HZ without any problems. Less
interrupts means more performance overall for the price for a process
waiting a little longer to react on an event. Nowadays most linux
configurations are "tickless", the kernel sets the next tick on the
fly, depending on current needs. I'm not into the exact mechanics of
these.
Anyway, for high precession sleeps the basic problem is, what use is
there to provide a nanosleep, if the scheduler will anyway give the
next process a few milliseconds to have the CPU? On any Desktop-OS
today, you have to life with the fact if you call sleep, the kernel
might actually give you control back anytime later than that, just as
soon as it can do for you.
There are operating systems that can give you a guaranteed response
time - that are "real time" OSes. Everything gets more complicated so
the kernel can guarantee you timeliness in microseconds or fewer. Some
embedded devices need to do that. Most PCs do not. Most variants of
"Real-Time-Linux" is actually another micro operating system running
above Linux, in which the Linux kernel as we know it, is just one of
its processes.
On Tue, Aug 16, 2011 at 12:17 PM, Lorenzo Donati
Post by Lorenzo Donati
Enrico and Sean: thanks for the reply.
I already knew some of those info, anyway. Sorry, probably I wasn't clear
enough: my curiosity wasn't for the idiosyncrasies in themselves, but for
a
Post by Lorenzo Donati
possible hardware-related reason beneath (that's why I marked OT the
message).
To be clearer: I'd like to know (whether or not an OS chooses to provide
an
Post by Lorenzo Donati
high resolution sleep function) if the modern PC hardware is capable of
providing such a timing resolution to the upper sw layer by default
(using
Post by Lorenzo Donati
some standard BIOS call or standard I/O port or whatever), or if it
depends
Post by Lorenzo Donati
on some peculiar hw characteristic such as CPU setup, chipset or similar
things.
TIA
-- Lorenzo
Enrico Colombini
2011-08-16 17:35:09 UTC
Permalink
Post by Axel Kittenberger
There are operating systems that can give you a guaranteed response
time - that are "real time" OSes.
Yes. However, I understand that for time-critical but not
nanosecond-critical applications (such as MIDI music or real-time video
processing) the OS can offer a middle way, usually in a 'media' library,
that guarantees sub-thread-slice accuracy at the cost of some extra
complication. Sort of an "almost real-time".

(I'm not an expert here, so the above statement may be inaccurate)
--
Enrico
Loading...