1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_MUTEXS_H
21 #define ZABBIX_MUTEXS_H
22 
23 #ifdef _WINDOWS
24 #	define ZBX_MUTEX_NULL		NULL
25 
26 #	define ZBX_MUTEX_LOG		zbx_mutex_create_per_process_name(L"ZBX_MUTEX_LOG")
27 #	define ZBX_MUTEX_PERFSTAT	zbx_mutex_create_per_process_name(L"ZBX_MUTEX_PERFSTAT")
28 
29 typedef wchar_t * zbx_mutex_name_t;
30 typedef HANDLE zbx_mutex_t;
31 #else	/* not _WINDOWS */
32 typedef enum
33 {
34 	ZBX_MUTEX_LOG = 0,
35 	ZBX_MUTEX_CACHE,
36 	ZBX_MUTEX_TRENDS,
37 	ZBX_MUTEX_CACHE_IDS,
38 	ZBX_MUTEX_SELFMON,
39 	ZBX_MUTEX_CPUSTATS,
40 	ZBX_MUTEX_DISKSTATS,
41 	ZBX_MUTEX_ITSERVICES,
42 	ZBX_MUTEX_VALUECACHE,
43 	ZBX_MUTEX_VMWARE,
44 	ZBX_MUTEX_SQLITE3,
45 	ZBX_MUTEX_PROCSTAT,
46 	ZBX_MUTEX_PROXY_HISTORY,
47 #ifdef HAVE_VMINFO_T_UPDATES
48 	ZBX_MUTEX_KSTAT,
49 #endif
50 	ZBX_MUTEX_COUNT
51 }
52 zbx_mutex_name_t;
53 
54 typedef enum
55 {
56 	ZBX_RWLOCK_CONFIG = 0,
57 	ZBX_RWLOCK_COUNT,
58 }
59 zbx_rwlock_name_t;
60 
61 #ifdef HAVE_PTHREAD_PROCESS_SHARED
62 #	define ZBX_MUTEX_NULL			NULL
63 #	define ZBX_RWLOCK_NULL			NULL
64 
65 #	define zbx_rwlock_wrlock(rwlock)	__zbx_rwlock_wrlock(__FILE__, __LINE__, rwlock)
66 #	define zbx_rwlock_rdlock(rwlock)	__zbx_rwlock_rdlock(__FILE__, __LINE__, rwlock)
67 #	define zbx_rwlock_unlock(rwlock)	__zbx_rwlock_unlock(__FILE__, __LINE__, rwlock)
68 
69 typedef pthread_mutex_t * zbx_mutex_t;
70 typedef pthread_rwlock_t * zbx_rwlock_t;
71 
72 void	__zbx_rwlock_wrlock(const char *filename, int line, zbx_rwlock_t rwlock);
73 void	__zbx_rwlock_rdlock(const char *filename, int line, zbx_rwlock_t rwlock);
74 void	__zbx_rwlock_unlock(const char *filename, int line, zbx_rwlock_t rwlock);
75 void	zbx_rwlock_destroy(zbx_rwlock_t *rwlock);
76 void	zbx_locks_disable(void);
77 #else	/* fallback to semaphores if read-write locks are not available */
78 #	define ZBX_RWLOCK_NULL				-1
79 #	define ZBX_MUTEX_NULL				-1
80 
81 #	define zbx_rwlock_wrlock(rwlock)		__zbx_mutex_lock(__FILE__, __LINE__, rwlock)
82 #	define zbx_rwlock_rdlock(rwlock)		__zbx_mutex_lock(__FILE__, __LINE__, rwlock)
83 #	define zbx_rwlock_unlock(rwlock)		__zbx_mutex_unlock(__FILE__, __LINE__, rwlock)
84 #	define zbx_rwlock_destroy(rwlock)		zbx_mutex_destroy(rwlock)
85 
86 typedef int zbx_mutex_t;
87 typedef int zbx_rwlock_t;
88 #endif
89 int	zbx_locks_create(char **error);
90 int	zbx_rwlock_create(zbx_rwlock_t *rwlock, zbx_rwlock_name_t name, char **error);
91 #endif	/* _WINDOWS */
92 #	define zbx_mutex_lock(mutex)		__zbx_mutex_lock(__FILE__, __LINE__, mutex)
93 #	define zbx_mutex_unlock(mutex)		__zbx_mutex_unlock(__FILE__, __LINE__, mutex)
94 
95 int	zbx_mutex_create(zbx_mutex_t *mutex, zbx_mutex_name_t name, char **error);
96 void	__zbx_mutex_lock(const char *filename, int line, zbx_mutex_t mutex);
97 void	__zbx_mutex_unlock(const char *filename, int line, zbx_mutex_t mutex);
98 void	zbx_mutex_destroy(zbx_mutex_t *mutex);
99 
100 #ifdef _WINDOWS
101 zbx_mutex_name_t	zbx_mutex_create_per_process_name(const zbx_mutex_name_t prefix);
102 #endif
103 
104 #endif	/* ZABBIX_MUTEXS_H */
105