Skip to content

Commit bfc3027

Browse files
author
José Valim
committed
Add Process.send/2 and Process.send_after/3
1 parent bec7a0b commit bfc3027

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# v0.12.3-dev
22

33
* Enhancements
4+
* [Kernel] Explicit functions inlined by the compiler, including operators. This means that `Kernel.+/2` will now expand to `:erlang.+/2` and so on
45
* [Mix] Do not fail if a Mix dependency relies on an outdated Elixir version
6+
* [Process] Add `Process.send/2` and `Process.send_after/3`
57
* [Version] Add `Version.compare/2`
68

79
* Bug fixes
810
* [Atom] Inspect `:...` and `:foo@bar` without quoting
911
* [Kernel] Guarantee nullary funs/macros are allowed in guards
12+
* [Process] Ensure monitoring functions are inlined by the compiler
1013

1114
* Deprecations
1215
* [Kernel] `binary_to_term/1`, `binary_to_term/2`, `term_to_binary/1` and `term_to_binary/2` are deprecated in favor of their counterparts in the `:erlang` module

lib/elixir/lib/process.ex

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,37 @@ defmodule Process do
111111
end
112112

113113
@doc """
114-
Returns the pid of a new process started by the application of `fun`.
115-
It behaves exactly the same as `Kernel.spawn/1`.
114+
Sends a message to the given process.
116115
117-
Inlined by the compiler.
116+
It does the same as `Kernel.send/2`.
118117
"""
119-
@spec spawn((() -> any)) :: pid
120-
def spawn(fun) do
121-
:erlang.spawn(fun)
118+
@spec send(dest, msg) :: msg when
119+
dest: pid | port | atom | { atom, node },
120+
msg: any
121+
def send(dest, msg) do
122+
:erlang.send(dest, msg)
123+
end
124+
125+
@doc """
126+
Sends `msg` to `dest` after `time` millisecons.
127+
128+
If `dest` is a pid, it has to be a pid of a local process, dead or alive.
129+
If `dest` is an atom, it is supposed to be the name of a registered process
130+
which is looked up at the time of delivery. No error is given if the name does
131+
not refer to a process.
132+
133+
This function returns a timer reference, which can be read or canceled with
134+
`:erlang.read_timer/1`, `:erlang.start_timer/3` and `:erlang.cancel_timer/1`.
135+
Note `time` cannot be greater than `4294967295`.
136+
137+
Finally, the timer will be automatically canceled if the given `dest` is a pid
138+
which is not alive or when the given pid exits. Note that timers will not be
139+
automatically canceled when `dest` is an atom (as the atom resolution is done
140+
on delivery).
141+
"""
142+
@spec send_after(pid | atom, term, non_neg_integer) :: reference
143+
def send_after(dest, msg, time) do
144+
:erlang.send_after(time, dest, msg)
122145
end
123146

124147
@type spawn_opt :: :link | :monitor | {:priority, :low | :normal | :high} |
@@ -127,6 +150,17 @@ defmodule Process do
127150
{:min_bin_vheap_size, non_neg_integer}
128151
@type spawn_opts :: [spawn_opt]
129152

153+
@doc """
154+
Returns the pid of a new process started by the application of `fun`.
155+
It behaves exactly the same as `Kernel.spawn/1`.
156+
157+
Inlined by the compiler.
158+
"""
159+
@spec spawn((() -> any)) :: pid
160+
def spawn(fun) do
161+
:erlang.spawn(fun)
162+
end
163+
130164
@doc """
131165
Returns the pid of a new process started by the application of `fun`.
132166

0 commit comments

Comments
 (0)