00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "config.h"
00021 #include "defines.h"
00022
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <pthread.h>
00026 #include <string.h>
00027 #include <ctype.h>
00028
00029 #include <sys/time.h>
00030
00031 #include "cf_pthread.h"
00032
00033 #include "hashlib.h"
00034 #include "readline.h"
00035 #include "utils.h"
00036 #include "fo_server.h"
00037 #include "serverlib.h"
00038
00039
00040 void cf_mutex_init(const u_char *name,t_cf_mutex *mutex) {
00041 mutex->name = strdup(name);
00042 pthread_mutex_init(&mutex->mutex,NULL);
00043 }
00044 void cf_mutex_destroy(t_cf_mutex *mutex) {
00045 free(mutex->name);
00046 pthread_mutex_destroy(&mutex->mutex);
00047 }
00048
00049 void cf_rwlock_init(const u_char *name,t_cf_rwlock *rwlock) {
00050 rwlock->name = strdup(name);
00051 pthread_rwlock_init(&rwlock->rwlock,NULL);
00052 }
00053 void cf_rwlock_destroy(t_cf_rwlock *rwlock) {
00054 free(rwlock->name);
00055 pthread_rwlock_destroy(&rwlock->rwlock);
00056 }
00057
00058 void cf_lock_mutex(const u_char *file,const int line,t_cf_mutex *mutex) {
00059 #ifdef _FO_LOCK_DEBUG
00060 struct timeval tv1,tv2;
00061 struct timezone tz;
00062
00063 unsigned long long diff;
00064 #endif
00065
00066 int status;
00067
00068 #ifdef _FO_LOCK_DEBUG
00069 if(gettimeofday(&tv1,&tz) == -1) return;
00070
00071 cf_log(LOG_ERR,file,line,"PTHREAD MUTEX TRY LOCK '%s'\n",mutex->name);
00072 #endif
00073
00074 if((status = pthread_mutex_lock(&mutex->mutex)) != 0) {
00075 cf_log(LOG_ERR,__FILE__,__LINE__,"pthread_mutex_lock: %s\n",strerror(status));
00076 exit(-1);
00077 }
00078
00079 #ifdef _FO_LOCK_DEBUG
00080 if(gettimeofday(&tv2,&tz) == -1) return;
00081
00082 diff = tv2.tv_sec * 1000 + tv2.tv_usec;
00083 diff -= tv1.tv_sec * 1000 + tv1.tv_usec;
00084
00085
00086 if(diff < 0) diff = 0;
00087
00088 cf_log(LOG_ERR,file,line,"PTHREAD MUTEX LOCKED '%s' %lld\n",mutex->name,diff);
00089 #endif
00090 }
00091
00092 void cf_unlock_mutex(const u_char *file,const int line,t_cf_mutex *mutex) {
00093 #ifdef _FO_LOCK_DEBUG
00094 struct timeval tv1,tv2;
00095 struct timezone tz;
00096 unsigned long long diff;
00097 #endif
00098
00099 int status;
00100
00101 #ifdef _FO_LOCK_DEBUG
00102 if(gettimeofday(&tv1,&tz) == -1) return;
00103
00104 cf_log(LOG_ERR,file,line,"PTHREAD MUTEX TRY UNLOCK '%s'\n",mutex->name);
00105 #endif
00106
00107 if((status = pthread_mutex_unlock(&mutex->mutex)) != 0) {
00108 cf_log(LOG_ERR,__FILE__,__LINE__,"pthread_mutex_unlock: %s\n",strerror(status));
00109 exit(-1);
00110 }
00111
00112 #ifdef _FO_LOCK_DEBUG
00113 if(gettimeofday(&tv2,&tz) == -1) return;
00114
00115 diff = tv2.tv_sec * 1000 + tv2.tv_usec;
00116 diff -= tv1.tv_sec * 1000 + tv1.tv_usec;
00117
00118
00119 if(diff < 0) diff = 0;
00120
00121 cf_log(LOG_ERR,file,line,"PTHREAD MUTEX UNLOCK '%s' %lld\n",mutex->name,diff);
00122 #endif
00123 }
00124
00125 void cf_rwlock_rdlock(const u_char *file,const int line,t_cf_rwlock *rwlock) {
00126 #ifdef _FO_LOCK_DEBUG
00127 struct timeval tv1,tv2;
00128 struct timezone tz;
00129 unsigned long long diff;
00130 #endif
00131
00132 int status;
00133
00134 #ifdef _FO_LOCK_DEBUG
00135 if(gettimeofday(&tv1,&tz) == -1) return;
00136
00137 cf_log(LOG_ERR,file,line,"PTHREAD RWLOCK TRY RDLOCK '%s'\n",rwlock->name);
00138 #endif
00139
00140 if((status = pthread_rwlock_rdlock(&rwlock->rwlock)) != 0) {
00141 cf_log(LOG_ERR,__FILE__,__LINE__,"pthread_rwlock_rdlock: %s\n",strerror(status));
00142 exit(-1);
00143 }
00144
00145 #ifdef _FO_LOCK_DEBUG
00146 if(gettimeofday(&tv2,&tz) == -1) return;
00147
00148 diff = tv2.tv_sec * 1000 + tv2.tv_usec;
00149 diff -= tv1.tv_sec * 1000 + tv1.tv_usec;
00150
00151
00152 if(diff < 0) diff = 0;
00153
00154 cf_log(LOG_ERR,file,line,"PTHREAD RWLOCK RDLOCKED '%s' %lld\n",rwlock->name,diff);
00155 #endif
00156 }
00157
00158 void cf_rwlock_wrlock(const u_char *file,const int line,t_cf_rwlock *rwlock) {
00159 #ifdef _FO_LOCK_DEBUG
00160 struct timeval tv1,tv2;
00161 struct timezone tz;
00162 unsigned long long diff;
00163 #endif
00164
00165 int status;
00166
00167 #ifdef _FO_LOCK_DEBUG
00168 if(gettimeofday(&tv1,&tz) == -1) return;
00169
00170 cf_log(LOG_ERR,file,line,"PTHREAD RWLOCK TRY WRLOCK '%s'\n",rwlock->name);
00171 #endif
00172
00173 if((status = pthread_rwlock_wrlock(&rwlock->rwlock)) != 0) {
00174 cf_log(LOG_ERR,__FILE__,__LINE__,"pthread_rwlock_wrlock: %s\n",strerror(status));
00175 exit(-1);
00176 }
00177
00178 #ifdef _FO_LOCK_DEBUG
00179 if(gettimeofday(&tv2,&tz) == -1) return;
00180
00181 diff = tv2.tv_sec * 1000 + tv2.tv_usec;
00182 diff -= tv1.tv_sec * 1000 + tv1.tv_usec;
00183
00184
00185 if(diff < 0) diff = 0;
00186
00187 cf_log(LOG_ERR,file,line,"PTHREAD RWLOCK WRLOCKED '%s' %lld\n",rwlock->name,diff);
00188 #endif
00189 }
00190
00191 void cf_rwlock_unlock(const u_char *file,const int line,t_cf_rwlock *rwlock) {
00192 #ifdef _FO_LOCK_DEBUG
00193 struct timeval tv1,tv2;
00194 struct timezone tz;
00195 unsigned long long diff;
00196 #endif
00197
00198 int status;
00199
00200 #ifdef _FO_LOCK_DEBUG
00201 if(gettimeofday(&tv1,&tz) == -1) return;
00202
00203 cf_log(LOG_ERR,file,line,"PTHREAD RWLOCK TRY UNLOCK '%s'\n",rwlock->name);
00204 #endif
00205
00206 if((status = pthread_rwlock_unlock(&rwlock->rwlock)) != 0) {
00207 cf_log(LOG_ERR,__FILE__,__LINE__,"pthread_rwlock_unlock: %s\n",strerror(status));
00208 exit(-1);
00209 }
00210
00211 #ifdef _FO_LOCK_DEBUG
00212 if(gettimeofday(&tv2,&tz) == -1) return;
00213
00214 diff = tv2.tv_sec * 1000 + tv2.tv_usec;
00215 diff -= tv1.tv_sec * 1000 + tv1.tv_usec;
00216
00217
00218 if(diff < 0) diff = 0;
00219
00220 cf_log(LOG_ERR,file,line,"PTHREAD RWLOCK UNLOCK '%s' %lld\n",rwlock->name,diff);
00221 #endif
00222 }
00223
00224