Skip to content

Commit

Permalink
add error log
Browse files Browse the repository at this point in the history
  • Loading branch information
liexusong committed Sep 5, 2013
1 parent 4d83fe8 commit e9c37fe
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 32 deletions.
19 changes: 13 additions & 6 deletions beast_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

static inline int __do_lock(beast_locker_t *locker, int type)
{
int ret;
int ret, tries = 10;
struct flock lock;

lock.l_type = type;
Expand All @@ -23,7 +23,9 @@ static inline int __do_lock(beast_locker_t *locker, int type)

do {
ret = fcntl(locker->fd, F_SETLKW, &lock);
} while (ret < 0 && (errno == EINTR || errno == EAGAIN));
tries--;
} while (ret < 0 && tries > 0 &&
(errno == EINTR || errno == EAGAIN));

return ret;
}
Expand Down Expand Up @@ -58,30 +60,36 @@ beast_locker_t *beast_locker_create(char *path)
}


void beast_locker_wrlock(beast_locker_t *locker)
int beast_locker_wrlock(beast_locker_t *locker)
{
if (__do_lock(locker, F_WRLCK) < 0) {
beast_write_log(beast_log_notice,
"beast_locker_wrlock failed errno(%d)", errno);
return -1;
}
return 0;
}


void beast_locker_rdlock(beast_locker_t *locker)
int beast_locker_rdlock(beast_locker_t *locker)
{
if (__do_lock(locker, F_RDLCK) < 0) {
beast_write_log(beast_log_notice,
"beast_locker_rdlock failed errno(%d)", errno);
return -1;
}
return 0;
}


void beast_locker_unlock(beast_locker_t *locker)
int beast_locker_unlock(beast_locker_t *locker)
{
if (__do_lock(locker, F_UNLCK) < 0) {
beast_write_log(beast_log_notice,
"beast_locker_unlock failed errno(%d)", errno);
return -1;
}
return 0;
}


Expand All @@ -92,4 +100,3 @@ void beast_locker_destroy(beast_locker_t *locker)
free(locker->path);
free(locker);
}

6 changes: 3 additions & 3 deletions beast_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ typedef struct {
extern char *beast_lock_path;

beast_locker_t *beast_locker_create(char *path);
void beast_locker_wrlock(beast_locker_t *locker);
void beast_locker_rdlock(beast_locker_t *locker);
void beast_locker_unlock(beast_locker_t *locker);
int beast_locker_wrlock(beast_locker_t *locker);
int beast_locker_rdlock(beast_locker_t *locker);
int beast_locker_unlock(beast_locker_t *locker);
void beast_locker_destroy(beast_locker_t *locker);

#define beast_locker_lock(locker) beast_locker_wrlock(locker)
Expand Down
48 changes: 37 additions & 11 deletions beast_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,12 @@ void *beast_mm_malloc(int size)
int offset;
void *p = NULL;

beast_locker_lock(beast_mm_locker);

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_malloc() failed, unable get locker");
return NULL;
}

offset = beast_mm_allocate(beast_mm_block, size);
if (offset != -1) {
p = (void *)(((char *)beast_mm_block) + offset);
Expand All @@ -264,13 +268,17 @@ void *beast_mm_calloc(int size)
int offset;
void *p = NULL;

beast_locker_lock(beast_mm_locker);

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_calloc() failed, unable get locker");
return NULL;
}

offset = beast_mm_allocate(beast_mm_block, size);
if (offset != -1) {
p = (void *)(((char *)beast_mm_block) + offset);
}

beast_locker_unlock(beast_mm_locker);

if (NULL != p) {
Expand All @@ -281,28 +289,39 @@ void *beast_mm_calloc(int size)
}


void beast_mm_free(void *p)
int beast_mm_free(void *p)
{
int offset;

offset = (unsigned int)((char *)p - (char *)beast_mm_block);
if (offset <= 0) {
return;
return -1;
}

if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_free() failed, unable get locker");
return -1;
}

beast_locker_lock(beast_mm_locker);
beast_mm_deallocate(beast_mm_block, offset);

beast_locker_unlock(beast_mm_locker);
return 0;
}


void beast_mm_flush()
int beast_mm_flush()
{
beast_header_t *header;
beast_block_t *block;
void *shmaddr;

beast_locker_lock(beast_mm_locker);
if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_flush() failed, unable get locker");
return -1;
}

