~rdoering/ubuntu/karmic/erlang/fix-535090

« back to all changes in this revision

Viewing changes to lib/sasl/doc/src/rel/sp.2.erl

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-module(sp).
 
2
-vsn(2).
 
3
 
 
4
-export([start/0, get_data/0, set_data/1]).
 
5
-export([init/1, system_continue/3, system_terminate/4, 
 
6
        system_code_change/4]).
 
7
 
 
8
-record(state, {data, last_pid}).
 
9
 
 
10
start() ->
 
11
    Pid = proc_lib:spawn_link(?MODULE, init, [self()]),
 
12
    {ok, Pid}.
 
13
 
 
14
get_data() ->
 
15
    sp_server ! {self(), get_data},
 
16
    receive
 
17
        {sp_server, Data} -> Data
 
18
    end.
 
19
 
 
20
set_data(Data) ->
 
21
    sp_server ! {self(), set_data, Data}.
 
22
 
 
23
init(Parent) ->
 
24
    register(sp_server, self()),
 
25
    process_flag(trap_exit, true),
 
26
    loop(#state{last_pid = no_one}, Parent).
 
27
 
 
28
loop(State, Parent) ->
 
29
    receive
 
30
        {system, From, Request} ->
 
31
            sys:handle_system_msg(Request, From, Parent, 
 
32
                                  ?MODULE, [], State);
 
33
        {'EXIT', Parent, Reason} ->
 
34
            cleanup(State),
 
35
            exit(Reason);
 
36
        {From, get_data} ->
 
37
            From ! {sp_server, State#state.data},
 
38
            loop(State, Parent);
 
39
        {From, set_data, Data} ->
 
40
            loop(State#state{data = Data, last_pid = From}, Parent);
 
41
        _Any ->
 
42
            loop(State, Parent)
 
43
    end.
 
44
 
 
45
cleanup(State) -> ok.
 
46
 
 
47
%% Here are the sys call back functions
 
48
system_continue(Parent, _, State) ->
 
49
    loop(State, Parent).
 
50
 
 
51
system_terminate(Reason, Parent, _, State) ->
 
52
    cleanup(State),
 
53
    exit(Reason).
 
54
 
 
55
system_code_change({state, Data}, _Mod, 1, _Extra) ->
 
56
    {ok, #state{data = Data, last_pid = no_one}};
 
57
system_code_change(#state{data = Data}, _Mod, {down, 1}, _Extra) ->
 
58
    {ok, {state, Data}}.