| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 24 апр. 2024 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IPC_SHAREDMUTEX_H_ |
| 23 |
#define LSP_PLUG_IN_IPC_SHAREDMUTEX_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
|
| 27 |
#include <lsp-plug.in/common/atomic.h> |
| 28 |
#include <lsp-plug.in/common/status.h> |
| 29 |
#include <lsp-plug.in/common/types.h> |
| 30 |
#include <lsp-plug.in/ipc/Thread.h> |
| 31 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 32 |
#include <lsp-plug.in/runtime/system.h> |
| 33 |
|
| 34 |
namespace lsp |
| 35 |
{ |
| 36 |
namespace ipc |
| 37 |
{ |
| 38 |
#if defined(PLATFORM_LINUX) || defined(PLATFORM_FREEBSD) |
| 39 |
#define LSP_ROBUST_MUTEX_SUPPORTED |
| 40 |
#endif /* PLATFORM_LINUX, PLATFORM_FREEBSD */ |
| 41 |
|
| 42 |
#ifdef LSP_ROBUST_MUTEX_SUPPORTED |
| 43 |
struct shared_mutex_t; |
| 44 |
#endif /* LSP_ROBUST_MUTEX_SUPPORTED */ |
| 45 |
|
| 46 |
/** |
| 47 |
* Named global non-recursive shared mutex for inter-process communication. |
| 48 |
* The object tracks it's lock state and automatically unlocks on close(). |
| 49 |
* Lock operations can be executed by multiple threads on the same mutex object. |
| 50 |
* Creation and destruction functions are not thread safe. |
| 51 |
*/ |
| 52 |
class SharedMutex |
| 53 |
{ |
| 54 |
private: |
| 55 |
#ifdef PLATFORM_WINDOWS |
| 56 |
typedef void *HANDLE; |
| 57 |
|
| 58 |
HANDLE hLock; |
| 59 |
#else |
| 60 |
int hFD; |
| 61 |
|
| 62 |
#ifdef LSP_ROBUST_MUTEX_SUPPORTED |
| 63 |
shared_mutex_t *hLock; |
| 64 |
#endif /* LSP_ROBUST_MUTEX_SUPPORTED */ |
| 65 |
#endif /* PLATFORM_WINDOWS */ |
| 66 |
thread_id_t nOwner; |
| 67 |
|
| 68 |
public: |
| 69 |
explicit SharedMutex(); |
| 70 |
SharedMutex(const SharedMutex &) = delete; |
| 71 |
SharedMutex(SharedMutex &&) = delete; |
| 72 |
~SharedMutex(); |
| 73 |
|
| 74 |
SharedMutex & operator = (const SharedMutex &) = delete; |
| 75 |
SharedMutex & operator = (SharedMutex &&) = delete; |
| 76 |
|
| 77 |
private: |
| 78 |
inline bool is_opened() const; |
| 79 |
|
| 80 |
status_t open_internal(const LSPString *name); |
| 81 |
|
| 82 |
private: |
| 83 |
#ifndef PLATFORM_WINDOWS |
| 84 |
static status_t lock_descriptor(int fd, int flags); |
| 85 |
#ifdef LSP_ROBUST_MUTEX_SUPPORTED |
| 86 |
static status_t lock_memory(int fd, shared_mutex_t *mutex); |
| 87 |
static status_t unlock_memory(int fd, shared_mutex_t *mutex); |
| 88 |
#endif /* LSP_ROBUST_MUTEX_SUPPORTED */ |
| 89 |
#endif /* PLATFORM_WINDOWS */ |
| 90 |
|
| 91 |
public: |
| 92 |
/** |
| 93 |
* Open or create shared mutex object |
| 94 |
* @param name the UTF-8 encoded name of the shared mutex |
| 95 |
* @return status of operation |
| 96 |
*/ |
| 97 |
status_t open(const char *name); |
| 98 |
|
| 99 |
/** |
| 100 |
* Open or create shared mutex object |
| 101 |
* @param name the name of the shared mutex |
| 102 |
* @return status of operation |
| 103 |
*/ |
| 104 |
status_t open(const LSPString *name); |
| 105 |
|
| 106 |
/** |
| 107 |
* Close shared mutex object |
| 108 |
* @return status of operation |
| 109 |
*/ |
| 110 |
status_t close(); |
| 111 |
|
| 112 |
/** |
| 113 |
* Take a lock on shared mutex. |
| 114 |
* @return status of operation |
| 115 |
*/ |
| 116 |
status_t lock(); |
| 117 |
|
| 118 |
/** |
| 119 |
* Try to obtain a lock on shared mutex for the specified period of time |
| 120 |
* @param delay delay to obtain a lock |
| 121 |
* @return status of operation |
| 122 |
*/ |
| 123 |
status_t lock(system::time_millis_t delay); |
| 124 |
|
| 125 |
/** |
| 126 |
* Try to take a lock on shared mutex. |
| 127 |
* @return status of operation |
| 128 |
*/ |
| 129 |
status_t try_lock(); |
| 130 |
|
| 131 |
/** |
| 132 |
* Unlock shared mutex |
| 133 |
* @return status of operation |
| 134 |
*/ |
| 135 |
status_t unlock(); |
| 136 |
}; |
| 137 |
} /* namespace ipc */ |
| 138 |
} /* namespace lsp */ |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
#endif /* LSP_PLUG_IN_IPC_SHAREDMUTEX_H_ */ |