Make 'refCount' volatile and detect support for atomic inc/dec in configure.ac

This commit is contained in:
bossiel 2014-05-04 03:11:04 +00:00
parent 3502376f64
commit a34f5884e6
4 changed files with 21 additions and 12 deletions

View File

@ -205,6 +205,15 @@ AC_TRY_COMPILE([#include <pthread.h>],
[AC_DEFINE(TSK_RECURSIVE_MUTEXATTR, PTHREAD_MUTEX_RECURSIVE_NP,
[Check whether PTHREAD_MUTEX_RECURSIVE_NP is defined])])])
AC_TRY_COMPILE([#include <stdlib.h>],
[static int a = 0; __sync_fetch_and_add(&a, 1);],
[AC_DEFINE(HAVE___SYNC_FETCH_AND_ADD, 1,
[Check whether __sync_fetch_and_add is defined])],[])
AC_TRY_COMPILE([#include <stdlib.h>],
[static int a = 0; __sync_fetch_and_sub(&a, 1);],
[AC_DEFINE(HAVE___SYNC_FETCH_AND_SUB, 1,
[Check whether __sync_fetch_and_sub is defined])],[])
AC_DEFINE(USE_POLL, 1, [Setting USE_POLL to 1 for backward compatibility])
AC_CHECK_FUNCS([inet_pton inet_ntop poll getdtablesize opendir closedir getpid])

View File

@ -73,4 +73,15 @@ typedef unsigned int tsk_size_t; /**< Unsigned size */
#define tsk_null 0 /**< Null pointer */
#endif
#if defined(__GNUC__) || (HAVE___SYNC_FETCH_AND_ADD && HAVE___SYNC_FETCH_AND_SUB)
# define tsk_atomic_inc(_ptr_) __sync_fetch_and_add((_ptr_), 1)
# define tsk_atomic_dec(_ptr_) __sync_fetch_and_sub((_ptr_), 1)
#elif defined(_MSC_VER)
# define tsk_atomic_inc(_ptr_) InterlockedIncrement((_ptr_))
# define tsk_atomic_dec(_ptr_) InterlockedDecrement((_ptr_))
#else
# define tsk_atomic_inc(_ptr_) ++(*(_ptr_))
# define tsk_atomic_dec(_ptr_) --(*(_ptr_))
#endif
#endif /* _TINYSAK_COMMON_H_ */

View File

@ -41,17 +41,6 @@ static int tsk_objects_count = 0;
# define TSK_DEBUG_OBJECTS 0
#endif
#if defined(__GNUC__)
# define tsk_atomic_inc(_ptr_) __sync_fetch_and_add((_ptr_), 1)
# define tsk_atomic_dec(_ptr_) __sync_fetch_and_sub((_ptr_), 1)
#elif defined(_MSC_VER)
# define tsk_atomic_inc(_ptr_) InterlockedIncrement((_ptr_))
# define tsk_atomic_dec(_ptr_) InterlockedDecrement((_ptr_))
#else
# define tsk_atomic_inc(_ptr_) ++(*(_ptr_))
# define tsk_atomic_dec(_ptr_) --(*(_ptr_))
#endif
/**@ingroup tsk_object_group
* Creates new object. The object MUST be declared using @ref TSK_DECLARE_OBJECT macro.
* @param objdef The object meta-data (definition). For more infomation see @ref tsk_object_def_t.

View File

@ -78,7 +78,7 @@ typedef void tsk_object_t;
*/
#define TSK_DECLARE_OBJECT \
const void* __def__; /**< Opaque data holding a pointer to the actual meta-data(size, constructor, destructor and comparator) */ \
long refCount /**< Reference counter. */
volatile long refCount /**< Reference counter. */
/**@ingroup tsk_object_group
* Internal macro to get the definition of the object.