shmaddr = beast_mm_block;
header = (beast_header_t *)shmaddr;
Expand All @@ -320,6 +339,8 @@ void beast_mm_flush()
block->next = 0;

beast_locker_unlock(beast_mm_locker);

return 0;
}


Expand All @@ -331,7 +352,12 @@ int beast_mm_availspace()
int size;
beast_header_t *header = (beast_header_t *)beast_mm_block;

beast_locker_lock(beast_mm_locker);
if (beast_locker_lock(beast_mm_locker) == -1) {
beast_write_log(beast_log_notice,
"beast_mm_availspace() failed, unable get locker");
return -1;
}

size = header->avail;
beast_locker_unlock(beast_mm_locker);

Expand Down
4 changes: 2 additions & 2 deletions beast_mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
int beast_mm_init(int block_size);
void *beast_mm_malloc(int size);
void *beast_mm_calloc(int size);
void beast_mm_free(void *p);
void beast_mm_flush();
int beast_mm_free(void *p);
int beast_mm_flush();
int beast_mm_availspace();
int beast_mm_realspace();
void beast_mm_destroy();
Expand Down
40 changes: 30 additions & 10 deletions cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ cache_item_t *beast_cache_find(cache_key_t *key)
int index = hashval % BUCKETS_DEFAULT_SIZE;
cache_item_t *item, *temp;

beast_locker_rdlock(beast_cache_locker); /* read lock */
if (beast_locker_rdlock(beast_cache_locker) == -1) {
beast_write_log(beast_log_error,
"Unable call beast_cache_find(), failed to get locker");
return NULL;
}

item = beast_cache_buckets[index];
while (item) {
Expand All @@ -112,9 +116,9 @@ cache_item_t *beast_cache_find(cache_key_t *key)
beast_mm_free(item);
item = NULL;
}

beast_locker_unlock(beast_cache_locker);

return item;
}

Expand All @@ -137,7 +141,11 @@ cache_item_t *beast_cache_create(cache_key_t *key, int size)
{
int nsize;

beast_locker_lock(beast_cache_locker);
if (beast_locker_lock(beast_cache_locker) == -1) {
beast_write_log(beast_log_error,
"Unable call beast_cache_create(), failed to get locker");
return NULL;
}

beast_mm_flush(); /* clean all caches */

Expand Down Expand Up @@ -173,7 +181,11 @@ cache_item_t *beast_cache_push(cache_item_t *item)
int index = hashval % BUCKETS_DEFAULT_SIZE;
cache_item_t **this, *self;

beast_locker_lock(beast_cache_locker); /* lock */
if (beast_locker_lock(beast_cache_locker) == -1) {
beast_write_log(beast_log_error,
"Unable call beast_cache_push(), failed to get locker");
return NULL;
}

this = &beast_cache_buckets[index];
while (*this) {
Expand Down Expand Up @@ -214,7 +226,11 @@ int beast_cache_destroy()
return 0;
}

beast_locker_lock(beast_cache_locker);
if (beast_locker_lock(beast_cache_locker) == -1) {
beast_write_log(beast_log_error,
"Unable call beast_cache_destroy(), failed to get locker");
return NULL;
}

for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
item = beast_cache_buckets[index];
Expand All @@ -225,12 +241,12 @@ int beast_cache_destroy()
}
}

beast_mm_free(beast_cache_buckets);
beast_mm_destroy();

beast_locker_unlock(beast_cache_locker);
beast_locker_destroy(beast_cache_locker);

beast_mm_free(beast_cache_buckets);
beast_mm_destroy();

beast_cache_initialization = 0;

return 0;
Expand All @@ -243,7 +259,11 @@ void beast_cache_info(zval *retval)
int i;
cache_item_t *item;

beast_locker_rdlock(beast_cache_locker); /* read lock */
if (beast_locker_rdlock(beast_cache_locker) == -1) {
beast_write_log(beast_log_error,
"Unable call beast_cache_info(), failed to get locker");
return;
}

for (i = 0; i < BUCKETS_DEFAULT_SIZE; i++) {
item = beast_cache_buckets[i];
Expand Down

0 comments on commit e9c37fe

Please sign in to comment.