Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added DWARF debug information support for JIT-compiled code
- Added I2C and SPI APIs to rp2 platform
- Added `code:get_object_code/1`
- Added `timer:send_after/2`, `timer:send_after/3` and `timer:apply_after/4`

### Changed
- ~10% binary size reduction by rewriting module loading logic
Expand Down
52 changes: 51 additions & 1 deletion libs/estdlib/src/timer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
%%-----------------------------------------------------------------------------
-module(timer).

-export([sleep/1]).
-export([sleep/1, send_after/2, send_after/3, apply_after/4]).

%%-----------------------------------------------------------------------------
%% @param Timeout number of milliseconds to sleep or `infinity'
Expand All @@ -43,3 +43,53 @@ sleep(Timeout) ->
after Timeout ->
ok
end.

%%-----------------------------------------------------------------------------
%% @param Time time in milliseconds after which to send the message.
%% @param Message the message to send to the calling process.
%% @returns `{ok, TRef}' where `TRef' is a reference to the timer.
%%
%% @doc Sends `Message' to the calling process after `Time' milliseconds.
%% @end
%%-----------------------------------------------------------------------------
-spec send_after(Time :: non_neg_integer(), Message :: term()) -> {ok, reference()}.
send_after(Time, Message) ->
TRef = timer_manager:send_after(Time, self(), Message),
{ok, TRef}.

%%-----------------------------------------------------------------------------
%% @param Time time in milliseconds after which to send the message.
%% @param Pid the process to which to send the message.
%% @param Message the message to send.
%% @returns `{ok, TRef}' where `TRef' is a reference to the timer.
%%
%% @doc Sends `Message' to `Pid' after `Time' milliseconds.
%% @end
%%-----------------------------------------------------------------------------
-spec send_after(Time :: non_neg_integer(), Pid :: pid() | atom(), Message :: term()) ->
{ok, reference()}.
send_after(Time, Pid, Message) ->
TRef = timer_manager:send_after(Time, Pid, Message),
{ok, TRef}.

%%-----------------------------------------------------------------------------
%% @param Time time in milliseconds after which to apply the function.
%% @param Module the module of the function to apply.
%% @param Function the function to apply.
%% @param Arguments the arguments to pass to the function.
%% @returns `{ok, TRef}' where `TRef' is a reference to the timer.
%%
%% @doc Applies `Module:Function(Arguments)' after `Time' milliseconds.
%% @end
%%-----------------------------------------------------------------------------
-spec apply_after(
Time :: non_neg_integer(), Module :: module(), Function :: atom(), Arguments :: [term()]
) -> {ok, reference()}.
apply_after(Time, Module, Function, Arguments) ->
Pid = spawn(fun() ->
receive
apply_now -> apply(Module, Function, Arguments)
end
end),
TRef = timer_manager:send_after(Time, Pid, apply_now),
{ok, TRef}.
44 changes: 44 additions & 0 deletions tests/libs/estdlib/test_timer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
ok = test_timer_loop(),
ok = test_timer_badargs(),
ok = test_infinity(),
ok = test_send_after_2(),
ok = test_send_after_3(),
ok = test_apply_after(),
ok.

test_timer() ->
Expand Down Expand Up @@ -80,3 +83,44 @@
ok ->
ok = etest:assert_true(erlang:is_process_alive(Pid))
end.

test_send_after_2() ->
{ok, _TRef} = timer:send_after(500, hello),
receive
hello -> {error, too_early}

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / Analyze (cpp)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 27)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF -DAVM_DISABLE_JIT_DWARF=OFF)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (clang-18, 26)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 26)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (clang-12, clang++-12, clang-12, ubuntu-22.04)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-11, 28)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 28)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (cc, 27, c++, 1.17)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-13, 27)

a term is constructed, but never used

Check warning on line 90 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-11, 27)

a term is constructed, but never used
after 250 ->
ok
end,
receive
hello -> ok
after 10000 ->
{error, timeout}
end.

test_send_after_3() ->
Self = self(),
{ok, _TRef} = timer:send_after(500, Self, hello_dest),
receive
hello_dest -> {error, too_early}

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / Analyze (cpp)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 27)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF -DAVM_DISABLE_JIT_DWARF=OFF)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (clang-18, 26)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 26)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (clang-12, clang++-12, clang-12, ubuntu-22.04)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-11, 28)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 28)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (cc, 27, c++, 1.17)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-13, 27)

a term is constructed, but never used

Check warning on line 104 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-11, 27)

a term is constructed, but never used
after 250 ->
ok
end,
receive
hello_dest -> ok
after 10000 ->
{error, timeout}
end.

test_apply_after() ->
Self = self(),
{ok, _TRef} = timer:apply_after(500, erlang, send, [Self, apply_result]),
receive
apply_result -> {error, too_early}

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / Analyze (cpp)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 27)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 27, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF -DAVM_DISABLE_JIT_DWARF=OFF)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (clang-18, 26)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 26)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (clang-12, clang++-12, clang-12, ubuntu-22.04)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-11, 28)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-26, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-15-intel, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / run-tests (macos-15, 28)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 26, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 28, mbedtls@3)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (macos-14, 28, mbedtls@3, -DAVM_DISABLE_JIT=OFF)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (cc, 27, c++, 1.17)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-13, 27)

a term is constructed, but never used

Check warning on line 118 in tests/libs/estdlib/test_timer.erl

View workflow job for this annotation

GitHub Actions / build-and-test (gcc-11, 27)

a term is constructed, but never used
after 250 ->
ok
end,
receive
apply_result -> ok
after 10000 ->
{error, timeout}
end.
Loading