001package org.eclipse.aether.named.support; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayDeque; 023import java.util.Deque; 024import java.util.concurrent.TimeUnit; 025 026/** 027 * Named lock support implementation that is using "adapted" semaphore (to be able to use semaphores not sharing common 028 * API). 029 */ 030public class AdaptedSemaphoreNamedLock extends NamedLockSupport 031{ 032 /** 033 * Wrapper for semaphore-like stuff, that do not share common ancestor. Semaphore must be created to support {@link 034 * Integer#MAX_VALUE} permissions. 035 */ 036 public interface AdaptedSemaphore 037 { 038 boolean tryAcquire( int perms, long time, TimeUnit unit ) throws InterruptedException; 039 040 void release( int perms ); 041 } 042 043 /** 044 * Count of permissions involved with "nop" locking. When required lock step is preceded with a step that already 045 * fulfils currently requested locking, no locking is needed. In other words, caller already possesses the access to 046 * lock protected resource. The "nop" locking is used to track proper "boxing" of lock/unlock calls. 047 */ 048 private static final int NONE = 0; 049 050 /** 051 * Count of permissions involved with shared locking 052 */ 053 private static final int SHARED = 1; 054 055 /** 056 * Count of permissions involved with exclusive locking 057 */ 058 private static final int EXCLUSIVE = Integer.MAX_VALUE; 059 060 private final ThreadLocal<Deque<Integer>> threadPerms; 061 062 private final AdaptedSemaphore semaphore; 063 064 public AdaptedSemaphoreNamedLock( final String name, final NamedLockFactorySupport factory, 065 final AdaptedSemaphore semaphore ) 066 { 067 super( name, factory ); 068 this.threadPerms = ThreadLocal.withInitial( ArrayDeque::new ); 069 this.semaphore = semaphore; 070 } 071 072 @Override 073 public boolean lockShared( final long time, final TimeUnit unit ) throws InterruptedException 074 { 075 Deque<Integer> perms = threadPerms.get(); 076 if ( !perms.isEmpty() ) 077 { // we already own shared or exclusive lock 078 perms.push( NONE ); 079 return true; 080 } 081 if ( semaphore.tryAcquire( SHARED, time, unit ) ) 082 { 083 perms.push( SHARED ); 084 return true; 085 } 086 return false; 087 } 088 089 @Override 090 public boolean lockExclusively( final long time, final TimeUnit unit ) throws InterruptedException 091 { 092 Deque<Integer> perms = threadPerms.get(); 093 if ( !perms.isEmpty() ) 094 { // we already own shared or exclusive lock 095 if ( perms.contains( EXCLUSIVE ) ) 096 { 097 perms.push( NONE ); 098 return true; 099 } 100 else 101 { 102 return false; // Lock upgrade not supported 103 } 104 } 105 if ( semaphore.tryAcquire( EXCLUSIVE, time, unit ) ) 106 { 107 perms.push( EXCLUSIVE ); 108 return true; 109 } 110 return false; 111 } 112 113 @Override 114 public void unlock() 115 { 116 Deque<Integer> steps = threadPerms.get(); 117 if ( steps.isEmpty() ) 118 { 119 throw new IllegalStateException( "Wrong API usage: unlock w/o lock" ); 120 } 121 int step = steps.pop(); 122 if ( step > 0 ) 123 { 124 semaphore.release( step ); 125 } 126 } 127}