| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009, 2015 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This |
| 5 |
* program and the accompanying materials are made available under the terms of |
| 6 |
* the Eclipse Public License 2.0 which accompanies this distribution, and is |
| 7 |
* available at |
| 8 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: EPL-2.0 |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* IBM Corporation - initial API and implementation |
| 14 |
* Red Hat Inc. - Bug 460967 |
| 15 |
******************************************************************************/ |
| 16 |
package org.eclipse.equinox.internal.p2.engine; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.io.IOException; |
| 20 |
import java.net.MalformedURLException; |
| 21 |
import java.net.URL; |
| 22 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
| 23 |
import org.eclipse.osgi.service.datalocation.Location; |
| 24 |
import org.eclipse.osgi.util.NLS; |
| 25 |
|
| 26 |
/** |
| 27 |
* The purpose of this class is to enable cross process locking. |
| 28 |
* See 257654 for more details. |
| 29 |
*/ |
| 30 |
public class ProfileLock { |
| 31 |
private static final String LOCK_FILENAME = ".lock"; //$NON-NLS-1$ |
| 32 |
|
| 33 |
private final Location location; |
| 34 |
private final Object lock; |
| 35 |
private Thread lockHolder; |
| 36 |
private int waiting; |
| 37 |
|
| 38 |
public ProfileLock(Object lock, File profileDirectory) { |
| 39 |
this.lock = lock; |
| 40 |
location = createLockLocation(profileDirectory); |
| 41 |
} |
| 42 |
|
| 43 |
private static Location createLockLocation(File parent) { |
| 44 |
Location anyLoc = ServiceHelper.getService(EngineActivator.getContext(), Location.class); |
| 45 |
try { |
| 46 |
final URL url = parent.toURL(); |
| 47 |
Location location = anyLoc.createLocation(null, url, false); |
| 48 |
location.set(url, false, LOCK_FILENAME); |
| 49 |
return location; |
| 50 |
} catch (MalformedURLException e) { |
| 51 |
throw new IllegalArgumentException(NLS.bind(Messages.SimpleProfileRegistry_Bad_profile_location, e.getLocalizedMessage()), e); |
| 52 |
} catch (IllegalStateException e) { |
| 53 |
throw e; |
| 54 |
} catch (IOException e) { |
| 55 |
throw new IllegalStateException(e.getLocalizedMessage(), e); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Asserts that this thread currently holds the profile lock. |
| 61 |
* @throws IllegalStateException If this thread does not currently hold the profile lock |
| 62 |
*/ |
| 63 |
public void checkLocked() { |
| 64 |
synchronized (lock) { |
| 65 |
if (lockHolder == null) |
| 66 |
throw new IllegalStateException(Messages.SimpleProfileRegistry_Profile_not_locked); |
| 67 |
|
| 68 |
Thread current = Thread.currentThread(); |
| 69 |
if (lockHolder != current) |
| 70 |
throw new IllegalStateException(Messages.thread_not_owner); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Attempts to obtain an exclusive write lock on a profile. The profile lock must be |
| 76 |
* owned by any process and thread that wants to modify a profile. If the lock |
| 77 |
* is currently held by another thread in this process, this method will block until |
| 78 |
* the lock becomes available. If the lock is currently held by another process, |
| 79 |
* this method returns <code>false</code>. Re-entrant attempts to acquire the |
| 80 |
* same profile lock multiple times in the same thread is not allowed. |
| 81 |
* |
| 82 |
* @return <code>true</code> if the lock was successfully obtained by this thread, |
| 83 |
* and <code>false</code> if another process is currently holding the lock. |
| 84 |
*/ |
| 85 |
public boolean lock() { |
| 86 |
synchronized (lock) { |
| 87 |
Thread current = Thread.currentThread(); |
| 88 |
if (lockHolder == current) |
| 89 |
throw new IllegalStateException(Messages.profile_lock_not_reentrant); |
| 90 |
|
| 91 |
boolean locationLocked = (waiting != 0); |
| 92 |
while (lockHolder != null) { |
| 93 |
locationLocked = true; |
| 94 |
waiting++; |
| 95 |
boolean interrupted = false; |
| 96 |
try { |
| 97 |
lock.wait(); |
| 98 |
} catch (InterruptedException e) { |
| 99 |
interrupted = true; |
| 100 |
} finally { |
| 101 |
waiting--; |
| 102 |
// if interrupted restore interrupt to thread state |
| 103 |
if (interrupted) |
| 104 |
current.interrupt(); |
| 105 |
} |
| 106 |
} |
| 107 |
try { |
| 108 |
if (!locationLocked && !location.lock()) |
| 109 |
return false; |
| 110 |
|
| 111 |
lockHolder = current; |
| 112 |
} catch (IOException e) { |
| 113 |
throw new IllegalStateException(NLS.bind(Messages.SimpleProfileRegistry_Profile_not_locked_due_to_exception, e.getLocalizedMessage()), e); |
| 114 |
} |
| 115 |
return true; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Releases the exclusive write lock on a profile. This method must only be called |
| 121 |
* by a thread that currently owns the lock. |
| 122 |
*/ |
| 123 |
public void unlock() { |
| 124 |
synchronized (lock) { |
| 125 |
if (lockHolder == null) |
| 126 |
throw new IllegalStateException(Messages.SimpleProfileRegistry_Profile_not_locked); |
| 127 |
|
| 128 |
Thread current = Thread.currentThread(); |
| 129 |
if (lockHolder != current) |
| 130 |
throw new IllegalStateException(Messages.thread_not_owner); |
| 131 |
|
| 132 |
lockHolder = null; |
| 133 |
if (waiting == 0) |
| 134 |
location.release(); |
| 135 |
else |
| 136 |
lock.notify(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns whether a thread in this process currently holds the profile lock. |
| 142 |
* |
| 143 |
* @return <code>true</code> if a thread in this process owns the profile lock, |
| 144 |
* and <code>false</code> otherwise |
| 145 |
*/ |
| 146 |
public boolean processHoldsLock() { |
| 147 |
synchronized (lock) { |
| 148 |
return lockHolder != null; |
| 149 |
} |
| 150 |
} |
| 151 |
} |