1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-24 05:06:27 +08:00

fix: mutex

This commit is contained in:
源文雨
2022-04-10 15:14:32 +08:00
parent 5084f796a9
commit 465afde645
3 changed files with 46 additions and 45 deletions

40
dict.c
View File

@@ -7,7 +7,6 @@
#include "dict.h" #include "dict.h"
#include "server.h" #include "server.h"
static pthread_rwlock_t mu;
static char* filepath; static char* filepath;
static uint8_t dict_md5[16]; static uint8_t dict_md5[16];
@@ -21,8 +20,8 @@ static FILE* thread_fp[THREADCNT];
#define _dict_md5_4 ((uint32_t*)&dict_md5) #define _dict_md5_4 ((uint32_t*)&dict_md5)
#endif #endif
int fill_md5() { int fill_md5(pthread_rwlock_t* mu) {
size_t size = get_dict_size(); size_t size = get_dict_size(mu);
if(!size) { if(!size) {
memset(dict_md5, 0, 16); memset(dict_md5, 0, 16);
puts("Dict is empty, use all zero md5"); puts("Dict is empty, use all zero md5");
@@ -30,15 +29,15 @@ int fill_md5() {
} }
uint8_t* dict_buff = (uint8_t*)malloc(size); uint8_t* dict_buff = (uint8_t*)malloc(size);
if(dict_buff) { if(dict_buff) {
pthread_rwlock_rdlock(&mu); pthread_rwlock_rdlock(mu);
rewind(fp_read); rewind(fp_read);
if(fread(dict_buff, size, 1, fp_read) == 1) { if(fread(dict_buff, size, 1, fp_read) == 1) {
pthread_rwlock_unlock(&mu); pthread_rwlock_unlock(mu);
md5(dict_buff, size, dict_md5); md5(dict_buff, size, dict_md5);
free(dict_buff); free(dict_buff);
return 1; return 1;
} else { } else {
pthread_rwlock_unlock(&mu); pthread_rwlock_unlock(mu);
free(dict_buff); free(dict_buff);
puts("Read dict error"); puts("Read dict error");
return 0; return 0;
@@ -49,26 +48,25 @@ int fill_md5() {
} }
} }
int init_dict(char* file_path) { int init_dict(char* file_path, pthread_rwlock_t* mu) {
fp = fopen(file_path, "rb+"); fp = fopen(file_path, "rb+");
fp_read = fopen(file_path, "rb"); fp_read = fopen(file_path, "rb");
if(fp) { if(fp) {
int err = pthread_rwlock_init(&mu, NULL); int err = pthread_rwlock_init(mu, NULL);
if(err) { if(err) {
puts("Init lock error"); puts("Init lock error");
return 0; return 0;
} }
filepath = file_path; filepath = file_path;
return fill_md5(); return fill_md5(mu);
} else {
puts("Open dict error");
return 0;
} }
puts("Open dict error");
return 0;
} }
FILE* open_dict(uint8_t lock_type, uint32_t index) { FILE* open_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu) {
if(lock_type & DICT_LOCK_EX) { if(lock_type & DICT_LOCK_EX) {
pthread_rwlock_wrlock(&mu); pthread_rwlock_wrlock(mu);
if(!fp) fp = fopen(filepath, "rb+"); if(!fp) fp = fopen(filepath, "rb+");
else rewind(fp); else rewind(fp);
return fp; return fp;
@@ -77,7 +75,7 @@ FILE* open_dict(uint8_t lock_type, uint32_t index) {
puts("Open dict: Index out of bounds"); puts("Open dict: Index out of bounds");
return NULL; return NULL;
} }
pthread_rwlock_rdlock(&mu); pthread_rwlock_rdlock(mu);
if(!thread_fp[index]) thread_fp[index] = fopen(filepath, "rb"); if(!thread_fp[index]) thread_fp[index] = fopen(filepath, "rb");
else rewind(thread_fp[index]); else rewind(thread_fp[index]);
return thread_fp[index]; return thread_fp[index];
@@ -92,20 +90,20 @@ FILE* get_dict_fp_rd() {
return fp_read; return fp_read;
} }
void close_dict(uint8_t lock_type, uint32_t index) { void close_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu) {
if(lock_type & DICT_LOCK_EX) fflush(fp); if(lock_type & DICT_LOCK_EX) fflush(fp);
pthread_rwlock_unlock(&mu); pthread_rwlock_unlock(mu);
puts("Close dict"); puts("Close dict");
} }
off_t get_dict_size() { off_t get_dict_size(pthread_rwlock_t* mu) {
struct stat statbuf; struct stat statbuf;
pthread_rwlock_rdlock(&mu); pthread_rwlock_rdlock(mu);
if(stat(filepath, &statbuf)==0) { if(stat(filepath, &statbuf)==0) {
pthread_rwlock_unlock(&mu); pthread_rwlock_unlock(mu);
return statbuf.st_size; return statbuf.st_size;
} }
pthread_rwlock_unlock(&mu); pthread_rwlock_unlock(mu);
return -1; return -1;
} }

14
dict.h
View File

@@ -16,13 +16,13 @@ typedef struct DICT DICT;
#define DICT_LOCK_SH 0x01 #define DICT_LOCK_SH 0x01
#define DICT_LOCK_EX 0x02 #define DICT_LOCK_EX 0x02
int init_dict(char* file_path); void close_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu);
void close_dict(uint8_t lock_type, uint32_t index); int fill_md5(pthread_rwlock_t* mu);
int fill_md5(); int init_dict(char* file_path, pthread_rwlock_t* mu);
FILE* get_dict_fp_wr(); int is_md5_equal(uint8_t* digest);
off_t get_dict_size();
FILE* get_dict_fp_rd(); FILE* get_dict_fp_rd();
int is_md5_equal(uint8_t* digest); FILE* get_dict_fp_wr();
FILE *open_dict(uint8_t lock_type, uint32_t index); off_t get_dict_size(pthread_rwlock_t* mu);
FILE* open_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu);
#endif #endif

View File

@@ -49,6 +49,7 @@ static DICT* setdict;
static uint32_t* items_len; static uint32_t* items_len;
static CONFIG* cfg; static CONFIG* cfg;
static pthread_attr_t attr; static pthread_attr_t attr;
static pthread_rwlock_t mu;
#define DICTPOOLSZ (((uint32_t)-1)>>((sizeof(uint32_t)*8-DICTPOOLBIT))) #define DICTPOOLSZ (((uint32_t)-1)>>((sizeof(uint32_t)*8-DICTPOOLBIT)))
static DICT* dict_pool[DICTPOOLSZ+1]; static DICT* dict_pool[DICTPOOLSZ+1];
@@ -139,10 +140,11 @@ static int send_data(int accept_fd, int index, char *data, size_t length) {
static int send_all(THREADTIMER *timer) { static int send_all(THREADTIMER *timer) {
int re = 1; int re = 1;
FILE *fp = open_dict(DICT_LOCK_SH, timer->index); pthread_cleanup_push((void*)&pthread_rwlock_unlock, (void*)&mu);
FILE *fp = open_dict(DICT_LOCK_SH, timer->index, &mu);
if(!fp) return 1; if(!fp) return 1;
timer->lock_type = DICT_LOCK_SH; timer->lock_type = DICT_LOCK_SH;
off_t len = 0, file_size = get_dict_size(); off_t len = 0, file_size = get_dict_size(&mu);
char* buf = (char*)malloc(file_size); char* buf = (char*)malloc(file_size);
if(buf) { if(buf) {
if(fread(buf, file_size, 1, fp) == 1) { if(fread(buf, file_size, 1, fp) == 1) {
@@ -163,7 +165,8 @@ static int send_all(THREADTIMER *timer) {
} }
free(buf); free(buf);
} }
close_dict(DICT_LOCK_SH, timer->index); close_dict(DICT_LOCK_SH, timer->index, &mu);
pthread_cleanup_pop(0);
return re; return re;
} }
@@ -208,15 +211,15 @@ static void init_dict_pool(FILE *fp) {
static int s1_get(THREADTIMER *timer) { static int s1_get(THREADTIMER *timer) {
uint8_t digest[16]; uint8_t digest[16];
FILE *fp = open_dict(DICT_LOCK_SH, timer->index); FILE *fp = open_dict(DICT_LOCK_SH, timer->index, &mu);
//timer->status = 0; //timer->status = 0;
if(fp) { while(fp) {
int ch; int ch;
timer->lock_type = DICT_LOCK_SH; timer->lock_type = DICT_LOCK_SH;
md5((uint8_t*)timer->dat, strlen(timer->dat)+1, digest); md5((uint8_t*)timer->dat, strlen(timer->dat)+1, digest);
uint8_t* dp = digest; uint8_t* dp = digest;
int p = ((*((uint32_t*)digest))>>(8*sizeof(uint32_t)-DICTPOOLBIT))&DICTPOOLSZ; int p = ((*((uint32_t*)digest))>>(8*sizeof(uint32_t)-DICTPOOLBIT))&DICTPOOLSZ;
if(!dict_pool[p]) return close_and_send(timer, "null", 4); if(!dict_pool[p]) break;
int c = 16-4; int c = 16-4;
int notok = 1; int notok = 1;
@@ -235,6 +238,8 @@ static int s1_get(THREADTIMER *timer) {
return r; return r;
} else free(spb); } else free(spb);
} }
break;
} }
return close_and_send(timer, "null", 4); return close_and_send(timer, "null", 4);
} }
@@ -242,7 +247,7 @@ static int s1_get(THREADTIMER *timer) {
static int s2_set(THREADTIMER *timer) { static int s2_set(THREADTIMER *timer) {
uint8_t digest[16]; uint8_t digest[16];
timer->lock_type = DICT_LOCK_EX; timer->lock_type = DICT_LOCK_EX;
FILE *fp = open_dict(DICT_LOCK_EX, timer->index); FILE *fp = open_dict(DICT_LOCK_EX, timer->index, &mu);
if(fp) { if(fp) {
md5((uint8_t*)timer->dat, strlen(timer->dat)+1, digest); md5((uint8_t*)timer->dat, strlen(timer->dat)+1, digest);
uint8_t* dp = digest; uint8_t* dp = digest;
@@ -351,7 +356,7 @@ static int s4_del(THREADTIMER *timer) {
uint8_t digest[16]; uint8_t digest[16];
char ret[4]; char ret[4];
timer->lock_type = DICT_LOCK_EX; timer->lock_type = DICT_LOCK_EX;
FILE *fp = open_dict(DICT_LOCK_EX, timer->index); FILE *fp = open_dict(DICT_LOCK_EX, timer->index, &mu);
//timer->status = 0; //timer->status = 0;
if(fp) { if(fp) {
md5((uint8_t*)timer->dat, strlen(timer->dat)+1, digest); md5((uint8_t*)timer->dat, strlen(timer->dat)+1, digest);
@@ -373,7 +378,7 @@ static int s4_del(THREADTIMER *timer) {
static int s5_md5(THREADTIMER *timer) { static int s5_md5(THREADTIMER *timer) {
//timer->status = 0; //timer->status = 0;
fill_md5(); fill_md5(&mu);
if(is_md5_equal((uint8_t*)timer->dat)) return send_data(timer->accept_fd, timer->index, "null", 4); if(is_md5_equal((uint8_t*)timer->dat)) return send_data(timer->accept_fd, timer->index, "null", 4);
else return send_data(timer->accept_fd, timer->index, "nequ", 4); else return send_data(timer->accept_fd, timer->index, "nequ", 4);
} }
@@ -400,7 +405,6 @@ static void accept_timer(void *p) {
pthread_t thread = accept_threads[index]; pthread_t thread = accept_threads[index];
if(thread) { if(thread) {
pthread_kill(thread, SIGQUIT); pthread_kill(thread, SIGQUIT);
accept_threads[index] = 0;
puts("Kill thread"); puts("Kill thread");
} }
} }
@@ -418,9 +422,9 @@ static void kill_thread(THREADTIMER* timer) {
timer->ptr = NULL; timer->ptr = NULL;
puts("Free data"); puts("Free data");
} }
if(timer->lock_type) close_dict(timer->lock_type, timer->index); if(timer->lock_type) close_dict(timer->lock_type, timer->index, &mu);
free(timer); free(timer);
puts("Finish killing\n"); puts("Finish killing");
} }
static void handle_pipe(int signo) { static void handle_pipe(int signo) {
@@ -433,9 +437,7 @@ static void handle_accept(void *p) {
if(accept_fd > 0) { if(accept_fd > 0) {
puts("\nConnected to the client"); puts("\nConnected to the client");
pthread_t thread; pthread_t thread;
pthread_key_t key; pthread_cleanup_push((void*)&kill_thread, p);
pthread_key_create(&key, (void *)&kill_thread);
pthread_setspecific(key, p);
if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) puts("Error creating timer thread"); if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) puts("Error creating timer thread");
else puts("Creating timer thread succeeded"); else puts("Creating timer thread succeeded");
//send_data(accept_fd, "Welcome to simple dict server.", 31); //send_data(accept_fd, "Welcome to simple dict server.", 31);
@@ -541,6 +543,7 @@ static void handle_accept(void *p) {
} }
CONV_END: puts("Conversation end"); CONV_END: puts("Conversation end");
} else puts("Error allocating buffer"); } else puts("Error allocating buffer");
pthread_cleanup_pop(1);
puts("Thread exited normally"); puts("Thread exited normally");
} else puts("Error accepting client"); } else puts("Error accepting client");
} }
@@ -601,7 +604,7 @@ static void accept_client() {
} }
static int close_and_send(THREADTIMER* timer, char *data, size_t numbytes) { static int close_and_send(THREADTIMER* timer, char *data, size_t numbytes) {
close_dict(timer->lock_type, timer->index); close_dict(timer->lock_type, timer->index, &mu);
return send_data(timer->accept_fd, timer->index, data, numbytes); return send_data(timer->accept_fd, timer->index, data, numbytes);
} }
@@ -622,7 +625,7 @@ int main(int argc, char *argv[]) {
if(!fp) fp = fopen(argv[as_daemon?4:3], "wb+"); if(!fp) fp = fopen(argv[as_daemon?4:3], "wb+");
if(fp) { if(fp) {
fclose(fp); fclose(fp);
if(init_dict(argv[as_daemon?4:3])) { if(init_dict(argv[as_daemon?4:3], &mu)) {
fp = NULL; fp = NULL;
if(argv[as_daemon?5:4][0] == '-') { // use env if(argv[as_daemon?5:4][0] == '-') { // use env
fp = (FILE*)1; fp = (FILE*)1;