Skip to content

Commit

Permalink
use THAtomic functions when available
Browse files Browse the repository at this point in the history
  • Loading branch information
andresy committed Oct 12, 2015
1 parent 75e9587 commit e685c30
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
cmake_minimum_required (VERSION 2.8)
cmake_policy(VERSION 2.8)

FIND_PACKAGE(Torch QUIET)
IF(Torch_FOUND)
ADD_DEFINITIONS(-DHAS_TORCH)
ENDIF()

set(PKGNAME tds)

add_library(${PKGNAME} MODULE
Expand All @@ -10,6 +15,10 @@ add_library(${PKGNAME} MODULE
"tds_vec.c"
)

IF(Torch_FOUND)
TARGET_LINK_LIBRARIES(${PKGNAME} TH)
ENDIF()

install(TARGETS ${PKGNAME} LIBRARY
DESTINATION ${LUA_CPATH})

Expand Down
1 change: 1 addition & 0 deletions rocks/tds-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ build = {
type = "cmake",
variables = {
CMAKE_BUILD_TYPE="Release",
CMAKE_PREFIX_PATH="$(LUA_BINDIR)/..",
LUA_PATH="$(LUADIR)",
LUA_CPATH="$(LIBDIR)"
}
Expand Down
17 changes: 13 additions & 4 deletions tds_hash.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <stdio.h>
#include <string.h>

#include "klib/khash.h"
#include "tds_utils.h"
#include "tds_elem.h"
#include "tds_hash.h"

#if HAS_TORCH
#include "THAtomic.h"
#endif

static uint32_t tds_elem_hashkey_(tds_elem elem)
{
return tds_elem_hashkey(&elem);
Expand All @@ -21,7 +24,7 @@ KHASH_INIT(tds_elem, tds_elem, tds_elem, 1, tds_elem_hashkey_, tds_elem_isequal_
/* hash structure */
struct tds_hash_ {
khash_t(tds_elem) *hash;
long refcount;
int refcount;
};


Expand Down Expand Up @@ -87,13 +90,21 @@ unsigned long tds_hash_size(tds_hash *hash)

void tds_hash_retain(tds_hash *hash)
{
#if HAS_TORCH
THAtomicIncrementRef(&hash->refcount);
#else
hash->refcount++;
#endif
}

void tds_hash_free(tds_hash* hash)
{
#if HAS_TORCH
if(THAtomicDecrementRef(&hash->refcount))
#else
hash->refcount--;
if(hash->refcount == 0)
#endif
{
khint_t k;
for(k = kh_begin(hash->hash); k != kh_end(hash->hash); ++k) {
Expand All @@ -105,8 +116,6 @@ void tds_hash_free(tds_hash* hash)
kh_destroy(tds_elem, hash->hash);
tds_free(hash);
}
else if(hash->refcount < 0)
printf("[tds hash] warning: refcount issue\n");
}

/* iterator */
Expand Down
15 changes: 12 additions & 3 deletions tds_vec.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <stdio.h> /* for printf */
#include <string.h> /* for memmove */
#include "tds_utils.h"
#include "tds_vec.h"

#if HAS_TORCH
#include "THAtomic.h"
#endif

/* note: size_t >= 0 */

#define BLOCK_SIZE 1024
Expand Down Expand Up @@ -101,20 +104,26 @@ int tds_vec_resize(tds_vec *vec, size_t size)

void tds_vec_retain(tds_vec *vec)
{
#if HAS_TORCH
THAtomicIncrementRef(&vec->refcount);
#else
vec->refcount++;
#endif
}

void tds_vec_free(tds_vec* vec)
{
size_t k;
#if HAS_TORCH
if(THAtomicDecrementRef(&vec->refcount))
#else
vec->refcount--;
if(vec->refcount == 0)
#endif
{
for(k = 0; k < vec->n; k++)
tds_elem_free_content(&vec->data[k]);
tds_free(vec->data);
tds_free(vec);
}
else if(vec->refcount < 0)
printf("[tds vec] warning: refcount issue\n");
}

0 comments on commit e685c30

Please sign in to comment.