|
13 | 13 | #include "traccc/definitions/qualifiers.hpp"
|
14 | 14 |
|
15 | 15 | namespace traccc::device {
|
| 16 | +/** |
| 17 | + * @brief An RAII-based lock, analogous to `std::unique_lock`. |
| 18 | + * |
| 19 | + * This unique lock type can be used to increase the safety of critical |
| 20 | + * sections at it guarantees that the lock will be released when it goes out of |
| 21 | + * scope. |
| 22 | + * |
| 23 | + * @tparam Mutex The underlying mutex type. |
| 24 | + * |
| 25 | + * @warning Be wary of using this code across different architectures, as |
| 26 | + * lockstep execution may seriously impact the behaviour of this code. In |
| 27 | + * particular, be wary of the fact that, on lockstep architectures, trying to |
| 28 | + * acquire this lock may result in deadlock as all locks will try to acquire |
| 29 | + * the lock at the same time. To resolve this, only try to acquire the lock |
| 30 | + * from one thread in a work group or block of threads. When using code written |
| 31 | + * in this way on non-lockstep architectures, be aware that the thread holding |
| 32 | + * the lock may exit a block before the other threads, releasing the lock early |
| 33 | + * and breaking thread safety. To avoid this problem, always synchronize |
| 34 | + * threads after the critical section. |
| 35 | + */ |
16 | 36 | template <typename Mutex>
|
17 | 37 | class unique_lock {
|
18 | 38 | public:
|
19 | 39 | using mutex_type = Mutex;
|
20 | 40 |
|
21 |
| - /* |
22 |
| - * Construct a unique lock without locking. |
| 41 | + /** |
| 42 | + * @brief Construct a unique lock without locking. |
23 | 43 | */
|
24 | 44 | TRACCC_HOST_DEVICE
|
25 | 45 | unique_lock(mutex_type& m, std::defer_lock_t);
|
26 | 46 |
|
27 |
| - /* |
28 |
| - * Construct a unique lock, attempting to lock it. |
| 47 | + /** |
| 48 | + * @brief Construct a unique lock, attempting to lock it. |
| 49 | + * |
| 50 | + * @warning This function returning does _not_ guarantee that the lock has |
| 51 | + * been acquired. |
| 52 | + * |
| 53 | + * @note Despite the warnings about acquiring locks in lockstep |
| 54 | + * architectures, calling this function across multiple threads is safe as |
| 55 | + * it is a non-blocking lock, e.g. it will fail for all but at most one |
| 56 | + * thread. |
29 | 57 | */
|
30 | 58 | TRACCC_HOST_DEVICE
|
31 | 59 | unique_lock(mutex_type& m, std::try_to_lock_t);
|
32 | 60 |
|
33 |
| - /* |
34 |
| - * Construct a unique lock which was previously locked. |
| 61 | + /** |
| 62 | + * @brief Construct a unique lock which was previously locked. |
35 | 63 | */
|
36 | 64 | TRACCC_HOST_DEVICE
|
37 | 65 | unique_lock(mutex_type& m, std::adopt_lock_t);
|
38 | 66 |
|
39 |
| - /* |
40 |
| - * Destroy a lock, freeing the underlying mutex. |
| 67 | + /** |
| 68 | + * @brief Destroy a lock, freeing the underlying mutex. |
41 | 69 | */
|
42 | 70 | TRACCC_HOST_DEVICE
|
43 | 71 | ~unique_lock();
|
44 | 72 |
|
45 |
| - /* |
46 |
| - * Lock the lock, blocking until the operation succeeds. |
| 73 | + /** |
| 74 | + * @brief Lock the lock, blocking until the operation succeeds. |
| 75 | + * |
| 76 | + * @warning On lockstep architectures, calling this method on more than a |
| 77 | + * single thread in a group will result in deadlock. |
47 | 78 | */
|
48 | 79 | TRACCC_HOST_DEVICE
|
49 | 80 | void lock();
|
50 | 81 |
|
51 |
| - /* |
52 |
| - * Try to lock the lock without blocking. |
| 82 | + /** |
| 83 | + * @brief Try to lock the lock without blocking. |
| 84 | + * |
| 85 | + * @note Calling this method from multiple threads in the same block is |
| 86 | + * safe. |
53 | 87 | */
|
54 | 88 | TRACCC_HOST_DEVICE
|
55 | 89 | bool try_lock();
|
56 | 90 |
|
57 |
| - /* |
58 |
| - * Explicitly lock the underlying lock. |
| 91 | + /** |
| 92 | + * @brief Explicitly unlock the underlying lock. |
| 93 | + * |
| 94 | + * @warning Calling this method on a lock which has not been acquired |
| 95 | + * constitutes undefined behaviour. |
59 | 96 | */
|
60 | 97 | TRACCC_HOST_DEVICE
|
61 | 98 | void unlock();
|
62 | 99 |
|
63 |
| - /* |
64 |
| - * Check if the lock is locked by this object. |
| 100 | + /** |
| 101 | + * @brief Check if the lock is locked by this object. |
65 | 102 | */
|
66 | 103 | TRACCC_HOST_DEVICE
|
67 | 104 | bool owns_lock() const;
|
|
0 commit comments