* Remove old atomic.h and atomic_test.cpp.
Use stdatomi.h in counter.c.
* Suppress warnings on unused variables/parameters.
* Suppress warnings on redefined typedef.
Change-Id: Ic7ec652608f2c9423984a3631fb58efea5a835e7
# libbtcore static library for target
# ========================================================
include $(CLEAR_VARS)
-# osi/include/atomic.h depends on gcc atomic functions
-LOCAL_CLANG := false
+LOCAL_CLANG_CFLAGS += -Wno-error=typedef-redefinition
LOCAL_C_INCLUDES := $(btcoreCommonIncludes)
LOCAL_SRC_FILES := $(btcoreCommonSrc)
LOCAL_CFLAGS := -std=c99 $(bdroid_CFLAGS)
# ========================================================
ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
-# osi/include/atomic.h depends on gcc atomic functions
-LOCAL_CLANG := false
+LOCAL_CLANG_CFLAGS += -Wno-error=typedef-redefinition
LOCAL_C_INCLUDES := $(btcoreCommonIncludes)
LOCAL_SRC_FILES := $(btcoreCommonSrc)
# TODO(armansito): Setting _GNU_SOURCE isn't very platform-independent but
# libbtcore unit tests for target
# ========================================================
include $(CLEAR_VARS)
-# osi/include/atomic.h depends on gcc atomic functions
-LOCAL_CLANG := false
+LOCAL_CLANG_CFLAGS += -Wno-error=typedef-redefinition
LOCAL_C_INCLUDES := $(btcoreCommonIncludes)
LOCAL_SRC_FILES := $(btcoreCommonTestSrc)
LOCAL_CFLAGS := -Wall -Werror -Werror=unused-variable
# ========================================================
ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
-# osi/include/atomic.h depends on gcc atomic functions
-LOCAL_CLANG := false
+LOCAL_CLANG_CFLAGS += -Wno-error=typedef-redefinition
LOCAL_C_INCLUDES := $(btcoreCommonIncludes)
LOCAL_SRC_FILES := $(btcoreCommonTestSrc)
LOCAL_CFLAGS := -Wall -Werror -Werror=unused-variable
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
+#include <stdatomic.h>
#include <string.h>
#include <sys/eventfd.h>
#include "btcore/include/counter.h"
#include "btcore/include/module.h"
#include "osi/include/allocator.h"
-#include "osi/include/atomic.h"
#include "osi/include/hash_functions.h"
#include "osi/include/hash_map.h"
#include "osi/include/list.h"
typedef int (*handler_t)(socket_t * socket);
typedef struct counter_t {
- atomic_s64_t val;
+ _Atomic(int64_t) val;
} counter_t;
typedef struct hash_element_t {
assert(name != NULL);
counter_t *counter = name_to_counter_(name);
if (counter)
- atomic_store_s64(&counter->val, val);
+ atomic_store(&counter->val, val);
}
void counter_add(const char *name, counter_data_t val) {
assert(name != NULL);
counter_t *counter = name_to_counter_(name);
if (counter) {
- if (val == 1)
- atomic_inc_prefix_s64(&counter->val);
- else
- atomic_add_s64(&counter->val, val);
+ atomic_fetch_add(&counter->val, val);
}
}
if (!counter) {
return NULL;
}
- atomic_store_s64(&counter->val, initial_val);
+ atomic_store(&counter->val, initial_val);
return counter;
}
./test/allocation_tracker_test.cpp \
./test/allocator_test.cpp \
./test/array_test.cpp \
- ./test/atomic_test.cpp \
./test/config_test.cpp \
./test/data_dispatcher_test.cpp \
./test/eager_reader_test.cpp \
# libosi static library for target
# ========================================================
include $(CLEAR_VARS)
-LOCAL_CLANG := false # osi/include/atomic.h depends on gcc atomic functions
LOCAL_C_INCLUDES := $(btosiCommonIncludes)
LOCAL_SRC_FILES := $(btosiCommonSrc)
LOCAL_CFLAGS := $(btosiCommonCFlags)
# ========================================================
ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
-LOCAL_CLANG := false # osi/include/atomic.h depends on gcc atomic functions
LOCAL_C_INCLUDES := $(btosiCommonIncludes)
LOCAL_SRC_FILES := $(btosiCommonSrc)
# TODO(armansito): Setting _GNU_SOURCE isn't very platform-independent but
# libosi unit tests for target
# ========================================================
include $(CLEAR_VARS)
-LOCAL_CLANG := false # osi/include/atomic.h depends on gcc atomic functions
LOCAL_C_INCLUDES := $(btosiCommonIncludes)
LOCAL_SRC_FILES := $(btosiCommonTestSrc)
LOCAL_CFLAGS := -Wall
# ========================================================
ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
-LOCAL_CLANG := false # osi/include/atomic.h depends on gcc atomic functions
LOCAL_C_INCLUDES := $(btosiCommonIncludes)
LOCAL_SRC_FILES := $(btosiCommonTestSrc)
LOCAL_CFLAGS := -Wall
"test/allocation_tracker_test.cpp",
"test/allocator_test.cpp",
"test/array_test.cpp",
- "test/atomic_test.cpp",
"test/config_test.cpp",
"test/data_dispatcher_test.cpp",
"test/eager_reader_test.cpp",
+++ /dev/null
-/******************************************************************************
- *
- * Copyright (C) 2014 Google, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************/
-
-#pragma once
-
-#include <stdbool.h>
-#include <stdint.h>
-
-// Set of atomic operations to work on data structures.
-// The atomic type data should only be manipulated by these functions.
-// This uses the gcc built in functions.
-// gcc doc section 6.50 "Built-in functions for memory model aware atomic operations"
-//
-// Data types:
-// atomic_<name>_t
-//
-// e.g.
-// atomic_s32_t for a signed 32 bit integer.
-// atomic_u32_t for an unsigned 32 bit integer.
-//
-// Functions:
-// atomic_<operation>_<name>(atomic_t *, ...)
-//
-// e.g.
-// uint32_t atomic_dec_prefix_u32(volatile atomic_u32_t *atomic)
-//
-// The prefix/postfix classification corresponds which value is returned
-// from the function call, the old or the new one.
-
-#if defined(GCC_VERSION) && GCC_VERSION < 40700
-#warning "Atomics not supported"
-#endif
-
-#define ATOMIC_TYPE(name, type) \
- struct atomic_##name { \
- type _val; \
-}; \
-typedef struct atomic_##name atomic_##name##_t;
-
-#define ATOMIC_STORE(name, type, sz) \
-static inline void atomic_store_##name(volatile atomic_##name##_t *atomic, type val) { \
- __atomic_store_##sz(&atomic->_val, val, __ATOMIC_SEQ_CST); \
-}
-
-#define ATOMIC_LOAD(name, type, sz) \
-static inline type atomic_load_##name(volatile atomic_##name##_t *atomic) { \
- return __atomic_load_##sz(&atomic->_val, __ATOMIC_SEQ_CST); \
-}
-
-// Returns value after operation, e.g. new value
-#define ATOMIC_INC_PREFIX(name, type, sz) \
-static inline type atomic_inc_prefix_##name(volatile atomic_##name##_t *atomic) { \
- return __atomic_add_fetch_##sz(atomic, 1, __ATOMIC_SEQ_CST); \
-}
-
-// Returns value after operation, e.g. new value
-#define ATOMIC_DEC_PREFIX(name, type, sz) \
-static inline type atomic_dec_prefix_##name(volatile atomic_##name##_t *atomic) { \
- return __atomic_sub_fetch_##sz(atomic, 1, __ATOMIC_SEQ_CST); \
-}
-
-// Returns value before operation, e.g. old value
-#define ATOMIC_INC_POSTFIX(name, type, sz) \
-static inline type atomic_inc_postfix_##name(volatile atomic_##name##_t *atomic) { \
- return __atomic_fetch_add_##sz(atomic, 1, __ATOMIC_SEQ_CST); \
-}
-
-// Returns value before operation, e.g. old value
-#define ATOMIC_DEC_POSTFIX(name, type, sz) \
-static inline type atomic_dec_postfix_##name(volatile atomic_##name##_t *atomic) { \
- return __atomic_fetch_sub_##sz(atomic, 1, __ATOMIC_SEQ_CST); \
-}
-
-// Returns value after operation, e.g. new value
-#define ATOMIC_ADD(name, type, sz) \
-static inline type atomic_add_##name(volatile atomic_##name##_t *atomic, type val) { \
- return __atomic_add_fetch_##sz(atomic, val, __ATOMIC_SEQ_CST); \
-}
-
-// Returns value after operation, e.g. new value
-#define ATOMIC_SUB(name, type, sz) \
-static inline type atomic_sub_##name(volatile atomic_##name##_t *atomic, type val) { \
- return __atomic_sub_fetch_##sz(atomic, val, __ATOMIC_SEQ_CST); \
-}
-
-#define ATOMIC_MAKE(name, type, sz) \
- ATOMIC_TYPE(name, type) \
- ATOMIC_STORE(name, type, sz) \
- ATOMIC_LOAD(name, type, sz) \
- ATOMIC_INC_PREFIX(name, type, sz) \
- ATOMIC_DEC_PREFIX(name, type, sz) \
- ATOMIC_INC_POSTFIX(name, type, sz) \
- ATOMIC_DEC_POSTFIX(name, type, sz) \
- ATOMIC_ADD(name, type, sz) \
- ATOMIC_SUB(name, type, sz)
-
-ATOMIC_MAKE(s32, int32_t, 4)
-ATOMIC_MAKE(u32, uint32_t, 4)
-ATOMIC_MAKE(s64, int64_t, 8)
-ATOMIC_MAKE(u64, uint64_t, 8)
return return_ptr;
}
-void *allocation_tracker_notify_free(uint8_t allocator_id, void *ptr) {
+void *allocation_tracker_notify_free(UNUSED_ATTR uint8_t allocator_id, void *ptr) {
if (!allocations || !ptr)
return ptr;
assert(allocation->allocator_id == allocator_id); // Must be from the same allocator
allocation->freed = true;
- const char *beginning_canary = ((char *)ptr) - canary_size;
- const char *end_canary = ((char *)ptr) + allocation->size;
+ UNUSED_ATTR const char *beginning_canary = ((char *)ptr) - canary_size;
+ UNUSED_ATTR const char *end_canary = ((char *)ptr) + allocation->size;
for (size_t i = 0; i < canary_size; i++) {
assert(beginning_canary[i] == canary[i]);
#include <hash_map.h>
#include "osi/include/allocator.h"
+#include "osi/include/osi.h"
struct hash_map_t;
if (hash_map_entry) {
// Calls hash_map callback to delete the hash_map_entry.
- bool rc = list_remove(hash_bucket_list, hash_map_entry);
+ UNUSED_ATTR bool rc = list_remove(hash_bucket_list, hash_map_entry);
assert(rc == true);
} else {
hash_map->hash_size++;
+++ /dev/null
-#include <gtest/gtest.h>
-
-extern "C" {
-#include "atomic.h"
-}
-
-const static size_t ATOMIC_DATA_MAX = 2;
-const static int ATOMIC_MAX_THREADS = 20;
-
-struct atomic_test_s32_s {
- pthread_t pthread_id;
- int thread_num;
- int max_val;
- atomic_s32_t *data;
-};
-
-void *atomic_thread(void *context) {
- struct atomic_test_s32_s *at = (struct atomic_test_s32_s *)context;
- for (int i = 0; i < at->max_val; i++) {
- usleep(1);
- atomic_inc_prefix_s32(&at->data[i]);
- }
- return NULL;
-}
-
-void *atomic_thread_inc_dec(void *context) {
- struct atomic_test_s32_s *at = (struct atomic_test_s32_s *)context;
- for (int i = 0; i < at->max_val; i++) {
- usleep(1);
- atomic_inc_prefix_s32(&at->data[i]);
- usleep(1);
- atomic_dec_prefix_s32(&at->data[i]);
- }
- return NULL;
-}
-
-TEST(AtomicTest, test_store_load_s32) {
- atomic_s32_t data;
-
- atomic_store_s32(&data, -1);
- EXPECT_EQ(-1, atomic_load_s32(&data));
-
- atomic_store_s32(&data, 0);
- EXPECT_EQ(0, atomic_load_s32(&data));
-
- atomic_store_s32(&data, 1);
- EXPECT_EQ(1, atomic_load_s32(&data));
-
- atomic_store_s32(&data, 2);
- EXPECT_EQ(2, atomic_load_s32(&data));
-}
-
-TEST(AtomicTest, test_store_load_u32) {
- atomic_u32_t data;
-
- atomic_store_u32(&data, -1);
- EXPECT_EQ((uint32_t)-1, atomic_load_u32(&data));
-
- atomic_store_u32(&data, 0);
- EXPECT_EQ((uint32_t)0, atomic_load_u32(&data));
-
- atomic_store_u32(&data, 1);
- EXPECT_EQ((uint32_t)1, atomic_load_u32(&data));
-
- atomic_store_u32(&data, 2);
- EXPECT_EQ((uint32_t)2, atomic_load_u32(&data));
-}
-
-TEST(AtomicTest, test_inc_dec_s32) {
- atomic_s32_t data;
-
- atomic_store_s32(&data, 0);
- EXPECT_EQ(0, atomic_load_s32(&data));
-
- int32_t val = atomic_inc_prefix_s32(&data);
- EXPECT_EQ(1, atomic_load_s32(&data));
- EXPECT_EQ(1, val);
-
- val = atomic_inc_prefix_s32(&data);
- EXPECT_EQ(2, atomic_load_s32(&data));
- EXPECT_EQ(2, val);
-
- val = atomic_inc_prefix_s32(&data);
- EXPECT_EQ(3, atomic_load_s32(&data));
- EXPECT_EQ(3, val);
-
- val = atomic_dec_prefix_s32(&data);
- EXPECT_EQ(2, val);
-
- val = atomic_dec_prefix_s32(&data);
- EXPECT_EQ(1, val);
- val = atomic_dec_prefix_s32(&data);
- EXPECT_EQ(0, val);
- val = atomic_dec_prefix_s32(&data);
- EXPECT_EQ(-1, val);
-}
-
-TEST(AtomicTest, test_inc_dec_u32) {
- atomic_u32_t data;
-
- atomic_store_u32(&data, 0);
- EXPECT_EQ((unsigned)0, atomic_load_u32(&data));
-
- uint32_t val = atomic_inc_prefix_u32(&data);
- EXPECT_EQ((unsigned)1, atomic_load_u32(&data));
- EXPECT_EQ((unsigned)1, val);
-
- val = atomic_inc_prefix_u32(&data);
- EXPECT_EQ((unsigned)2, atomic_load_u32(&data));
- EXPECT_EQ((unsigned)2, val);
-
- val = atomic_inc_prefix_u32(&data);
- EXPECT_EQ((unsigned)3, atomic_load_u32(&data));
- EXPECT_EQ((unsigned)3, val);
-
- val = atomic_dec_prefix_u32(&data);
- EXPECT_EQ((unsigned)2, val);
-
- val = atomic_dec_prefix_u32(&data);
- EXPECT_EQ((unsigned)1, val);
- val = atomic_dec_prefix_u32(&data);
- EXPECT_EQ((unsigned)0, val);
- val = atomic_dec_prefix_u32(&data);
- EXPECT_EQ((unsigned)-1, val);
-}
-
-TEST(AtomicTest, test_atomic_inc_thread) {
- struct atomic_test_s32_s atomic_test[ATOMIC_MAX_THREADS];
- atomic_s32_t data[ATOMIC_DATA_MAX];
-
- memset(&atomic_test, 0, sizeof(atomic_test));
- memset(data, 0, sizeof(data));
-
- for (unsigned int i = 0; i < ATOMIC_DATA_MAX; i++) {
- EXPECT_EQ(0, data[i]._val);
- }
-
- for (int i = 0; i < ATOMIC_MAX_THREADS; i++) {
- atomic_test[i].thread_num = i;
- atomic_test[i].max_val = ATOMIC_DATA_MAX;
- atomic_test[i].data = data;
- pthread_create(&atomic_test[i].pthread_id, NULL, atomic_thread, &atomic_test[i]);
- }
-
- for (int i = 0; i < ATOMIC_MAX_THREADS; i++) {
- int rc = pthread_join(atomic_test[i].pthread_id, NULL);
- EXPECT_EQ(0, rc);
- }
-
- for (unsigned int i = 0; i < ATOMIC_DATA_MAX; i++) {
- EXPECT_EQ(1 * ATOMIC_MAX_THREADS, data[i]._val);
- }
-}
-
-TEST(AtomicTest, test_atomic_inc_thread_single) {
- struct atomic_test_s32_s atomic_test[ATOMIC_MAX_THREADS];
- atomic_s32_t data;
-
- memset(&atomic_test, 0, sizeof(atomic_test));
- memset(&data, 0, sizeof(data));
-
- EXPECT_EQ(0, data._val);
-
- for (int i = 0; i < ATOMIC_MAX_THREADS; i++) {
- atomic_test[i].thread_num = i;
- atomic_test[i].max_val = 1;
- atomic_test[i].data = &data;
- pthread_create(&atomic_test[i].pthread_id, NULL, atomic_thread_inc_dec, &atomic_test[i]);
- }
-
- for (int i = 0; i < ATOMIC_MAX_THREADS; i++) {
- int rc = pthread_join(atomic_test[i].pthread_id, NULL);
- EXPECT_EQ(0, rc);
- }
- EXPECT_EQ(0, data._val);
-}
-
-TEST(AtomicTest, test_store_load_s64) {
- atomic_s64_t data;
-
- atomic_store_s64(&data, -1);
- EXPECT_EQ(-1, atomic_load_s64(&data));
-
- atomic_store_s64(&data, 0);
- EXPECT_EQ(0, atomic_load_s64(&data));
-
- atomic_store_s64(&data, 1);
- EXPECT_EQ(1, atomic_load_s64(&data));
-
- atomic_store_s64(&data, 2);
- EXPECT_EQ(2, atomic_load_s64(&data));
-}
-
-TEST(AtomicTest, test_inc_dec_s64) {
- atomic_s64_t data;
-
- atomic_store_s64(&data, 0);
- EXPECT_EQ(0, atomic_load_s64(&data));
-
- int64_t val = atomic_inc_prefix_s64(&data);
- EXPECT_EQ(1, atomic_load_s64(&data));
- EXPECT_EQ(1, val);
-
- val = atomic_inc_prefix_s64(&data);
- EXPECT_EQ(2, atomic_load_s64(&data));
- EXPECT_EQ(2, val);
-
- val = atomic_inc_prefix_s64(&data);
- EXPECT_EQ(3, atomic_load_s64(&data));
- EXPECT_EQ(3, val);
-
- val = atomic_dec_prefix_s64(&data);
- EXPECT_EQ(2, val);
-
- val = atomic_dec_prefix_s64(&data);
- EXPECT_EQ(1, val);
- val = atomic_dec_prefix_s64(&data);
- EXPECT_EQ(0, val);
- val = atomic_dec_prefix_s64(&data);
- EXPECT_EQ(-1, val);
-
- // Mutating using postfix.
- val = atomic_inc_postfix_s64(&data);
- EXPECT_EQ(0, atomic_load_s64(&data));
- EXPECT_EQ(-1, val);
-
- val = atomic_inc_postfix_s64(&data);
- EXPECT_EQ(1, atomic_load_s64(&data));
- EXPECT_EQ(0, val);
-
- val = atomic_inc_postfix_s64(&data);
- EXPECT_EQ(2, atomic_load_s64(&data));
- EXPECT_EQ(1, val);
-
- val = atomic_dec_postfix_s64(&data);
- EXPECT_EQ(2, val);
-
- val = atomic_dec_postfix_s64(&data);
- EXPECT_EQ(1, val);
- val = atomic_dec_postfix_s64(&data);
- EXPECT_EQ(0, val);
- val = atomic_dec_postfix_s64(&data);
- EXPECT_EQ(-1, val);
- EXPECT_EQ(-2, atomic_load_s64(&data));
-}
-
-TEST(AtomicTest, test_add_sub_u64) {
- atomic_u64_t data;
-
- atomic_store_u64(&data, 0);
- EXPECT_EQ((unsigned)0, atomic_load_u64(&data));
-
- uint64_t val = atomic_add_u64(&data, 0xffff);
- EXPECT_EQ((unsigned)0xffff, atomic_load_u64(&data));
- EXPECT_EQ((unsigned)0xffff, val);
-
- val = atomic_add_u64(&data, 0xffff);
- EXPECT_EQ((unsigned)(2 * 0xffff), atomic_load_u64(&data));
- EXPECT_EQ((unsigned)(2 * 0xffff), val);
-
- val = atomic_add_u64(&data, 0xffff);
- EXPECT_EQ((unsigned)(3 * 0xffff), atomic_load_u64(&data));
- EXPECT_EQ((unsigned)(3 * 0xffff), val);
- EXPECT_NE((unsigned)(3 * 0xfff0), val);
-
- val = atomic_sub_u64(&data, 0xffff);
- EXPECT_EQ((unsigned)(2 * 0xffff), val);
-
- val = atomic_sub_u64(&data, 0);
- EXPECT_EQ((unsigned)(2 * 0xffff), val);
- val = atomic_sub_u64(&data, 0xffff);
- EXPECT_EQ((unsigned)(1 * 0xffff), val);
-
- val = atomic_sub_u64(&data, 0xffff);
- EXPECT_EQ((unsigned)0, val);
-}
-
-