Skip to content

Commit bfc012d

Browse files
committed
Alternative implementation of SharedMutex using flock() mechanism
1 parent d3bf65d commit bfc012d

File tree

5 files changed

+405
-194
lines changed

5 files changed

+405
-194
lines changed

include/lsp-plug.in/ipc/SharedMutex.h

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,30 @@
2424

2525
#include <lsp-plug.in/runtime/version.h>
2626

27+
#include <lsp-plug.in/common/atomic.h>
2728
#include <lsp-plug.in/common/status.h>
2829
#include <lsp-plug.in/common/types.h>
29-
30+
#include <lsp-plug.in/ipc/Thread.h>
3031
#include <lsp-plug.in/runtime/LSPString.h>
31-
3232
#include <lsp-plug.in/runtime/system.h>
3333

3434
namespace lsp
3535
{
3636
namespace ipc
3737
{
38-
#ifndef PLATFORM_WINDOWS
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
3943
struct shared_mutex_t;
40-
#endif /* PLATFORM_WINDOWS */
44+
#endif /* LSP_ROBUST_MUTEX_SUPPORTED */
4145

4246
/**
4347
* Named global non-recursive shared mutex for inter-process communication.
44-
* The object tracks it's lock state and automatically unlocks on close(), so it's implementation
45-
* is not thread safe. Dont' share it between threads of a process. Instead of this, use different
46-
* SharedMutex object and open() method for the same target string.
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.
4751
*/
4852
class SharedMutex
4953
{
@@ -54,10 +58,12 @@ namespace lsp
5458
HANDLE hLock;
5559
#else
5660
int hFD;
61+
62+
#ifdef LSP_ROBUST_MUTEX_SUPPORTED
5763
shared_mutex_t *hLock;
64+
#endif /* LSP_ROBUST_MUTEX_SUPPORTED */
5865
#endif /* PLATFORM_WINDOWS */
59-
60-
bool bLocked;
66+
thread_id_t nOwner;
6167

6268
public:
6369
explicit SharedMutex();
@@ -69,11 +75,17 @@ namespace lsp
6975
SharedMutex & operator = (SharedMutex &&) = delete;
7076

7177
private:
78+
inline bool is_opened() const;
79+
7280
status_t open_internal(const LSPString *name);
7381

82+
private:
7483
#ifndef PLATFORM_WINDOWS
75-
static status_t lock_memory(int fd, shared_mutex_t *mutex);
76-
static status_t unlock_memory(int fd, shared_mutex_t *mutex);
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 */
7789
#endif /* PLATFORM_WINDOWS */
7890

7991
public:

include/lsp-plug.in/ipc/Thread.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ namespace lsp
5555
*/
5656
typedef status_t (* thread_proc_t)(void *arg);
5757

58+
typedef umword_t thread_id_t;
59+
60+
constexpr thread_id_t INVALID_THREAD_ID = 0;
61+
5862
/**
5963
* Thread class
6064
*/
@@ -179,6 +183,12 @@ namespace lsp
179183
* @return number of logical CPUs in the system available for processing
180184
*/
181185
static inline size_t system_cores() { return system::system_cores(); }
186+
187+
/**
188+
* Get current thread identifier.
189+
* @return current thread identifier
190+
*/
191+
static thread_id_t current_thread_id();
182192
};
183193

184194
} /* namespace ipc */

0 commit comments

Comments
 (0)