http://www.hoterran.info/erlang-otp-sys-sourcecode
-module(ch4).
-export([start_link/0]).
-export([alloc/0, free/1]).
-export([init/1]).
-export([system_continue/3, system_terminate/4,
write_debug/3,system_code_change/4]).
start_link() ->
proc_lib:start_link(ch4, init, [self()]).
alloc() ->
ch4 ! {self(), alloc},
receive
{ch4, Res} ->
Res
end.
free(Ch) ->
ch4 ! {free, Ch},
ok.
init(Parent) ->
register(ch4, self()),
Chs = 10,
Deb = sys:debug_options([trace,log]),
proc_lib:init_ack(Parent, {ok, self()}),
loop(Chs, Parent, Deb).
loop(Chs, Parent, Deb) ->
receive
{From, alloc} ->
Deb2 = sys:handle_debug(Deb, fun write_debug/3,
ch4, {in, alloc, From}),
From ! {ch4, "SS"},
Deb3 = sys:handle_debug(Deb2, fun write_debug/3,
ch4, {out, {ch4, "SS"}, From}),
loop("SS", Parent, Deb3);
{free, Ch} ->
Deb2 = sys:handle_debug(Deb, fun write_debug/3,
ch4, {in, {free, Ch}}),
Chs2 = free(Ch),
loop(Chs2, Parent, Deb2);
{system, From, Request} ->
sys:handle_system_msg(Request, From, Parent,
ch4, Deb, Chs)
end.
system_continue(Parent, Deb, Chs) ->
io:format("system continue~n"),
loop(Chs, Parent, Deb).
system_terminate(_Reason, _Parent, _Deb, _Chs) ->
exit(_Reason).
write_debug(Dev, Event, Name) ->
io:format(Dev, "~p event = ~p~n", [Name, Event]).
system_code_change(_Misc, _Module, _OldSvn,_Extra) ->
io:format("change code: ~p~n",[_OldSvn]).