Skip to content

Commit

Permalink
Fix atomics 64bits (#9)
Browse files Browse the repository at this point in the history
* micro-ROS changes over dashing

Feature/add security directory (#1)

* Added security directory

* Updated security directory

Feature/avoid filesystem and allocation (#2)

* Included RCUTILS_NO_FILESYSTEM and RCUTILS_AVOID_DYNAMIC_ALLOCATION

* Added no filesystem options

* Default allocators write access

* Avoid dynamic allocation and no filesytem on error handling

* Typo

* New flags for filesystem and avoid dynamic

* Error handling template

* New allocator approach

Add test_security_directory test from rcl. (#3)

Merge pull request #4 from micro-ROS/feature/zephyr_fixes

Feature/zephyr fixes

CMake refactor (#5)


Update approach (#6)

* Update approach

* Remove target_compile_definitions and refactor flags install

* Added RCUTILS_NO_FILESYSTEM on new functions

* Added RCUTILS_NO_FILESYSTEM on new functions

Co-authored-by: Pablo Garrido <pablogs9@gmail.com>

* Initial changes

* Add hashing and lock pool

* Updates

Co-authored-by: Jose Antonio Moral <joseantoniomoralparras@gmail.com>
Fix atomics 64bits (#9)
  • Loading branch information
pablogs9 committed Jan 13, 2021
1 parent 417d09c commit 47db372
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project(rcutils)
option(RCUTILS_NO_THREAD_SUPPORT "Disable thread support." OFF)
option(RCUTILS_NO_FILESYSTEM "Disable filesystem usage." OFF)
option(RCUTILS_AVOID_DYNAMIC_ALLOCATION "Disable dynamic allocations." OFF)
option(RCUTILS_NO_64_ATOMIC "Disable support for 64 bits atomic operations." OFF)

# Default to C11
if(NOT CMAKE_C_STANDARD)
Expand Down Expand Up @@ -74,6 +75,7 @@ set(rcutils_sources
src/time.c
${time_impl_c}
src/uint8_array.c
$<$<BOOL:${RCUTILS_NO_64_ATOMIC}>:src/atomic_64bits.c>
)

set_source_files_properties(
Expand Down
92 changes: 92 additions & 0 deletions src/atomic_64bits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
#include <stdbool.h>

#define FLAGS_LEN 23

static bool * get_memory_lock(void *address)
{
static bool memory_locks[FLAGS_LEN] = { 0 };
uintptr_t a = (uintptr_t)(address);

// Public domain hash function taken from http://burtleburtle.net/bob/hash/integer.html
a = (a ^ 61) ^ (a >> 16);
a = a + (a << 3);
a = a ^ (a >> 4);
a = a * 0x27d4eb2d;
a = a ^ (a >> 15);

a = a % FLAGS_LEN;
return memory_locks + a;
}

void lock_memory(uint64_t *address){
bool * memory_lock = get_memory_lock(address);

while (__atomic_test_and_set(memory_lock, __ATOMIC_ACQUIRE) == 1);
}

void unlock_memory(uint64_t *address){
bool * memory_lock = get_memory_lock(address);

__atomic_clear(memory_lock, __ATOMIC_RELEASE);
}

uint64_t __atomic_load_8(uint64_t *mem, int model) {
(void) model;

lock_memory(mem);
uint64_t ret = *mem;
unlock_memory(mem);
return ret;
}

void __atomic_store_8(uint64_t *mem, uint64_t val, int model) {
(void) model;

lock_memory(mem);
*mem = val;
unlock_memory(mem);
}

uint64_t __atomic_exchange_8(uint64_t *mem, uint64_t val, int model) {
(void) model;

lock_memory(mem);
uint64_t ret = *mem;
*mem = val;
unlock_memory(mem);
return ret;
}

uint64_t __atomic_fetch_add_8(uint64_t *mem, uint64_t val, int model) {
(void) model;

lock_memory(mem);
uint64_t ret = *mem;
*mem += val;
unlock_memory(mem);
return ret;
}

#ifdef __cplusplus
}
#endif

0 comments on commit 47db372

Please sign in to comment.