Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 303fb245 authored by Chih-hung Hsieh's avatar Chih-hung Hsieh Committed by Gerrit Code Review
Browse files

Merge "Enable clang compilation, use stdatomic.h."

parents 29d07e29 cdbab5a9
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -46,8 +46,7 @@ btcoreCommonIncludes := \
# 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)
@@ -61,8 +60,7 @@ include $(BUILD_STATIC_LIBRARY)
# ========================================================
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
@@ -82,8 +80,7 @@ endif
# 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
@@ -97,8 +94,7 @@ include $(BUILD_NATIVE_TEST)
# ========================================================
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
+5 −8
Original line number Diff line number Diff line
@@ -23,13 +23,13 @@
#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"
@@ -41,7 +41,7 @@
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 {
@@ -157,17 +157,14 @@ void counter_set(const char *name, counter_data_t val) {
  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);
  }
}

@@ -187,7 +184,7 @@ static counter_t *counter_new_(counter_data_t initial_val) {
  if (!counter) {
    return NULL;
  }
  atomic_store_s64(&counter->val, initial_val);
  atomic_store(&counter->val, initial_val);
  return counter;
}

+0 −5
Original line number Diff line number Diff line
@@ -55,7 +55,6 @@ btosiCommonTestSrc := \
    ./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 \
@@ -77,7 +76,6 @@ btosiCommonCFlags := -std=c99 -Wall -Werror -fvisibility=hidden
# 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)
@@ -93,7 +91,6 @@ include $(BUILD_STATIC_LIBRARY)
# ========================================================
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
@@ -117,7 +114,6 @@ endif
# 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
@@ -131,7 +127,6 @@ include $(BUILD_NATIVE_TEST)
# ========================================================
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
+0 −1
Original line number Diff line number Diff line
@@ -61,7 +61,6 @@ executable("net_test_osi") {
    "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",

system/osi/include/atomic.h

deleted100644 → 0
+0 −115
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  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)
Loading