|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef CELIX_UV_CLEANUP_H |
| 21 | +#define CELIX_UV_CLEANUP_H |
| 22 | + |
| 23 | +#include <uv.h> |
| 24 | + |
| 25 | +#include "celix_cleanup.h" |
| 26 | + |
| 27 | +#ifdef __cplusplus |
| 28 | +extern "C" { |
| 29 | +#endif |
| 30 | + |
| 31 | +CELIX_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(uv_thread_t, uv_thread_join) |
| 32 | +CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(uv_mutex_t, uv_mutex_destroy) |
| 33 | +CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(uv_rwlock_t, uv_rwlock_destroy) |
| 34 | +CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(uv_cond_t, uv_cond_destroy) |
| 35 | +CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(uv_key_t, uv_key_delete) |
| 36 | + |
| 37 | +/** |
| 38 | + * @brief Lock guard for uv mutexes. |
| 39 | + */ |
| 40 | +typedef struct celix_uv_mutex_lock_guard { |
| 41 | + uv_mutex_t* mutex; |
| 42 | +} celix_uv_mutex_lock_guard_t; |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Initialize a lock guard for @a mutex. |
| 46 | + * |
| 47 | + * Lock a mutex and return a celix_uv_mutex_lock_guard_t. |
| 48 | + * Unlock with celixUvMutexLockGuard_deinit(). Using uv_mutex_lock() on a mutex |
| 49 | + * while a celix_uv_mutex_lock_guard_t exists can lead to undefined behaviour. |
| 50 | + * |
| 51 | + * No allocation is performed, it is equivalent to a uv_mutex_lock() call. |
| 52 | + * This is intended to be used with celix_auto(). |
| 53 | + * |
| 54 | + * @param mutex A mutex to lock. |
| 55 | + * @return An initialized lock guard to be used with celix_auto(). |
| 56 | + */ |
| 57 | +static CELIX_UNUSED inline celix_uv_mutex_lock_guard_t celixUvMutexLockGuard_init(uv_mutex_t* mutex) { |
| 58 | + celix_uv_mutex_lock_guard_t guard; |
| 59 | + guard.mutex = mutex; |
| 60 | + uv_mutex_lock(mutex); |
| 61 | + return guard; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief Deinitialize a lock guard for a mutex. |
| 66 | + * |
| 67 | + * Unlock the mutex of a guard. |
| 68 | + * No memory is freed, it is equivalent to a uv_mutex_unlock() call. |
| 69 | + * |
| 70 | + * @param guard A celix_uv_mutex_lock_guard_t. |
| 71 | + */ |
| 72 | +static CELIX_UNUSED inline void celixUvMutexLockGuard_deinit(celix_uv_mutex_lock_guard_t* guard) { |
| 73 | + if (guard->mutex) { |
| 74 | + uv_mutex_unlock(guard->mutex); |
| 75 | + guard->mutex = NULL; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +CELIX_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(celix_uv_mutex_lock_guard_t, celixUvMutexLockGuard_deinit) |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief A RAII style write lock guard for uv_rwlock_t. |
| 83 | + * |
| 84 | + * The lock is obtained in the constructor and released in the destructor. |
| 85 | + * This is intended to be used with celix_auto(). |
| 86 | + */ |
| 87 | +typedef struct celix_uv_rwlock_wlock_guard { |
| 88 | + uv_rwlock_t* lock; |
| 89 | +} celix_uv_rwlock_wlock_guard_t; |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Initialize a write lock guard for @a lock. |
| 93 | + * |
| 94 | + * Obtain a write lock on @a lock and return a celix_uv_rwlock_wlock_guard_t. |
| 95 | + * Unlock with celixUvRwlockWlockGuard_deinit(). Using uv_rwlock_wrunlock() |
| 96 | + * on @lock while a celix_uv_rwlock_wlock_guard_t exists can lead to undefined behaviour. |
| 97 | + * |
| 98 | + * No allocation is performed, it is equivalent to a uv_rwlock_wrlock() call. |
| 99 | + * This is intended to be used with celix_auto(). |
| 100 | + * |
| 101 | + * @param lock A read-write lock to lock. |
| 102 | + * @return An initialized write lock guard to be used with celix_auto(). |
| 103 | + */ |
| 104 | +static CELIX_UNUSED inline celix_uv_rwlock_wlock_guard_t celixUvRwlockWlockGuard_init(uv_rwlock_t* lock) { |
| 105 | + celix_uv_rwlock_wlock_guard_t guard; |
| 106 | + guard.lock = lock; |
| 107 | + uv_rwlock_wrlock(lock); |
| 108 | + return guard; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief Deinitialize a write lock guard. |
| 113 | + * |
| 114 | + * Release a write lock on the read-write lock contained in @a guard. |
| 115 | + * See celixUvRwlockWlockGuard_init() for details. |
| 116 | + * No memory is freed, it is equivalent to a uv_rwlock_wrunlock() call. |
| 117 | + * |
| 118 | + * @param guard A celix_uv_rwlock_wlock_guard_t. |
| 119 | + */ |
| 120 | +static CELIX_UNUSED inline void celixUvRwlockWlockGuard_deinit(celix_uv_rwlock_wlock_guard_t* guard) { |
| 121 | + if (guard->lock) { |
| 122 | + uv_rwlock_wrunlock(guard->lock); |
| 123 | + guard->lock = NULL; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +CELIX_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(celix_uv_rwlock_wlock_guard_t, celixUvRwlockWlockGuard_deinit) |
| 128 | + |
| 129 | +/** |
| 130 | + * @brief A RAII style read lock guard for uv_rwlock_t. |
| 131 | + * |
| 132 | + * The lock is obtained in the constructor and released in the destructor. |
| 133 | + * This is intended to be used with celix_auto(). |
| 134 | + */ |
| 135 | +typedef struct celix_uv_rwlock_rlock_guard { |
| 136 | + uv_rwlock_t* lock; |
| 137 | +} celix_uv_rwlock_rlock_guard_t; |
| 138 | + |
| 139 | +/** |
| 140 | + * @brief Initialize a read lock guard for a lock. |
| 141 | + * |
| 142 | + * Obtain a read lock on a lock and return a celix_uv_rwlock_rlock_guard_t. |
| 143 | + * Unlock with celixUvRwlockRlockGuard_deinit(). Using uv_rwlock_rdunlock() |
| 144 | + * on @lock while a celix_uv_rwlock_rlock_guard_t exists can lead to undefined behaviour. |
| 145 | + * |
| 146 | + * No allocation is performed, it is equivalent to a uv_rwlock_rdlock() call. |
| 147 | + * This is intended to be used with celix_auto(). |
| 148 | + * |
| 149 | + * @param lock A read-write lock to lock. |
| 150 | + * @return A guard to be used with celix_auto(). |
| 151 | + */ |
| 152 | +static CELIX_UNUSED inline celix_uv_rwlock_rlock_guard_t celixUvRwlockRlockGuard_init(uv_rwlock_t* lock) { |
| 153 | + celix_uv_rwlock_rlock_guard_t guard; |
| 154 | + guard.lock = lock; |
| 155 | + uv_rwlock_rdlock(lock); |
| 156 | + return guard; |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * @brief Deinitialize a read lock guard. |
| 161 | + * |
| 162 | + * Release a read lock on the read-write lock contained in a guard. |
| 163 | + * See celixUvRwlockRlockGuard_init() for details. |
| 164 | + * No memory is freed, it is equivalent to a uv_rwlock_rdunlock() call. |
| 165 | + * |
| 166 | + * @param guard A celix_uv_rwlock_rlock_guard_t. |
| 167 | + */ |
| 168 | +static CELIX_UNUSED inline void celixUvRwlockRlockGuard_deinit(celix_uv_rwlock_rlock_guard_t* guard) { |
| 169 | + if (guard->lock) { |
| 170 | + uv_rwlock_rdunlock(guard->lock); |
| 171 | + guard->lock = NULL; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +CELIX_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(celix_uv_rwlock_rlock_guard_t, celixUvRwlockRlockGuard_deinit) |
| 176 | + |
| 177 | +#ifdef __cplusplus |
| 178 | +} |
| 179 | +#endif |
| 180 | + |
| 181 | +#endif /* CELIX_UV_CLEANUP_H */ |
0 commit comments