Skip to content

Commit a1dbb26

Browse files
committed
Allow timeouts up to 64 bits (OTP-25 behavior)
Signed-off-by: Fred Dushin <[email protected]>
1 parent 219d828 commit a1dbb26

File tree

5 files changed

+9
-8
lines changed

5 files changed

+9
-8
lines changed

src/libAtomVM/opcodesswitch.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,15 +2120,16 @@ static bool maybe_call_native(Context *ctx, AtomString module_name, AtomString f
21202120
DECODE_COMPACT_TERM(timeout, code, i, next_off)
21212121

21222122
#ifdef IMPL_EXECUTE_LOOP
2123+
avm_int64_t t = 0;
21232124
if (term_is_any_integer(timeout)) {
2124-
avm_int64_t t = term_maybe_unbox_int64(timeout);
2125-
if (t < 0 || t > 4294967295L) {
2125+
t = term_maybe_unbox_int64(timeout);
2126+
if (UNLIKELY(t < 0)) {
21262127
RAISE_ERROR(TIMEOUT_VALUE_ATOM);
21272128
}
21282129
} else if (UNLIKELY(timeout != INFINITY_ATOM)) {
21292130
RAISE_ERROR(TIMEOUT_VALUE_ATOM);
21302131
}
2131-
TRACE("wait_timeout/2, label: %i, timeout: %li\n", label, (long int) term_to_int32(timeout));
2132+
TRACE("wait_timeout/2, label: %i, timeout: %li\n", label, (long int) t);
21322133

21332134
NEXT_INSTRUCTION(next_off);
21342135
//TODO: it looks like x[0] might be used instead of jump_to_on_restore
@@ -2139,7 +2140,7 @@ static bool maybe_call_native(Context *ctx, AtomString module_name, AtomString f
21392140
int needs_to_wait = 0;
21402141
if ((ctx->flags & (WaitingTimeout | WaitingTimeoutExpired)) == 0) {
21412142
if (timeout != INFINITY_ATOM) {
2142-
scheduler_set_timeout(ctx, term_to_int32(timeout));
2143+
scheduler_set_timeout(ctx, t);
21432144
}
21442145
needs_to_wait = 1;
21452146
} else if ((ctx->flags & WaitingTimeout) != 0) {

src/libAtomVM/scheduler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static void scheduler_timeout_callback(struct TimerWheelItem *it)
139139
scheduler_make_ready(ctx->global, ctx);
140140
}
141141

142-
void scheduler_set_timeout(Context *ctx, uint32_t timeout)
142+
void scheduler_set_timeout(Context *ctx, avm_int64_t timeout)
143143
{
144144
GlobalContext *glb = ctx->global;
145145

src/libAtomVM/scheduler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Context *scheduler_next(GlobalContext *global, Context *c);
102102
* @param ctx the context that will be put on sleep
103103
* @param timeout amount of time to be waited in milliseconds.
104104
*/
105-
void scheduler_set_timeout(Context *ctx, uint32_t timeout);
105+
void scheduler_set_timeout(Context *ctx, avm_int64_t timeout);
106106

107107
void scheduler_cancel_timeout(Context *ctx);
108108

src/libAtomVM/timer_wheel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern "C" {
2929
#include <stdint.h>
3030

3131
#include "list.h"
32+
#include "term_typedef.h"
3233

3334
struct TimerWheelItem;
3435
typedef void(timer_wheel_callback_t)(struct TimerWheelItem *);
@@ -84,7 +85,7 @@ static inline void timer_wheel_item_init(struct TimerWheelItem *it, timer_wheel_
8485
it->callback = cb;
8586
}
8687

87-
static inline uint64_t timer_wheel_expiry_to_monotonic(const struct TimerWheel *tw, uint32_t expiry)
88+
static inline uint64_t timer_wheel_expiry_to_monotonic(const struct TimerWheel *tw, avm_int64_t expiry)
8889
{
8990
return tw->monotonic_time + expiry;
9091
}

tests/libs/estdlib/test_timer.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ timer_loop(I) ->
6767

6868
test_timer_badargs() ->
6969
{'EXIT', {timeout_value, _}} = (catch timer:sleep(-1)),
70-
{'EXIT', {timeout_value, _}} = (catch timer:sleep(4294967295 + 1)),
7170
{'EXIT', {timeout_value, _}} = (catch timer:sleep(not_infinity)),
7271
ok.
7372

0 commit comments

Comments
 (0)