View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2026 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix.memory;
19  
20  import java.io.Closeable;
21  import java.io.IOException;
22  
23  /**
24   * A mutually exclusive lock, which may or may not be reentrant.
25   *
26   * @author Christian Kohlschütter
27   */
28  public interface SharedMutex extends Closeable {
29    /**
30     * Try to lock the mutex.
31     * <p>
32     * Note that unless {@link #isReentrant()} is {@code}, trying to lock the mutex a second time will
33     * not succeed until someone else calls {@link #unlock()}
34     *
35     * @param timeoutMillis The timeout, in milliseconds, or {@code 0} for "try indefinitely".
36     * @return {@code true} if the lock was acquired.
37     * @throws IOException on error.
38     */
39    boolean tryLock(int timeoutMillis) throws IOException;
40  
41    /**
42     * Unlocks the mutex.
43     * <p>
44     * By default, no ownership checks are performed.
45     *
46     * @throws IOException on error.
47     */
48    void unlock() throws IOException;
49  
50    /**
51     * Reports if this lock instance is re-entrant, or not.
52     * <p>
53     * The value returned is constant.
54     *
55     * @return {@code true} if reentrant.
56     */
57    boolean isReentrant();
58  
59    /**
60     * Reports if this lock instance can safely be accessed from multiple processes, or not. The
61     * actual way of accessing this {@link SharedMutex} is unspecified, but typically this is
62     * coordinated via {@link SharedMemory}.
63     * <p>
64     * The value returned is constant.
65     *
66     * @return {@code true} if inter-process access is permitted.
67     */
68    boolean isInterProcess();
69  }