1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use cratecreate_mutex;
use crateFFI_GUARD;
use crate::;
use c_void;
use c_int;
/// Create a new tracked mutex.
///
/// Creates a mutex that will be tracked by the deadlock detector. The current
/// thread will be recorded as the creator of this mutex.
///
/// # Returns
/// * Void pointer to the mutex, or NULL on allocation failure
///
/// # Safety
/// - The returned pointer is a raw pointer to a heap allocation and must be freed by `deloxide_destroy_mutex`.
/// - Any usage from C must ensure not to free or move the returned pointer by other means.
pub unsafe extern "C"
/// Create a new tracked mutex with specified creator thread ID.
///
/// Similar to deloxide_create_mutex(), but allows specifying which thread
/// should be considered the "owner" for resource tracking purposes.
///
/// # Arguments
/// * `creator_thread_id` - ID of the thread to be registered as the creator of this mutex.
///
/// # Returns
/// * Void pointer to the mutex, or NULL on allocation failure
///
/// # Safety
/// - The returned pointer is a raw pointer to a heap allocation and must be freed by `deloxide_destroy_mutex`.
/// - Any usage from C must ensure not to free or move the returned pointer by other means.
pub unsafe extern "C"
/// Destroy a tracked mutex.
///
/// Frees the memory associated with a tracked mutex and removes it from
/// the deadlock detector's tracking.
///
/// # Arguments
/// * `mutex` - Pointer to a mutex created with `deloxide_create_mutex`.
///
/// # Safety
/// - The caller must ensure that `mutex` is not used by any thread after this function is called.
/// - The pointer must be one previously obtained from `deloxide_create_mutex` (i.e., it must not be a stack pointer or null pointer).
pub unsafe extern "C"
/// Lock a tracked mutex.
///
/// Attempts to acquire the lock on a mutex while tracking the operation
/// for deadlock detection.
///
/// # Arguments
/// * `mutex` - Pointer to a mutex created with `deloxide_create_mutex`.
///
/// # Returns
/// * `0` on success
/// * `-1` if the mutex pointer is NULL
///
/// # Safety
/// - The caller must pass a valid pointer to a `Mutex<()>`.
/// - The lock is re-entrant in the sense of C code, but you must not call `deloxide_lock` twice on the same mutex from the same thread without calling `deloxide_unlock`.
pub unsafe extern "C"
/// Unlock a tracked mutex.
///
/// Releases a lock on a mutex while tracking the operation for deadlock detection.
///
/// # Arguments
/// * `mutex` - Pointer to a mutex created with `deloxide_create_mutex`.
///
/// # Returns
/// * `0` on success
/// * `-1` if the mutex pointer is NULL
///
/// # Safety
/// - The pointer must be valid (i.e., a previously created `Mutex<()>`).
/// - The mutex must have been previously locked by the current thread.
pub unsafe extern "C"
/// Get the creator thread ID of a mutex.
///
/// Returns the ID of the thread that created the specified mutex.
///
/// # Arguments
/// * `mutex` - Pointer to a mutex created with `deloxide_create_mutex`.
///
/// # Returns
/// * Thread ID of the creator thread, or 0 if the mutex is NULL
///
/// # Safety
/// - The caller must pass a valid pointer to a `Mutex<()>`.
pub unsafe extern "C"