1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2004-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20-module(otp_5235).
21-export([?MODULE/0]).
22
23-record(commit, {node,
24		 decision, % presume_commit | Decision
25		 ram_copies = [],
26		 disc_copies = [],
27		 disc_only_copies = [],
28		 snmp = [],
29		 schema_ops = [self(),make_ref()]
30		}).
31
32?MODULE() ->
33    process_flag(trap_exit, true),
34    N = 1024,
35    clone(N),
36    wait(N).
37
38wait(0) -> ok;
39wait(N) ->
40    receive
41	{'EXIT',_,normal} ->
42	    wait(N-1);
43	Other ->
44	    exit(Other)
45    end.
46
47clone(0) -> ok;
48clone(N) ->
49    spawn_link(fun worker/0),
50    clone(N-1).
51
52worker() ->
53    Seq = lists:seq(1, 10),
54    PidList = [{N,self()} || N <- Seq],
55    Commit = #commit{ram_copies=PidList,disc_copies=[],
56		     disc_only_copies=[],snmp=[]},
57    List = lists:duplicate(2, Commit),
58    verify(run(2, List)).
59
60verify([#commit{node=true,ram_copies=L}|T]) ->
61    verify_1(L, 1),
62    verify(T);
63verify([]) -> ok.
64
65verify_1([{N,Pid}|T], N) when Pid =:= self() ->
66    verify_1(T, N+1);
67verify_1([], _) -> ok.
68
69run(0, L) -> L;
70run(N, L) -> run(N-1, reverse(L)).
71
72reverse([]) -> [];
73reverse([H|R]) when record(H, commit) ->
74    [H#commit{
75       ram_copies       =  lists:reverse(H#commit.ram_copies),
76       disc_copies      =  lists:reverse(H#commit.disc_copies),
77       disc_only_copies =  lists:reverse(H#commit.disc_only_copies),
78       snmp             = lists:reverse(H#commit.snmp),
79       node = erlang:yield()
80      }
81     | reverse(R)].
82
83
84
85
86