use of java.nio.channels.FileLock in project kafka by apache.
the class StateDirectory method lock.
/**
* Get the lock for the {@link TaskId}s directory if it is available
* @param taskId
* @param retry
* @return true if successful
* @throws IOException
*/
boolean lock(final TaskId taskId, int retry) throws IOException {
// we already have the lock so bail out here
if (locks.containsKey(taskId)) {
return true;
}
final File lockFile = new File(directoryForTask(taskId), LOCK_FILE_NAME);
final FileChannel channel;
try {
channel = getOrCreateFileChannel(taskId, lockFile.toPath());
} catch (NoSuchFileException e) {
// file, in this case we will return immediately indicating locking failed.
return false;
}
final FileLock lock = tryLock(retry, channel);
if (lock != null) {
locks.put(taskId, lock);
}
return lock != null;
}
use of java.nio.channels.FileLock in project kafka by apache.
the class StateDirectory method tryLock.
private FileLock tryLock(int retry, final FileChannel channel) throws IOException {
FileLock lock = tryAcquireLock(channel);
while (lock == null && retry > 0) {
try {
Thread.sleep(200);
} catch (Exception ex) {
// do nothing
}
retry--;
lock = tryAcquireLock(channel);
}
return lock;
}
use of java.nio.channels.FileLock in project android_frameworks_base by ParanoidAndroid.
the class MiniThumbFile method saveMiniThumbToFile.
public synchronized void saveMiniThumbToFile(byte[] data, long id, long magic) throws IOException {
RandomAccessFile r = miniThumbDataFile();
if (r == null)
return;
long pos = id * BYTES_PER_MINTHUMB;
FileLock lock = null;
try {
if (data != null) {
if (data.length > BYTES_PER_MINTHUMB - HEADER_SIZE) {
// not enough space to store it.
return;
}
mBuffer.clear();
mBuffer.put((byte) 1);
mBuffer.putLong(magic);
mBuffer.putInt(data.length);
mBuffer.put(data);
mBuffer.flip();
lock = mChannel.lock(pos, BYTES_PER_MINTHUMB, false);
mChannel.write(mBuffer, pos);
}
} catch (IOException ex) {
Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; ", ex);
throw ex;
} catch (RuntimeException ex) {
// Other NIO related exception like disk full, read only channel..etc
Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; disk full or mount read-only? " + ex.getClass());
} finally {
try {
if (lock != null)
lock.release();
} catch (IOException ex) {
// ignore it.
}
}
}
use of java.nio.channels.FileLock in project XobotOS by xamarin.
the class FileChannelImpl method addLock.
/**
* Add a new pending lock to the manager. Throws an exception if the lock
* would overlap an existing lock. Once the lock is acquired it remains in
* this set as an acquired lock.
*/
private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
long lockEnd = lock.position() + lock.size();
for (FileLock existingLock : locks) {
if (existingLock.position() > lockEnd) {
// cannot overlap).
break;
}
if (existingLock.overlaps(lock.position(), lock.size())) {
throw new OverlappingFileLockException();
}
}
locks.add(lock);
}
use of java.nio.channels.FileLock in project XobotOS by xamarin.
the class FileChannelImpl method basicLock.
private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (!shared) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
if (shared) {
throw new NonReadableChannelException();
}
}
if (position < 0 || size < 0) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
FileLock pendingLock = new FileLockImpl(this, position, size, shared);
addLock(pendingLock);
StructFlock flock = new StructFlock();
flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
flock.l_whence = (short) SEEK_SET;
flock.l_start = position;
flock.l_len = translateLockLength(size);
boolean success = false;
try {
success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
} finally {
if (!success) {
removeLock(pendingLock);
}
}
return success ? pendingLock : null;
}
Aggregations