add pthread lock

This commit is contained in:
Cloud Wu
2015-08-13 21:00:20 +08:00
parent 0ce9921c25
commit bf8f9b8654
18 changed files with 249 additions and 104 deletions

View File

@@ -6,6 +6,7 @@
#include "malloc_hook.h"
#include "skynet.h"
#include "atomic.h"
static size_t _used_memory = 0;
static size_t _memory_block = 0;
@@ -32,11 +33,11 @@ get_allocated_field(uint32_t handle) {
ssize_t old_alloc = data->allocated;
if(old_handle == 0 || old_alloc <= 0) {
// data->allocated may less than zero, because it may not count at start.
if(!__sync_bool_compare_and_swap(&data->handle, old_handle, handle)) {
if(!ATOM_CAS(&data->handle, old_handle, handle)) {
return 0;
}
if (old_alloc < 0) {
__sync_bool_compare_and_swap(&data->allocated, old_alloc, 0);
ATOM_CAS(&data->allocated, old_alloc, 0);
}
}
if(data->handle != handle) {
@@ -47,21 +48,21 @@ get_allocated_field(uint32_t handle) {
inline static void
update_xmalloc_stat_alloc(uint32_t handle, size_t __n) {
__sync_add_and_fetch(&_used_memory, __n);
__sync_add_and_fetch(&_memory_block, 1);
ATOM_ADD(&_used_memory, __n);
ATOM_INC(&_memory_block);
ssize_t* allocated = get_allocated_field(handle);
if(allocated) {
__sync_add_and_fetch(allocated, __n);
ATOM_ADD(allocated, __n);
}
}
inline static void
update_xmalloc_stat_free(uint32_t handle, size_t __n) {
__sync_sub_and_fetch(&_used_memory, __n);
__sync_sub_and_fetch(&_memory_block, 1);
ATOM_SUB(&_used_memory, __n);
ATOM_DEC(&_memory_block);
ssize_t* allocated = get_allocated_field(handle);
if(allocated) {
__sync_sub_and_fetch(allocated, __n);
ATOM_SUB(allocated, __n);
}
}