File: locks.cpp

package info (click to toggle)
netmrg 0.20-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,856 kB
  • ctags: 2,009
  • sloc: php: 9,401; sh: 4,644; cpp: 2,898; perl: 621; ansic: 381; makefile: 331; xml: 92; sql: 71; sed: 16
file content (88 lines) | stat: -rw-r--r-- 2,211 bytes parent folder | download | duplicates (6)
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
/********************************************
* NetMRG Integrator
*
* locks.cpp
* NetMRG Gatherer Locks
*
* see doc/LICENSE for copyright information
********************************************/

#include "locks.h"
#include "utils.h"

// Create mutex locks
static pthread_mutex_t active_threads_lock	= PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mysql_lock			= PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t snmp_lock			= PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t rrdtool_lock			= PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t settings_lock		= PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t pipe_lock			= PTHREAD_MUTEX_INITIALIZER;

// Create conditional variables
static pthread_cond_t active_threads_cv		= PTHREAD_COND_INITIALIZER;

pthread_mutex_t* get_lock(Lock myLock)
{
	switch (myLock)
	{
		case lkActiveThreads:	return &active_threads_lock;
		case lkMySQL:			return &mysql_lock;
		case lkSNMP:			return &snmp_lock;
		case lkRRD:				return &rrdtool_lock;
		case lkSettings:		return &settings_lock;
		case lkPipe:			return &pipe_lock;
	}
	return NULL;
}

pthread_cond_t* get_cond(Cond myCond)
{
	switch (myCond)
	{
		case cActiveThreads:	return &active_threads_cv;
	}
	return NULL;
}

string get_lock_name(Lock myLock)
{
	switch (myLock)
	{
		case lkActiveThreads:	return "ActiveThreads";
		case lkMySQL:			return "MySQL";
		case lkSNMP:			return "SNMP";
		case lkRRD:				return "RRD";
		case lkSettings:		return "Settings";
		case lkPipe:			return "Pipe";
	}
	return "";
}

void netmrg_mutex_lock(Lock myLock)
{
	debuglogger(DEBUG_THREAD, LEVEL_DEBUG, NULL, "Locking " + get_lock_name(myLock));
	pthread_mutex_lock(get_lock(myLock));
}

void netmrg_mutex_unlock(Lock myLock)
{
	debuglogger(DEBUG_THREAD, LEVEL_DEBUG, NULL, "Unlocking " + get_lock_name(myLock));
	pthread_mutex_unlock(get_lock(myLock));
}

int	netmrg_mutex_trylock(Lock myLock)
{
	debuglogger(DEBUG_THREAD, LEVEL_DEBUG, NULL, "Trying to lock " + get_lock_name(myLock));
	return pthread_mutex_trylock(get_lock(myLock));
}

void netmrg_cond_signal(Cond myCond)
{
	pthread_cond_signal(get_cond(myCond));
}

void netmrg_cond_wait(Cond myCond, Lock myLock)
{
	pthread_cond_wait(get_cond(myCond), get_lock(myLock));
}