Skip to content

Commit

Permalink
implement own lock
Browse files Browse the repository at this point in the history
  • Loading branch information
computerphilosopher committed Aug 6, 2019
1 parent 638e828 commit 6746bb9
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 438 deletions.
34 changes: 28 additions & 6 deletions cmd_in_second.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct cmd_in_second {
int32_t bulk_limit;
int32_t log_per_timer;
state cur_state;
pthread_mutex_t lock;
};

static struct cmd_in_second this;
Expand Down Expand Up @@ -196,7 +197,6 @@ static void buffer_add(const logtype* log)
}
buffer->front = (buffer->front+1) % buffer->capacity;
}

}

static void timer_add()
Expand Down Expand Up @@ -226,7 +226,9 @@ static bool is_cmd_to_log(const char* collection_name, const char* cmd)
bool cmd_in_second_write(const char* collection_name, const char* cmd,
const char* key, const char* client_ip)
{
pthread_mutex_lock(&this.lock);
if (this.cur_state != ON_LOGGING || !is_cmd_to_log(collection_name, cmd)) {
pthread_mutex_unlock(&this.lock);
return false;
}

Expand All @@ -241,38 +243,56 @@ bool cmd_in_second_write(const char* collection_name, const char* cmd,
buffer_add(&log);
this.timer.circular_counter = (this.timer.circular_counter+1) % this.log_per_timer;

pthread_mutex_unlock(&this.lock);
return true;
}

void cmd_in_second_init()
{
assert("test");
this.cur_state = NOT_STARTED;
pthread_mutex_init(&this.lock, NULL);

this.buffer.front = 0;
this.buffer.rear = 0;
this.buffer.ring = NULL;

this.timer.front = 0;
this.timer.rear = 0;
this.timer.capacity = 0;
this.timer.circular_counter = 0;
this.timer.ring = NULL;
}

int32_t cmd_in_second_start(const char* collection_name, const char* cmd,
const int32_t bulk_limit)
{

pthread_mutex_lock(&this.lock);

if (this.cur_state != NOT_STARTED) {
pthread_mutex_unlock(&this.lock);
return CMD_IN_SECOND_STARTED_ALREADY;
}

this.bulk_limit = bulk_limit;

this.buffer.capacity = bulk_limit+1;
this.buffer.front = 0;
this.buffer.rear = 0;
this.buffer.ring = (logtype*)malloc(this.buffer.capacity * sizeof(logtype));

if (this.buffer.ring == NULL) {
pthread_mutex_unlock(&this.lock);
return CMD_IN_SECOND_NO_MEM;
}

this.log_per_timer = bulk_limit / 10 + (bulk_limit % 10 != 0);
this.timer.capacity = this.log_per_timer + 1;
this.timer.front = 0;
this.timer.rear = 0;
this.timer.circular_counter = 0;

this.timer.ring = (struct timeval*)malloc(this.timer.capacity * sizeof(struct timeval));

if (this.timer.ring == NULL) {
free(this.buffer.ring);
pthread_mutex_unlock(&this.lock);
return CMD_IN_SECOND_NO_MEM;
}

Expand All @@ -281,5 +301,7 @@ int32_t cmd_in_second_start(const char* collection_name, const char* cmd,

this.cur_state = ON_LOGGING;

pthread_mutex_unlock(&this.lock);

return CMD_IN_SECOND_START;
}
5 changes: 3 additions & 2 deletions cmd_in_second.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __CMD_IN_SECOND_LOG__
#define __CMD_IN_SECOND_LOG__
#ifndef __CMD_IN_SECOND_
#define __CMD_IN_SECOND_
#endif

#include <stdbool.h>
Expand All @@ -10,5 +10,6 @@
#define CMD_IN_SECOND_STARTED_ALREADY 1
#define CMD_IN_SECOND_NO_MEM 2

void cmd_in_second_init(void);
int32_t cmd_in_second_start(const char* collection_name, const char* cmd, const int32_t bulk_limit);
bool cmd_in_second_write(const char* collection_name, const char* cmd, const char* key, const char* client_ip);
Loading

0 comments on commit 6746bb9

Please sign in to comment.