summaryrefslogtreecommitdiff
path: root/snmplib/mt_support.c
blob: d28ebe47792f022be6a22ee095e4e60bb0130334 (plain)
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
/*
 * mt_support.c - multi-thread resource locking support 
 */
/*
 * Author: Markku Laukkanen
 * Created: 6-Sep-1999
 * History:
 *  8-Sep-1999 M. Slifcak method names changed;
 *                        use array of resource locking structures.
 */

#include <net-snmp/net-snmp-config.h>
#include <errno.h>
#include <net-snmp/library/mt_support.h>

#ifdef __cplusplus
extern          "C" {
#endif

#ifdef NETSNMP_REENTRANT

static mutex_type s_res[MT_MAX_IDS][MT_LIB_MAXIMUM];  /* locking structures */

static mutex_type *
_mt_res(int groupID, int resourceID)
{
    if (groupID < 0) {
	return 0;
    }
    if (groupID >= MT_MAX_IDS) {
	return 0;
    }
    if (resourceID < 0) {
	return 0;
    }
    if (resourceID >= MT_LIB_MAXIMUM) {
	return 0;
    }
    return (&s_res[groupID][resourceID]);
}

static int
snmp_res_init_mutex(mutex_type *mutex)
{
    int rc = 0;
#if HAVE_PTHREAD_H
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    rc = pthread_mutex_init(mutex, &attr);
    pthread_mutexattr_destroy(&attr);
#elif defined(WIN32)
    InitializeCriticalSection(mutex);
#endif

    return rc;
}

int
snmp_res_init(void)
{
    int ii, jj, rc = 0;
    mutex_type *mutex;

    for (jj = 0; (0 == rc) && (jj < MT_MAX_IDS); jj++) {
	for (ii = 0; (0 == rc) && (ii < MT_LIB_MAXIMUM); ii++) {
	    mutex = _mt_res(jj, ii);
	    if (!mutex) {
		continue;
	    }
	    rc = snmp_res_init_mutex(mutex);
	}
    }

    return rc;
}

int
snmp_res_destroy_mutex(int groupID, int resourceID)
{
    int rc = 0;
    mutex_type *mutex = _mt_res(groupID, resourceID);
    if (!mutex) {
	return EFAULT;
    }

#if HAVE_PTHREAD_H
    rc = pthread_mutex_destroy(mutex);
#elif defined(WIN32)
    DeleteCriticalSection(mutex);
#endif

    return rc;
}

int
snmp_res_lock(int groupID, int resourceID)
{
    int rc = 0;
    mutex_type *mutex = _mt_res(groupID, resourceID);
    
    if (!mutex) {
	return EFAULT;
    }

#if HAVE_PTHREAD_H
    rc = pthread_mutex_lock(mutex);
#elif defined(WIN32)
    EnterCriticalSection(mutex);
#endif

    return rc;
}

int
snmp_res_unlock(int groupID, int resourceID)
{
    int rc = 0;
    mutex_type *mutex = _mt_res(groupID, resourceID);

    if (!mutex) {
	return EFAULT;
    }

#if HAVE_PTHREAD_H
    rc = pthread_mutex_unlock(mutex);
#elif defined(WIN32)
    LeaveCriticalSection(mutex);
#endif

    return rc;
}

#else  /*  NETSNMP_REENTRANT  */
#ifdef WIN32

/*
 * Provide "do nothing" targets for Release (.DLL) builds. 
 */

int
snmp_res_init(void)
{
    return 0;
}

int
snmp_res_lock(int groupID, int resourceID)
{
    return 0;
}

int
snmp_res_unlock(int groupID, int resourceID)
{
    return 0;
}

int
snmp_res_destroy_mutex(int groupID, int resourceID)
{
    return 0;
}
#endif /*  WIN32  */
#endif /*  NETSNMP_REENTRANT  */

#ifdef __cplusplus
}
#endif