CNDP 22.08.0
Loading...
Searching...
No Matches
cne_mutex_helper.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2022 Intel Corporation
3 */
4
5#ifndef __CNE_MUTEX_HELPER_H
6#define __CNE_MUTEX_HELPER_H
7
12
13#include <pthread.h>
14#include <cne_log.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
30static inline int
31cne_mutex_create(pthread_mutex_t *mutex, int flags)
32{
33 pthread_mutexattr_t attr;
34 int inited = 0, ret = EFAULT;
35
36 if (!mutex)
37 goto err;
38
39#define __do(_exp) \
40 do { \
41 ret = _exp; \
42 if (ret) \
43 goto err; \
44 } while (0 /* CONSTCOND */)
45
46 __do(pthread_mutexattr_init(&attr));
47 inited = 1;
48
49 __do(pthread_mutexattr_settype(&attr, flags));
50
51 __do(pthread_mutex_init(mutex, &attr));
52
53 __do(pthread_mutexattr_destroy(&attr));
54
55#undef __do
56
57 return 0;
58err:
59 if (inited) {
60 /* Do not lose the previous error value */
61 if (pthread_mutexattr_destroy(&attr))
62 CNE_DEBUG("unable to destroy mutex attribute, but is not the root cause\n");
63 }
64
65 errno = ret;
66 return -1;
67}
68
77static inline int
78cne_mutex_destroy(pthread_mutex_t *mutex)
79{
80 int ret = 0;
81
82 if (mutex)
83 ret = pthread_mutex_destroy(mutex);
84
85 errno = ret;
86 return (ret != 0) ? -1 : 0;
87}
88
89#ifdef __cplusplus
90}
91#endif
92
93#endif /* __CNE_MUTEX_HELPER_H */
static int cne_mutex_create(pthread_mutex_t *mutex, int flags)
static int cne_mutex_destroy(pthread_mutex_t *mutex)