2 * Copyright (C) 2013-2020 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #ifndef NBDKIT_CLEANUP_H
34 #define NBDKIT_CLEANUP_H
43 #include "unique-name.h"
45 /* Work around clang bug: https://bugs.llvm.org/show_bug.cgi?id=43482 */
47 #define CLANG_UNUSED_VARIABLE_WORKAROUND __attribute__((__unused__))
49 #define CLANG_UNUSED_VARIABLE_WORKAROUND
53 extern void cleanup_free (void *ptr
);
54 #define CLEANUP_FREE __attribute__((cleanup (cleanup_free)))
56 extern void cleanup_mutex_unlock (pthread_mutex_t
**ptr
);
57 #define CLEANUP_MUTEX_UNLOCK __attribute__((cleanup (cleanup_mutex_unlock)))
59 #define ACQUIRE_LOCK_FOR_CURRENT_SCOPE(mutex) \
60 ACQUIRE_LOCK_FOR_CURRENT_SCOPE_1((mutex), NBDKIT_UNIQUE_NAME(_lock))
61 #define ACQUIRE_LOCK_FOR_CURRENT_SCOPE_1(mutex, lock) \
62 CLEANUP_MUTEX_UNLOCK pthread_mutex_t *lock = mutex; \
64 int _r = pthread_mutex_lock (lock); \
68 extern void cleanup_rwlock_unlock (pthread_rwlock_t
**ptr
);
69 #define CLEANUP_RWLOCK_UNLOCK __attribute__((cleanup (cleanup_rwlock_unlock)))
71 #define ACQUIRE_WRLOCK_FOR_CURRENT_SCOPE(rwlock) \
72 ACQUIRE_WRLOCK_FOR_CURRENT_SCOPE_1((rwlock), NBDKIT_UNIQUE_NAME(_lock))
73 #define ACQUIRE_WRLOCK_FOR_CURRENT_SCOPE_1(rwlock, lock) \
74 CLEANUP_RWLOCK_UNLOCK pthread_rwlock_t *lock = rwlock; \
76 int _r = pthread_rwlock_wrlock (lock); \
80 #define ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE(rwlock) \
81 ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE_1((rwlock), NBDKIT_UNIQUE_NAME(_lock))
82 #define ACQUIRE_RDLOCK_FOR_CURRENT_SCOPE_1(rwlock, lock) \
83 CLEANUP_RWLOCK_UNLOCK pthread_rwlock_t *lock = rwlock; \
85 int _r = pthread_rwlock_rdlock (lock); \
89 /* cleanup-nbdkit.c */
90 struct nbdkit_extents
;
91 extern void cleanup_extents_free (struct nbdkit_extents
**ptr
);
92 #define CLEANUP_EXTENTS_FREE __attribute__((cleanup (cleanup_extents_free)))
93 struct nbdkit_exports
;
94 extern void cleanup_exports_free (struct nbdkit_exports
**ptr
);
95 #define CLEANUP_EXPORTS_FREE __attribute__((cleanup (cleanup_exports_free)))
101 #endif /* NBDKIT_CLEANUP_H */