Debian Codegraph @

rake.d/vendor/drake-0.9.2.0.3.1/lib/rake/parallel.rb

raw
1 #
2 # Parallel task execution for Rake.
3 #
4 # The comp_tree package is used to create a computation tree which
5 # executes Rake tasks.
6 #
7 # Tasks are first collected with a single-threaded dry run. This
8 # expands file rules, resolves prequisite names and finds task
9 # arguments. A computation tree is then built with the gathered
10 # tasks.
11 #
12 # Note that prerequisites are context-dependent. It is therefore not
13 # possible to create a 1-to-1 mapping between Rake::Tasks and
14 # CompTree::Nodes.
15 #
16 # Author: James M. Lawrence <[email protected]>
17 #
18
19 require 'comp_tree'
20
21 module Rake
22 module Parallel #:nodoc:
23 class Driver
24 # Tasks collected during the dry-run phase.
25 attr_reader :tasks
26
27 def initialize
28 @tasks = Hash.new
29 @mutex = Mutex.new
30 end
31
32 #
33 # Top-level parallel invocation.
34 #
35 # Called from Task#invoke (routed through Task#invoke_parallel).
36 #
37 def invoke(threads, task, *task_args)
38 if @mutex.try_lock
39 begin
40 @tasks.clear
41
42 # dry run task collector
43 task.invoke_serial(*task_args)
44
45 if @tasks.has_key? task
46 # hand it off to comp_tree
47 compute(task, threads)
48 end
49 ensure
50 @mutex.unlock
51 end
52 else
53 raise InvokeInsideInvoke
54 end
55 end
56
57 #
58 # Build and run the computation tree.
59 #
60 # Called from Parallel::Driver#invoke.
61 #
62 def compute(root_task, threads)
63 CompTree.build do |driver|
64 @tasks.each_pair do |task, (task_args, prereqs)|
65 needed_prereq_names = []
66
67 prereqs.each do |prereq|
68 # if a prereq is not needed then it didn't get into @tasks
69 if @tasks.has_key? prereq
70 needed_prereq_names << prereq.name
71 end
72 end
73
74 # define a computation node which executes the task
75 driver.define(task.name, *needed_prereq_names) {
76 task.execute(task_args)
77 }
78 end
79
80 # punch it
81 driver.compute(root_task.name, threads)
82 end
83 end
84 end
85
86 module ApplicationMixin
87 def parallel
88 @parallel ||= Driver.new
89 end
90 end
91
92 module TaskMixin
93 #
94 # Top-level parallel invocation.
95 #
96 # Called from Task#invoke.
97 #
98 def invoke_parallel(*task_args)
99 application.parallel.
100 invoke(application.options.threads, self, *task_args)
101 end
102
103 #
104 # Collect tasks for parallel execution.
105 #
106 # Called from Task#invoke_with_call_chain.
107 #
108 def collect_for_parallel_execution(task_args, new_chain, previous_chain)
109 # call Task#invoke_prerequisites directly (avoid overrides)
110 prereqs = Task.instance_method(:invoke_prerequisites).
111 bind(self).call(task_args, new_chain)
112
113 tasks = application.parallel.tasks
114 if needed? or prereqs.any? { |p| tasks[p] }
115 tasks[self] = [task_args, prereqs]
116 end
117 end
118 end
119 end
120
121 #
122 # Error indicating Task#invoke was called inside Task#invoke
123 # during parallel execution.
124 #
125 class InvokeInsideInvoke < StandardError
126 def message
127 "Cannot call Task#invoke within a task during parallel execution."
128 end
129 end
130 end