Re: max timeout
Sorry, I haven't done it reliable and as recommended by best practices

fixed:
sleep(Time) ->
case whereis(timer_server) of
undefined -> receive after Time -> ok end;
_ ->
Ref = make_ref(),
{ok, _} = send_after(Time, Ref),
receive Ref -> ok end
end.
On Fri, Aug 22, 2008 at 10:06 AM, Hynek Vychodil
<vychodil.hynek <at> gmail.com> wrote:
It should be much more simpler to do it without modify timer_server:
sleep(Time) ->
case whereis(timer_server) of
undefined -> receive after Time -> ok end;
_ ->
Ref = make_ref(),
send_after(Time, Ref),
receive Ref -> ok end
end.
On Fri, Aug 22, 2008 at 12:11 AM, Per Hedeland
<per <at> hedeland.org> wrote:
Alpár Jüttner <
alpar <at> cs.elte.hu> wrote:
>
>As I recall, someone noted on that thread that timer:sleep() works well
>for arbitrarily large numbers.
I thought that might have been me
- but I see now that I just said
"the timer module" and gave an example using timer:send_after/2, so I
was almost right...
>Now I looked at the source and found that this is _not_ true. Strangely
>enough, all the functions of the timer modules seem to work correctly
>with large numbers _except_ timer:sleep() which has a definition as
>simple as this:
>
>sleep(T) ->
> receive
> after T -> ok
> end.
Ouch. But no-one will ever need to sleep for 50 days or more, right?
>It might be worth fixing.
And I guess I half-owe the fix - the problem is (as I discovered...)
that there may be (is) code, um, "out there", that relies on being able
to use timer:sleep/1 without having the timer_server running *or*
getting "auto-started". Below is what I believe to be a backwards-
compatible fix (against R12B-3), but it's pretty ugly - would probably
be better to e.g. define a new function long_sleep/1 or somesuch for
the "unlimited" case.
--Per Hedeland
--- lib/stdlib/src/timer.erl.orig 2007-11-26 19:55:44.000000000 +0100
+++ lib/stdlib/src/timer.erl 2008-08-21 23:00:45.000000000 +0200
<at> <at> -75,9 +75,14 <at> <at>
cancel(BRef) ->
req(cancel, BRef).
-sleep(T) ->
- receive
- after T -> ok
+sleep(Time) ->
+ case whereis(timer_server) of
+ undefined ->
+ receive
+ after Time -> ok
+ end;
+ _ ->
+ req(sleep, Time)
end.
%%
<at> <at> -176,6 +181,13 <at> <at>
{reply, {error, badarg}, [], next_timeout()}
end;
+handle_call({sleep, Time, Started}, From, Ts) ->
+ Req = {apply_after, {Time, {gen_server, reply, [From, ok]}}, Started},
+ case handle_call(Req, From, Ts) of
+ {reply, {ok, _}, _, Timeout} -> {noreply, [], Timeout};
+ Reply -> Reply
+ end;
+
handle_call({cancel, BRef = {_Time, Ref}, _}, _From, Ts)
when is_reference(Ref) ->
delete_ref(BRef),
--
--Hynek (Pichi) Vychodil
--
--Hynek (Pichi) Vychodil
<div><div dir="ltr">Sorry, I haven't done it reliable and as recommended by best practices
<br><br>fixed:<br><br>sleep(Time) -><br>
case whereis(timer_server) of<br>
undefined -> receive after Time -> ok end;<br>
_ -><br> Ref = make_ref(),<br> {ok, _} = send_after(Time, Ref),<br> receive Ref -> ok end<br> end.<br><br><br><div class="gmail_quote">On Fri, Aug 22, 2008 at 10:06 AM, Hynek Vychodil <span dir="ltr"><<a href="mailto:vychodil.hynek <at> gmail.com">vychodil.hynek <at> gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote">
<div dir="ltr">It should be much more simpler to do it without modify timer_server:<div class="Ih2E3d">
<br><br>sleep(Time) -><br>
case whereis(timer_server) of<br>
undefined -> receive after Time -> ok end;<br>
</div>
_ -><br> Ref = make_ref(),<br> send_after(Time, Ref),<br> receive Ref -> ok end<br> end.<div>
<div></div>
<div class="Wj3C7c">
<br><br><div class="gmail_quote">On Fri, Aug 22, 2008 at 12:11 AM, Per Hedeland <span dir="ltr"><<a href="mailto:per <at> hedeland.org" target="_blank">per <at> hedeland.org</a>></span> wrote:<br><blockquote class="gmail_quote">
<div>Alpár Jüttner <<a href="mailto:alpar <at> cs.elte.hu" target="_blank">alpar <at> cs.elte.hu</a>> wrote:<br>
><br>
>As I recall, someone noted on that thread that timer:sleep() works well<br>
>for arbitrarily large numbers.<br><br>
</div>I thought that might have been me
- but I see now that I just said<br>
"the timer module" and gave an example using timer:send_after/2, so I<br>
was almost right...<br><div>
<br>
>Now I looked at the source and found that this is _not_ true. Strangely<br>
>enough, all the functions of the timer modules seem to work correctly<br>
>with large numbers _except_ timer:sleep() which has a definition as<br>
>simple as this:<br>
><br>
>sleep(T) -><br>
> receive<br>
> after T -> ok<br>
> end.<br><br>
</div>Ouch. But no-one will ever need to sleep for 50 days or more, right?
<br><div>
<br>
>It might be worth fixing.<br><br>
</div>And I guess I half-owe the fix - the problem is (as I discovered...)<br>
that there may be (is) code, um, "out there", that relies on being able<br>
to use timer:sleep/1 without having the timer_server running *or*≤br>
getting "auto-started". Below is what I believe to be a backwards-<br>
compatible fix (against R12B-3), but it's pretty ugly - would probably<br>
be better to e.g. define a new function long_sleep/1 or somesuch for<br>
the "unlimited" case.<br><br>
--Per Hedeland<br><br>
--- lib/stdlib/src/timer.erl.orig 2007-11-26 19:55:44.000000000 +0100<br>
+++ lib/stdlib/src/timer.erl 2008-08-21 23:00:45.000000000 +0200<br>
<at> <at> -75,9 +75,14 <at> <at> <br>
cancel(BRef) -><br>
req(cancel, BRef).<br><br>
-sleep(T) -><br>
- receive<br>
- after T -> ok<br>
+sleep(Time) -><br>
+ case whereis(timer_server) of<br>
+ undefined -><br>
+ receive<br>
+ after Time -> ok<br>
+ end;<br>
+ _ -><br>
+ req(sleep, Time)<br>
end.<br><br>
%%<br>
<at> <at> -176,6 +181,13 <at> <at> <br>
{reply, {error, badarg}, [], next_timeout()}<br>
end;<br><br>
+handle_call({sleep, Time, Started}, From, Ts) -><br>
+ Req = {apply_after, {Time, {gen_server, reply, [From, ok]}}, Started},<br>
+ case handle_call(Req, From, Ts) of<br>
+ {reply, {ok, _}, _, Timeout} -> {noreply, [], Timeout};<br>
+ Reply -> Reply<br>
+ end;<br>
+<br>
handle_call({cancel, BRef = {_Time, Ref}, _}, _From, Ts)<br>
when is_reference(Ref) -><br>
delete_ref(BRef),<br><div>
<div></div>
<div>_______________________________________________<br>
erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org" target="_blank">erlang-questions <at> erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</div>
</div>
</blockquote>
</div>
<br><br clear="all"><br>
</div>
</div>-- <br>--Hynek (Pichi) Vychodil<br>
</div>
</blockquote>
</div>
<br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br>
</div></div>