[ci skip] update generated files
[scons.git] / test / Parallel / ref_count.py
blob8a31bf3678bccce2ae30b5cad03b6aa1bd7f0adf
1 #!/usr/bin/env python
3 # __COPYRIGHT__
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27 """
28 Test for a specific race condition that used to stop a build cold when
29 a Node's ref_count would get decremented past 0 and "disappear" from
30 the Taskmaster's walk of the dependency graph.
32 Note that this test does not fail every time, but would at least fail
33 more than 60%-70% of the time on the system(s) I tested.
35 The rather complicated set up here creates a condition where,
36 after building four "object files" simultaneously while running with
37 -j4, sets up a race condition amongst the three dependencies of the
38 c6146/cpumanf.out file, where two of the dependencies are built at the
39 same time (that is, by the same command) and one is built separately.
41 We used to detect Nodes that had been started but not finished building
42 and just set the waiting ref_count to the number of Nodes. In this case,
43 if we got unlucky, we'd re-visit the Nodes for the two files first and
44 set the ref_count to two *before* the command that built individual node
45 completed and decremented the ref_count from two to one. Then when the
46 two files completed, we'd update the ref_count to 1 - 2 = -1, which would
47 cause the Taskmaster to *not* "wake up" the Node because it's ref_count
48 hadn't actually reached 0.
50 (The solution was to not set the ref_count, but to add to it only the
51 Nodes that were, in fact, added to the waiting_parents lists of various
52 child Nodes.)
53 """
55 import TestSCons
57 _python_ = TestSCons._python_
59 test = TestSCons.TestSCons()
61 test.write('build.py', """\
62 import sys
63 import time
64 args = sys.argv[1:]
65 outputs = []
66 while args:
67 if args[0][0] != '-':
68 break
69 arg = args.pop(0)
70 if arg == '-o':
71 outputs.append(args.pop(0))
72 continue
73 if arg == '-s':
74 time.sleep(int(args.pop(0)))
75 contents = ''
76 for ifile in args:
77 with open(ifile, 'r') as ifp:
78 contents = contents + ifp.read()
79 for ofile in outputs:
80 with open(ofile, 'w') as ofp:
81 ofp.write('%s: building from %s\\n' % (ofile, " ".join(args)))
82 ofp.write(contents)
83 """)
89 test.write('SConstruct', """\
90 env = Environment()
91 cmd = r'%(_python_)s build.py -o $TARGET $SOURCES'
92 f1 = env.Command('c6416/cpumanf/file1.obj', 'file1.c', cmd)
93 f2 = env.Command('c6416/cpumanf/file2.obj', 'file2.c', cmd)
94 f3 = env.Command('c6416/cpumanf/file3.obj', 'file3.c', cmd)
95 f4 = env.Command('c6416/cpumanf/file4.obj', 'file4.c', cmd)
96 f5 = env.Command('c6416/cpumanf/file5.obj', 'file5.c', cmd)
97 f6 = env.Command('c6416/cpumanf/file6.obj', 'file6.c', cmd)
99 objs = f1 + f2 + f3 + f4 + f5 + f6
101 btc = env.Command('build/target/cpumanf.out', 'c6416/cpumanf.out', cmd)
103 addcheck_obj = env.Command('addcheck.obj', 'addcheck.c', cmd)
105 addcheck_exe = env.Command('addcheck.exe', addcheck_obj, cmd)
107 cmd2 = r'%(_python_)s build.py -s 2 -o ${TARGETS[0]} -o ${TARGETS[1]} $SOURCES'
109 cksums = env.Command(['c6416/cpumanf_pre_cksum.out',
110 'c6416/cpumanf_pre_cksum.map'],
111 objs,
112 cmd2)
114 cpumanf_out = env.Command('c6416/cpumanf.out',
115 cksums + addcheck_exe,
116 cmd)
118 cpumanf = env.Alias('cpumanf', objs + btc)
120 env.Command('after.out', cpumanf, r'%(_python_)s build.py -o $TARGET after.in')
121 """ % locals())
123 test.write('file1.c', "file1.c\n")
124 test.write('file2.c', "file2.c\n")
125 test.write('file3.c', "file3.c\n")
126 test.write('file4.c', "file4.c\n")
127 test.write('file5.c', "file5.c\n")
128 test.write('file6.c', "file6.c\n")
130 test.write('addcheck.c', "addcheck.c\n")
132 test.write('after.in', "after.in\n")
134 test.run(arguments = '-j4 after.out')
136 test.must_match('after.out', """\
137 after.out: building from after.in
138 after.in
139 """, mode='r')
141 test.write('file5.c', "file5.c modified\n")
143 test.write('after.in', "after.in modified\n")
145 test.run(arguments = '-j4 after.out')
147 test.must_match('after.out', """\
148 after.out: building from after.in
149 after.in modified
150 """, mode='r')
152 test.pass_test()
154 # Local Variables:
155 # tab-width:4
156 # indent-tabs-mode:nil
157 # End:
158 # vim: set expandtab tabstop=4 shiftwidth=4: