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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
#ifndef ZABBIX_MUTEXS_H
#define ZABBIX_MUTEXS_H
#include "zbxcommon.h"
#include "zbxprof.h"
#ifdef _WINDOWS
# define ZBX_MUTEX_NULL NULL
# define ZBX_MUTEX_LOG zbx_mutex_create_per_process_name(L"ZBX_MUTEX_LOG")
# define ZBX_MUTEX_PERFSTAT zbx_mutex_create_per_process_name(L"ZBX_MUTEX_PERFSTAT")
typedef wchar_t * zbx_mutex_name_t;
typedef HANDLE zbx_mutex_t;
# define zbx_mutex_lock(mutex) __zbx_mutex_lock(__FILE__, __LINE__, mutex)
# define zbx_mutex_unlock(mutex) __zbx_mutex_unlock(__FILE__, __LINE__, mutex)
#else /* not _WINDOWS */
typedef enum
{
ZBX_MUTEX_LOG = 0,
ZBX_MUTEX_CACHE,
ZBX_MUTEX_TRENDS,
ZBX_MUTEX_CACHE_IDS,
ZBX_MUTEX_SELFMON,
ZBX_MUTEX_CPUSTATS,
ZBX_MUTEX_DISKSTATS,
ZBX_MUTEX_VALUECACHE,
ZBX_MUTEX_VMWARE,
ZBX_MUTEX_SQLITE3,
ZBX_MUTEX_PROCSTAT,
ZBX_MUTEX_PROXY_HISTORY,
#ifdef HAVE_VMINFO_T_UPDATES
ZBX_MUTEX_KSTAT,
#endif
ZBX_MUTEX_MODBUS,
ZBX_MUTEX_TREND_FUNC,
ZBX_MUTEX_REMOTE_COMMANDS,
ZBX_MUTEX_PROXY_BUFFER,
ZBX_MUTEX_VPS_MONITOR,
/* NOTE: Do not forget to sync changes here with mutex names in diag_add_locks_info()! */
ZBX_MUTEX_COUNT
}
zbx_mutex_name_t;
typedef enum
{
ZBX_RWLOCK_CONFIG = 0,
ZBX_RWLOCK_CONFIG_HISTORY,
ZBX_RWLOCK_VALUECACHE,
ZBX_RWLOCK_COUNT,
}
zbx_rwlock_name_t;
#ifdef HAVE_PTHREAD_PROCESS_SHARED
# define ZBX_MUTEX_NULL NULL
# define ZBX_RWLOCK_NULL NULL
# define zbx_rwlock_wrlock(rwlock) \
\
do \
{ \
zbx_prof_start(__func__, ZBX_PROF_RWLOCK); \
__zbx_rwlock_wrlock(__FILE__, __LINE__, rwlock); \
zbx_prof_end_wait(); \
} \
while (0)
# define zbx_rwlock_rdlock(rwlock) \
\
do \
{ \
zbx_prof_start(__func__, ZBX_PROF_RWLOCK); \
__zbx_rwlock_rdlock(__FILE__, __LINE__, rwlock); \
zbx_prof_end_wait(); \
} \
while(0)
# define zbx_rwlock_unlock(rwlock) \
\
do \
{ \
__zbx_rwlock_unlock(__FILE__, __LINE__, rwlock); \
zbx_prof_end(); \
} \
while(0)
typedef pthread_mutex_t * zbx_mutex_t;
typedef pthread_rwlock_t * zbx_rwlock_t;
void __zbx_rwlock_wrlock(const char *filename, int line, zbx_rwlock_t rwlock);
void __zbx_rwlock_rdlock(const char *filename, int line, zbx_rwlock_t rwlock);
void __zbx_rwlock_unlock(const char *filename, int line, zbx_rwlock_t rwlock);
void zbx_rwlock_destroy(zbx_rwlock_t *rwlock);
void zbx_locks_disable(void);
void zbx_locks_enable(void);
#else /* fallback to semaphores if read-write locks are not available */
# define ZBX_RWLOCK_NULL -1
# define ZBX_MUTEX_NULL -1
# define zbx_rwlock_wrlock(mutex) \
\
do \
{ \
zbx_prof_start(__func__, ZBX_PROF_MUTEX); \
__zbx_mutex_lock(__FILE__, __LINE__, mutex); \
zbx_prof_end_wait(); \
} \
while(0)
# define zbx_rwlock_rdlock(mutex) \
\
do \
{ \
zbx_prof_start(__func__, ZBX_PROF_MUTEX); \
__zbx_mutex_lock(__FILE__, __LINE__, mutex); \
zbx_prof_end_wait(); \
} \
while(0)
# define zbx_rwlock_unlock(mutex) \
\
do \
{ \
__zbx_mutex_unlock(__FILE__, __LINE__, mutex); \
zbx_prof_end(); \
} \
while(0)
# define zbx_rwlock_destroy(rwlock) zbx_mutex_destroy(rwlock)
typedef int zbx_mutex_t;
typedef int zbx_rwlock_t;
#endif
int zbx_locks_create(char **error);
void zbx_locks_destroy(void);
int zbx_rwlock_create(zbx_rwlock_t *rwlock, zbx_rwlock_name_t name, char **error);
zbx_mutex_t zbx_mutex_addr_get(zbx_mutex_name_t mutex_name);
zbx_rwlock_t zbx_rwlock_addr_get(zbx_rwlock_name_t rwlock_name);
# define zbx_mutex_lock(mutex) \
\
do \
{ \
zbx_prof_start(__func__, ZBX_PROF_MUTEX); \
__zbx_mutex_lock(__FILE__, __LINE__, mutex); \
zbx_prof_end_wait(); \
} \
while(0)
# define zbx_mutex_unlock(mutex) \
\
do \
{ \
__zbx_mutex_unlock(__FILE__, __LINE__, mutex); \
zbx_prof_end(); \
} \
while(0)
#endif /* _WINDOWS */
int zbx_mutex_create(zbx_mutex_t *mutex, zbx_mutex_name_t name, char **error);
void __zbx_mutex_lock(const char *filename, int line, zbx_mutex_t mutex);
void __zbx_mutex_unlock(const char *filename, int line, zbx_mutex_t mutex);
void zbx_mutex_destroy(zbx_mutex_t *mutex);
#ifdef _WINDOWS
zbx_mutex_name_t zbx_mutex_create_per_process_name(const zbx_mutex_name_t prefix);
#endif
#endif /* ZABBIX_MUTEXS_H */
|