~ubuntu-branches/ubuntu/saucy/jenkins/saucy

« back to all changes in this revision

Viewing changes to core/src/main/java/hudson/slaves/WorkspaceList.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-01-10 09:50:50 UTC
  • mfrom: (5.1.10 experimental)
  • Revision ID: package-import@ubuntu.com-20130110095050-kj8xuw20gcfh62k3
Tags: 1.480.2+dfsg-1~exp1
* New upstream release (Closes: #696816, #697617):
  - d/control: Added new BD on libjbcrypt-java.
  - d/control: Versioned BD jenkins-winstone >= 0.9.10-jenkins-40.
  - d/control: Versioned BD jenkins-trilead-ssh2 >= 214-jenkins-1.
  - Fixes the following security vulnerabilities:
    CVE-2012-6072, CVE-2012-6073, CVE-2012-6072, CVE-2013-0158.
* Tidied lintian warnings.
* Bumped Standards-Version: 3.9.4, no changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
/**
37
37
 * Used by {@link Computer} to keep track of workspaces that are actively in use.
38
38
 *
39
 
 * <p>
40
 
 * SUBJECT TO CHANGE! Do not use this from plugins directly.
41
 
 *
42
39
 * @author Kohsuke Kawaguchi
43
40
 * @since 1.319
44
41
 * @see Computer#getWorkspaceList()
71
68
 
72
69
        public final FilePath path;
73
70
 
 
71
        /**
 
72
         * Multiple threads can acquire the same lock if they share the same context object.
 
73
         */
 
74
        public final Object context;
 
75
        
 
76
        public int lockCount=1;
 
77
 
74
78
        private Entry(FilePath path, boolean quick) {
 
79
            this(path,quick,new Object()); // unique context
 
80
        }
 
81
        
 
82
        private Entry(FilePath path, boolean quick, Object context) {
75
83
            this.path = path;
76
84
            this.quick = quick;
 
85
            this.context = context;
77
86
        }
78
87
 
79
88
        @Override
110
119
                }
111
120
            };
112
121
        }
 
122
 
 
123
        /**
 
124
         * Creates a {@link Lease} object that points  to the specified path, but the lock
 
125
         * is controlled by the given parent lease object.
 
126
         */
 
127
        public static Lease createLinkedDummyLease(FilePath p, final Lease parent) {
 
128
            return new Lease(p) {
 
129
                public void release() {
 
130
                    parent.release();
 
131
                }
 
132
            };
 
133
        }
113
134
    }
114
135
 
115
136
    private final Map<FilePath,Entry> inUse = new HashMap<FilePath,Entry>();
119
140
 
120
141
    /**
121
142
     * Allocates a workspace by adding some variation to the given base to make it unique.
 
143
     *
 
144
     * <p>
 
145
     * This method doesn't block prolonged amount of time. Whenever a desired workspace
 
146
     * is in use, the unique variation is added.
122
147
     */
123
148
    public synchronized Lease allocate(FilePath base) throws InterruptedException {
 
149
        return allocate(base,new Object());
 
150
    }
 
151
 
 
152
    /**
 
153
     * See {@link #allocate(FilePath)}
 
154
     * 
 
155
     * @param context
 
156
     *      Threads that share the same context can re-acquire the same lock (which will just increment the lock count.)
 
157
     *      This allows related executors to share the same workspace.
 
158
     */
 
159
    public synchronized Lease allocate(FilePath base, Object context) throws InterruptedException {
124
160
        for (int i=1; ; i++) {
125
161
            FilePath candidate = i==1 ? base : base.withSuffix(COMBINATOR+i);
126
162
            Entry e = inUse.get(candidate);
127
 
            if(e!=null && !e.quick)
 
163
            if(e!=null && !e.quick && e.context!=context)
128
164
                continue;
129
 
            return acquire(candidate);
 
165
            return acquire(candidate,false,context);
130
166
        }
131
167
    }
132
168
 
133
169
    /**
134
 
     * Just record that this workspace is being used, without paying any attention to the sycnhronization support.
 
170
     * Just record that this workspace is being used, without paying any attention to the synchronization support.
135
171
     */
136
172
    public synchronized Lease record(FilePath p) {
137
173
        log("recorded  "+p);
145
181
     * Releases an allocated or acquired workspace.
146
182
     */
147
183
    private synchronized void _release(FilePath p) {
148
 
        Entry old = inUse.remove(p);
 
184
        Entry old = inUse.get(p);
149
185
        if (old==null)
150
186
            throw new AssertionError("Releasing unallocated workspace "+p);
 
187
        old.lockCount--;
 
188
        if (old.lockCount==0)
 
189
            inUse.remove(p);
151
190
        notifyAll();
152
191
    }
153
192
 
169
208
     *      This makes other calls to {@link #allocate(FilePath)} to wait for the release of this workspace.
170
209
     */
171
210
    public synchronized Lease acquire(FilePath p, boolean quick) throws InterruptedException {
 
211
        return acquire(p,quick,new Object());
 
212
    }
 
213
    
 
214
    /**
 
215
     * See {@link #acquire(FilePath,boolean)}
 
216
     *
 
217
     * @param context
 
218
     *      Threads that share the same context can re-acquire the same lock (which will just increment the lock count.)
 
219
     *      This allows related executors to share the same workspace.
 
220
     */
 
221
    public synchronized Lease acquire(FilePath p, boolean quick, Object context) throws InterruptedException {
 
222
        Entry e;
 
223
 
172
224
        Thread t = Thread.currentThread();
173
225
        String oldName = t.getName();
174
226
        t.setName("Waiting to acquire "+p+" : "+t.getName());
175
227
        try {
176
 
            while (inUse.containsKey(p)) {
 
228
            while (true) {
 
229
                e = inUse.get(p);
 
230
                if (e==null || e.context==context)
 
231
                    break;
177
232
                wait();
178
233
            }
179
234
        } finally {
180
235
            t.setName(oldName);
181
236
        }
182
237
        log("acquired "+p);
183
 
        inUse.put(p,new Entry(p,quick));
 
238
        
 
239
        if (e!=null)    e.lockCount++;
 
240
        else            inUse.put(p,new Entry(p,quick,context));
184
241
        return lease(p);
185
242
    }
186
243