net-snmp 5.7
mt_support.c
00001 /*
00002  * mt_support.c - multi-thread resource locking support 
00003  */
00004 /*
00005  * Author: Markku Laukkanen
00006  * Created: 6-Sep-1999
00007  * History:
00008  *  8-Sep-1999 M. Slifcak method names changed;
00009  *                        use array of resource locking structures.
00010  */
00011 
00012 #include <net-snmp/net-snmp-config.h>
00013 #include <errno.h>
00014 #include <net-snmp/library/mt_support.h>
00015 
00016 #ifdef __cplusplus
00017 extern          "C" {
00018 #endif
00019 
00020 #ifdef NETSNMP_REENTRANT
00021 
00022 static mutex_type s_res[MT_MAX_IDS][MT_LIB_MAXIMUM];  /* locking structures */
00023 
00024 static mutex_type *
00025 _mt_res(int groupID, int resourceID)
00026 {
00027     if (groupID < 0) {
00028         return 0;
00029     }
00030     if (groupID >= MT_MAX_IDS) {
00031         return 0;
00032     }
00033     if (resourceID < 0) {
00034         return 0;
00035     }
00036     if (resourceID >= MT_LIB_MAXIMUM) {
00037         return 0;
00038     }
00039     return (&s_res[groupID][resourceID]);
00040 }
00041 
00042 static int
00043 snmp_res_init_mutex(mutex_type *mutex)
00044 {
00045     int rc = 0;
00046 #if HAVE_PTHREAD_H
00047     rc = pthread_mutex_init(mutex, MT_MUTEX_INIT_DEFAULT);
00048 #elif defined(WIN32)
00049     InitializeCriticalSection(mutex);
00050 #endif
00051 
00052     return rc;
00053 }
00054 
00055 int
00056 snmp_res_init(void)
00057 {
00058     int ii, jj, rc = 0;
00059     mutex_type *mutex;
00060 
00061     for (jj = 0; (0 == rc) && (jj < MT_MAX_IDS); jj++) {
00062         for (ii = 0; (0 == rc) && (ii < MT_LIB_MAXIMUM); ii++) {
00063             mutex = _mt_res(jj, ii);
00064             if (!mutex) {
00065                 continue;
00066             }
00067             rc = snmp_res_init_mutex(mutex);
00068         }
00069     }
00070 
00071     return rc;
00072 }
00073 
00074 int
00075 snmp_res_destroy_mutex(int groupID, int resourceID)
00076 {
00077     int rc = 0;
00078     mutex_type *mutex = _mt_res(groupID, resourceID);
00079     if (!mutex) {
00080         return EFAULT;
00081     }
00082 
00083 #if HAVE_PTHREAD_H
00084     rc = pthread_mutex_destroy(mutex);
00085 #elif defined(WIN32)
00086     DeleteCriticalSection(mutex);
00087 #endif
00088 
00089     return rc;
00090 }
00091 
00092 int
00093 snmp_res_lock(int groupID, int resourceID)
00094 {
00095     int rc = 0;
00096     mutex_type *mutex = _mt_res(groupID, resourceID);
00097     
00098     if (!mutex) {
00099         return EFAULT;
00100     }
00101 
00102 #if HAVE_PTHREAD_H
00103     rc = pthread_mutex_lock(mutex);
00104 #elif defined(WIN32)
00105     EnterCriticalSection(mutex);
00106 #endif
00107 
00108     return rc;
00109 }
00110 
00111 int
00112 snmp_res_unlock(int groupID, int resourceID)
00113 {
00114     int rc = 0;
00115     mutex_type *mutex = _mt_res(groupID, resourceID);
00116 
00117     if (!mutex) {
00118         return EFAULT;
00119     }
00120 
00121 #if HAVE_PTHREAD_H
00122     rc = pthread_mutex_unlock(mutex);
00123 #elif defined(WIN32)
00124     LeaveCriticalSection(mutex);
00125 #endif
00126 
00127     return rc;
00128 }
00129 
00130 #else  /*  NETSNMP_REENTRANT  */
00131 #ifdef WIN32
00132 
00133 /*
00134  * Provide "do nothing" targets for Release (.DLL) builds. 
00135  */
00136 
00137 int
00138 snmp_res_init(void)
00139 {
00140     return 0;
00141 }
00142 
00143 int
00144 snmp_res_lock(int groupID, int resourceID)
00145 {
00146     return 0;
00147 }
00148 
00149 int
00150 snmp_res_unlock(int groupID, int resourceID)
00151 {
00152     return 0;
00153 }
00154 
00155 int
00156 snmp_res_destroy_mutex(int groupID, int resourceID)
00157 {
00158     return 0;
00159 }
00160 #endif /*  WIN32  */
00161 #endif /*  NETSNMP_REENTRANT  */
00162 
00163 #ifdef __cplusplus
00164 }
00165 #endif