View Javadoc
1   package org.metricshub.wmi;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2023 - 2025 MetricsHub
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
21   */
22  
23  import java.util.concurrent.locks.ReentrantReadWriteLock;
24  
25  /**
26   * Auto-closeable version of {@link ReentrantReadWriteLock}
27   * <p>
28   * Example:
29   * <pre><code>
30   * AutoCloseableReadWriteLock rwLock = new AutoCloseableReadWriteLock();
31   *
32   * try (AutoCloseableReadWriteLock.AutoCloseableReadLock readLock = rwLock.read()) {
33   * 	// Do things that do not require exclusive access
34   * }
35   * // Read lock (non-exclusive) is unlocked automatically here
36   *
37   * try (AutoCloseableReadWriteLock.AutoCloseableWriteLock writeLock = rwLock.write()) {
38   * 	// Do things that require exclusive access
39   * }
40   * // Lock is unlocked automatically here
41   * </code></pre>
42   *
43   */
44  public class AutoCloseableReadWriteLock extends ReentrantReadWriteLock {
45  
46  	private static final long serialVersionUID = 1L;
47  
48  	/**
49  	 * Constructs a new AutoCloseableReadWriteLock.
50  	 */
51  	public AutoCloseableReadWriteLock() {
52  		super();
53  	}
54  
55  	/**
56  	 * Locks the "read lock" of the {@link ReentrantReadWriteLock}. Use this to enter
57  	 * a section of the code that doesn't require exclusive access.
58  	 * @return An auto-closeable lock, to be used in a try-with-resource block
59  	 */
60  	public AutoCloseableReadLock read() {
61  		return new AutoCloseableReadLock(this.readLock());
62  	}
63  
64  	/**
65  	 * Locks the "write lock" of the {@link ReentrantReadWriteLock}. Use this to enter
66  	 * a section of the code that requires exclusive access.
67  	 * @return An auto-closeable lock, to be used in a try-with-resource block
68  	 */
69  	public AutoCloseableWriteLock write() {
70  		return new AutoCloseableWriteLock(this.writeLock());
71  	}
72  
73  	/**
74  	 * Auto-closeable version of {@link ReadLock}
75  	 */
76  	public static class AutoCloseableReadLock implements AutoCloseable {
77  
78  		final ReadLock readLock;
79  
80  		protected AutoCloseableReadLock(ReadLock lock) {
81  			readLock = lock;
82  			readLock.lock();
83  		}
84  
85  		@Override
86  		public void close() {
87  			readLock.unlock();
88  		}
89  	}
90  
91  	/**
92  	 * Auto-closeable version of {@link WriteLock}
93  	 */
94  	public static class AutoCloseableWriteLock implements AutoCloseable {
95  
96  		final WriteLock writeLock;
97  
98  		protected AutoCloseableWriteLock(WriteLock lock) {
99  			writeLock = lock;
100 			writeLock.lock();
101 		}
102 
103 		@Override
104 		public void close() {
105 			writeLock.unlock();
106 		}
107 	}
108 }