diff --git a/.github/workflows/emscripten.yaml b/.github/workflows/emscripten.yaml index b995749d9..fd545d6dc 100644 --- a/.github/workflows/emscripten.yaml +++ b/.github/workflows/emscripten.yaml @@ -34,7 +34,7 @@ jobs: - name: Compile debug run: | mkdir build - emcmake cmake -E env CFLAGS="-DZ_LINK_WS=1 -DZ_LINK_TCP=0 -DZ_LINK_UDP_MULTICAST=0 -DZ_LINK_UDP_UNICAST=0 -DZ_SCOUTING_UDP=0" cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_STANDARD=11 -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=OFF -DBUILD_MULTICAST=OFF -DBUILD_INTEGRATION=OFF -DBUILD_TOOLS=OFF -DZENOH_DEBUG=3 -H. -Bbuild + emcmake cmake -E env CFLAGS="-DZ_LINK_WS=1 -DZ_LINK_TCP=0 -DZ_LINK_UDP_MULTICAST=0 -DZ_LINK_UDP_UNICAST=0 -DZ_SCOUTING_UDP=0" cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_STANDARD=11 -DBUILD_WITH_CUSTOM_ALLOCATOR=OFF -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=OFF -DBUILD_MULTICAST=OFF -DBUILD_INTEGRATION=OFF -DBUILD_TOOLS=OFF -DZENOH_DEBUG=3 -H. -Bbuild make -C build diff --git a/.github/workflows/testing.yaml b/.github/workflows/testing.yaml new file mode 100644 index 000000000..6284a08de --- /dev/null +++ b/.github/workflows/testing.yaml @@ -0,0 +1,52 @@ +# +# Copyright (c) 2022 ZettaScale Technology +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License 2.0 which is available at +# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +# which is available at https://www.apache.org/licenses/LICENSE-2.0. +# +# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +# +# Contributors: +# ZettaScale Zenoh Team, +# +name: testing + +on: + push: + branches: [ '**' ] + pull_request: + branches: [ '**' ] + schedule: + - cron: '0 6 * * 1-5' + +jobs: + build: + + name: Build on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest, macOS-latest ] + + steps: + - uses: actions/checkout@v2 + + - name: Compile debug + run: make all + env: + BUILD_TYPE: Debug + BUILD_TESTING: ON + BUILD_MULTICAST: OFF + BUILD_INTEGRATION: OFF + + - name: Test debug + run: make test + env: + BUILD_TYPE: Debug # Workaround for Windows as it seems the previous step is being ignored + BUILD_TESTING: OFF # Workaround for Windows as it seems the previous step is being ignored + BUILD_MULTICAST: OFF # Workaround for Windows as it seems the previous step is being ignored + BUILD_INTEGRATION: ON # Workaround for Windows as it seems the previous step is being ignored + ZENOH_BRANCH: master diff --git a/BSDmakefile b/BSDmakefile index 790a7288e..d2439e12e 100644 --- a/BSDmakefile +++ b/BSDmakefile @@ -21,6 +21,10 @@ BUILD_TYPE?=Release # Accepted values: ON, OFF BUILD_EXAMPLES?=ON +# Build with custom / user allocators. This sets the BUILD_WITH_CUSTOM_ALLOCATOR variable. +# Accepted values: ON, OFF +BUILD_WITH_CUSTOM_ALLOCATOR?=OFF + # Build testing. This sets the BUILD_TESTING variable. # Accepted values: ON, OFF BUILD_TESTING?=ON @@ -58,7 +62,7 @@ CROSSIMG_PREFIX=zenoh-pico_ # NOTES: # - ARM: old versions of dockcross/dockcross were creating some issues since they used an old GCC (4.8.3) which lacks (even using -std=gnu11) -CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. +CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DBUILD_WITH_CUSTOM_ALLOCATOR=$(BUILD_WITH_CUSTOM_ALLOCATOR) -D -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. all: make diff --git a/CMakeLists.txt b/CMakeLists.txt index 076446581..0c1edd39c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,20 +242,26 @@ if(UNIX) install(FILES "${CMAKE_SOURCE_DIR}/zenohpico.pc" CONFIGURATIONS Release RelWithDebInfo DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") endif() +if(BUILD_WITH_CUSTOM_ALLOCATOR) + add_definitions(-DZ_CUSTOM_ALLOCATOR=1) +endif() + if(BUILD_EXAMPLES) add_subdirectory(examples) endif() -if(UNIX OR MSVC) - if(BUILD_TOOLS) +if(BUILD_TOOLS) + if(NOT BUILD_WITH_CUSTOM_ALLOCATOR) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools) add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c) target_link_libraries(z_keyexpr_canonizer ${Libname}) endif() +endif() - if(BUILD_TESTING) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") +if(BUILD_TESTING) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + if(NOT BUILD_WITH_CUSTOM_ALLOCATOR) add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c) add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) @@ -281,10 +287,12 @@ if(UNIX OR MSVC) add_test(z_api_null_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_null_drop_test) add_test(z_api_double_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_double_drop_test) endif() +endif() - if(BUILD_MULTICAST) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") +if(BUILD_MULTICAST) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + if(NOT BUILD_WITH_CUSTOM_ALLOCATOR) if(CMAKE_C_STANDARD MATCHES "11") add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c) target_link_libraries(z_peer_multicast_test ${Libname}) @@ -295,10 +303,12 @@ if(UNIX OR MSVC) add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test) endif() endif() +endif() - if(BUILD_INTEGRATION) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") +if(BUILD_INTEGRATION) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + if(NOT BUILD_WITH_CUSTOM_ALLOCATOR) if(CMAKE_C_STANDARD MATCHES "11") add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c) add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c) diff --git a/GNUmakefile b/GNUmakefile index cf4512a1b..f4ee3c87d 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -21,6 +21,10 @@ BUILD_TYPE?=Release # Accepted values: ON, OFF BUILD_EXAMPLES?=ON +# Build with custom / user allocators. This sets the BUILD_WITH_CUSTOM_ALLOCATOR variable. +# Accepted values: ON, OFF +BUILD_WITH_CUSTOM_ALLOCATOR?=OFF + # Build testing. This sets the BUILD_TESTING variable. # Accepted values: ON, OFF BUILD_TESTING?=ON @@ -58,7 +62,7 @@ CROSSIMG_PREFIX=zenoh-pico_ # NOTES: # - ARM: old versions of dockcross/dockcross were creating some issues since they used an old GCC (4.8.3) which lacks (even using -std=gnu11) -CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. +CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DBUILD_WITH_CUSTOM_ALLOCATOR=$(BUILD_WITH_CUSTOM_ALLOCATOR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. all: make diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 837e41199..36b4067ee 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,62 +1,86 @@ +# +# Copyright (c) 2022 ZettaScale Technology +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License 2.0 which is available at +# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +# which is available at https://www.apache.org/licenses/LICENSE-2.0. +# +# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +# +# Contributors: +# ZettaScale Zenoh Team, +# if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) - # Settings when 'examples' is the root projet - cmake_minimum_required(VERSION 3.20) - project(zenohpico_examples LANGUAGES C) - include(../cmake/helpers.cmake) - set_default_build_type(Release) - configure_include_project(ZENOHPICO zenohpico zenohpico ".." zenohc "https://github.com/eclipse-zenoh/zenoh-pico" "") - add_custom_target(examples ALL) + # Settings when 'examples' is the root projet + cmake_minimum_required(VERSION 3.20) + project(zenohpico_examples LANGUAGES C) + include(../cmake/helpers.cmake) + set_default_build_type(Release) + configure_include_project(ZENOHPICO zenohpico zenohpico ".." zenohc "https://github.com/eclipse-zenoh/zenoh-pico" "") + add_custom_target(examples ALL) else() - message(STATUS "zenoh-pico examples") - add_custom_target(examples) + message(STATUS "zenoh-pico examples") + add_custom_target(examples) endif() function(add_example name) - add_executable(${name} ${ARGN}) - set_property(TARGET ${name} PROPERTY C_STANDARD 11) - target_link_libraries(${name} zenohpico) - add_dependencies(examples ${name}) + add_executable(${name} ${ARGN}) + set_property(TARGET ${name} PROPERTY C_STANDARD 11) + target_link_libraries(${name} zenohpico) + add_dependencies(examples ${name}) endfunction() if(UNIX) + if(BUILD_WITH_CUSTOM_ALLOCATOR) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples) if(CMAKE_C_STANDARD MATCHES "99") - add_example(z_put unix/c99/z_put.c) - add_example(z_pub unix/c99/z_pub.c) - add_example(z_pub_st unix/c99/z_pub_st.c) - add_example(z_sub unix/c99/z_sub.c) - add_example(z_sub_st unix/c99/z_sub_st.c) - add_example(z_pull unix/c99/z_pull.c) - add_example(z_get unix/c99/z_get.c) - add_example(z_queryable unix/c99/z_queryable.c) - add_example(z_info unix/c99/z_info.c) - add_example(z_scout unix/c99/z_scout.c) - add_example(z_ping unix/c99/z_ping.c) - add_example(z_pong unix/c99/z_pong.c) + add_executable(z_pub_ualloc ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub_ualloc.c) else() - add_example(z_put unix/c11/z_put.c) - add_example(z_pub unix/c11/z_pub.c) - add_example(z_pub_st unix/c11/z_pub_st.c) - add_example(z_sub unix/c11/z_sub.c) - add_example(z_sub_st unix/c11/z_sub_st.c) - add_example(z_pull unix/c11/z_pull.c) - add_example(z_get unix/c11/z_get.c) - add_example(z_queryable unix/c11/z_queryable.c) - add_example(z_info unix/c11/z_info.c) - add_example(z_scout unix/c11/z_scout.c) - add_example(z_ping unix/c11/z_ping.c) - add_example(z_pong unix/c11/z_pong.c) + add_executable(z_pub_ualloc ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub_ualloc.c) endif() + + target_link_libraries(z_pub_ualloc ${Libname}) + else() + if(CMAKE_C_STANDARD MATCHES "99") + add_example(z_put unix/c99/z_put.c) + add_example(z_pub unix/c99/z_pub.c) + add_example(z_pub_st unix/c99/z_pub_st.c) + add_example(z_sub unix/c99/z_sub.c) + add_example(z_sub_st unix/c99/z_sub_st.c) + add_example(z_pull unix/c99/z_pull.c) + add_example(z_get unix/c99/z_get.c) + add_example(z_queryable unix/c99/z_queryable.c) + add_example(z_info unix/c99/z_info.c) + add_example(z_scout unix/c99/z_scout.c) + add_example(z_ping unix/c99/z_ping.c) + add_example(z_pong unix/c99/z_pong.c) + else() + add_example(z_put unix/c11/z_put.c) + add_example(z_pub unix/c11/z_pub.c) + add_example(z_pub_st unix/c11/z_pub_st.c) + add_example(z_sub unix/c11/z_sub.c) + add_example(z_sub_st unix/c11/z_sub_st.c) + add_example(z_pull unix/c11/z_pull.c) + add_example(z_get unix/c11/z_get.c) + add_example(z_queryable unix/c11/z_queryable.c) + add_example(z_info unix/c11/z_info.c) + add_example(z_scout unix/c11/z_scout.c) + add_example(z_ping unix/c11/z_ping.c) + add_example(z_pong unix/c11/z_pong.c) + endif() + endif() elseif(MSVC) - add_example(z_put windows/z_put.c) - add_example(z_pub windows/z_pub.c) - add_example(z_pub_st windows/z_pub_st.c) - add_example(z_sub windows/z_sub.c) - add_example(z_sub_st windows/z_sub_st.c) - add_example(z_pull windows/z_pull.c) - add_example(z_get windows/z_get.c) - add_example(z_queryable windows/z_queryable.c) - add_example(z_info windows/z_info.c) - add_example(z_scout windows/z_scout.c) - add_example(z_ping windows/z_ping.c) - add_example(z_pong windows/z_pong.c) + add_example(z_put windows/z_put.c) + add_example(z_pub windows/z_pub.c) + add_example(z_pub_st windows/z_pub_st.c) + add_example(z_sub windows/z_sub.c) + add_example(z_sub_st windows/z_sub_st.c) + add_example(z_pull windows/z_pull.c) + add_example(z_get windows/z_get.c) + add_example(z_queryable windows/z_queryable.c) + add_example(z_info windows/z_info.c) + add_example(z_scout windows/z_scout.c) + add_example(z_ping windows/z_ping.c) + add_example(z_pong windows/z_pong.c) endif() diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index 4db578c7e..2949298fb 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -9,17 +9,17 @@ // WARNING: for the sake of this example we are using "internal" structs and functions (starting with "_"). // Synchronisation primitives are planned to be added to the API in the future. -_z_condvar_t cond; -_z_mutex_t mutex; +z_condvar_t cond; +z_mutex_t mutex; void callback(const z_sample_t* sample, void* context) { (void)sample; (void)context; - _z_condvar_signal(&cond); + z_condvar_signal(&cond); } void drop(void* context) { (void)context; - _z_condvar_free(&cond); + z_condvar_free(&cond); } struct args_t { @@ -42,8 +42,8 @@ int main(int argc, char** argv) { "); return 1; } - _z_mutex_init(&mutex); - _z_condvar_init(&cond); + z_mutex_init(&mutex); + z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); if (!z_check(session)) { @@ -76,20 +76,20 @@ int main(int argc, char** argv) { for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; } - _z_mutex_lock(&mutex); + z_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000; for (clock_t now = clock(); now < warmup_end; now = clock()) { z_publisher_put(z_loan(pub), data, args.size, NULL); - _z_condvar_wait(&cond, &mutex); + z_condvar_wait(&cond, &mutex); } } clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { clock_t start = clock(); z_publisher_put(z_loan(pub), data, args.size, NULL); - _z_condvar_wait(&cond, &mutex); + z_condvar_wait(&cond, &mutex); clock_t end = clock(); results[i] = end - start; } @@ -97,7 +97,7 @@ int main(int argc, char** argv) { clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC; printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2); } - _z_mutex_unlock(&mutex); + z_mutex_unlock(&mutex); z_free(results); z_free(data); z_drop(z_move(pub)); diff --git a/examples/unix/c11/z_pub_ualloc.c b/examples/unix/c11/z_pub_ualloc.c new file mode 100644 index 000000000..d046fffc1 --- /dev/null +++ b/examples/unix/c11/z_pub_ualloc.c @@ -0,0 +1,199 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include +#include +#include +#include +#include +#include +#include + +// First Fit custom / user defined allocators +#define WORD uint64_t // FIXME: not true for other architectues like 32-bits architectures +#define VAS_SIZE 1 * 1024 * 1024 // Virtual address space in bytes + +typedef struct z_m_block_hdr_t { + size_t size; // Block size + struct z_m_block_t *next; // The next block in the list + _Bool is_used; // Whether this block is currently being used +} z_m_block_hdr_t; + +typedef struct z_m_block_t { + z_m_block_hdr_t hdr; + WORD data[1]; // Payload pointer. Note: this MUST always be the last member in the struct. +} z_m_block_t; + +z_mutex_t mut; +void *z_heap_start = NULL; // Heap start. It must be explicitly initialized +void *z_heap_end = NULL; // Heap end. Defined for convenience to avoid computing + // it multiple times +z_m_block_t *p_brk = NULL; // Program break identifying the top of our local heap + +void *z_minit(size_t size) { + z_heap_start = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + if (z_heap_start == MAP_FAILED) { + z_heap_start = NULL; + z_heap_end = NULL; + } else { + z_heap_end = z_heap_start + (size * sizeof(uint8_t)); + } + + z_m_block_t *new_block = z_heap_start; + if (((uint8_t *)new_block + (sizeof(z_m_block_hdr_t) + 0)) <= (uint8_t *)z_heap_end) { + *new_block = (z_m_block_t){.hdr = {.next = NULL, .is_used = true, .size = 0}}; + p_brk = (z_m_block_t *)((uint8_t *)new_block->data + new_block->hdr.size); + } + + return z_heap_start; +} + +void z_muninit(void *start, size_t size) { munmap(start, size); } + +// Align the size to the word size. +size_t z_malign(size_t size) { + size_t aligned_size = (size + sizeof(WORD) - 1) & ~(sizeof(WORD) - 1); + return aligned_size; +} + +// Allocates a block of memory of size bytes, following a first fit strategy. +// Due to allignment extra bytes might be allocated. +void *z_malloc(size_t size) { + z_mutex_lock(&mut); + WORD *ret = NULL; + size_t aligned_size = z_malign(size); + + z_m_block_t *block = z_heap_start; + _Bool found = false; + while (block->hdr.next != NULL) { + block = block->hdr.next; + if (block->hdr.is_used == false && block->hdr.size >= aligned_size) { + found = true; + break; + } + } + + if (found == true) { + ret = block->data; + block->hdr.is_used = true; + } else { + z_m_block_t *new_block = p_brk; + if (((uint8_t *)new_block + (sizeof(z_m_block_hdr_t) + aligned_size)) <= (uint8_t *)z_heap_end) { + *new_block = (z_m_block_t){.hdr = {.next = NULL, .is_used = true, .size = aligned_size}}; + ret = new_block->data; + block->hdr.next = new_block; + p_brk = (z_m_block_t *)((uint8_t *)new_block->data + new_block->hdr.size); + } + } + z_mutex_unlock(&mut); + + return ret; +} + +void z_free(void *ptr) { + z_mutex_lock(&mut); + z_m_block_t *block = ptr - sizeof(z_m_block_hdr_t); + block->hdr.is_used = false; + z_mutex_unlock(&mut); +} +// + +int main(int argc, char **argv) { + if (z_minit(VAS_SIZE) == NULL) { + printf("Failed to reserve memory\n"); + } + + const char *keyexpr = "demo/example/zenoh-pico-pub"; + const char *value = "Pub from Pico!"; + const char *mode = "client"; + char *locator = NULL; + + int opt; + while ((opt = getopt(argc, argv, "k:v:e:m:")) != -1) { + switch (opt) { + case 'k': + keyexpr = optarg; + break; + case 'v': + value = optarg; + break; + case 'e': + locator = optarg; + break; + case 'm': + mode = optarg; + break; + case '?': + if (optopt == 'k' || optopt == 'v' || optopt == 'e' || optopt == 'm') { + fprintf(stderr, "Option -%c requires an argument.\n", optopt); + } else { + fprintf(stderr, "Unknown option `-%c'.\n", optopt); + } + return 1; + default: + return -1; + } + } + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + printf("Declaring publisher for '%s'...\n", keyexpr); + z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(keyexpr), NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + char *buf = (char *)malloc(256); + for (int idx = 0; 1; ++idx) { + sleep(1); + snprintf(buf, 256, "[%4d] %s", idx, value); + printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + + z_publisher_put_options_t options = z_publisher_put_options_default(); + options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + } + + z_undeclare_publisher(z_move(pub)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + + free(buf); + + z_muninit(z_heap_start, VAS_SIZE); + + return 0; +} diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 379afd986..7834bb9ca 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -8,17 +8,17 @@ #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/system/platform.h" -_z_condvar_t cond; -_z_mutex_t mutex; +z_condvar_t cond; +z_mutex_t mutex; void callback(const z_sample_t* sample, void* context) { (void)sample; (void)context; - _z_condvar_signal(&cond); + z_condvar_signal(&cond); } void drop(void* context) { (void)context; - _z_condvar_free(&cond); + z_condvar_free(&cond); } struct args_t { @@ -41,8 +41,8 @@ int main(int argc, char** argv) { "); return 1; } - _z_mutex_init(&mutex); - _z_condvar_init(&cond); + z_mutex_init(&mutex); + z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_config_move(&config)); if (!z_session_check(&session)) { @@ -76,20 +76,20 @@ int main(int argc, char** argv) { for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; } - _z_mutex_lock(&mutex); + z_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000; for (clock_t now = clock(); now < warmup_end; now = clock()) { z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); - _z_condvar_wait(&cond, &mutex); + z_condvar_wait(&cond, &mutex); } } clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { clock_t start = clock(); z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); - _z_condvar_wait(&cond, &mutex); + z_condvar_wait(&cond, &mutex); clock_t end = clock(); results[i] = end - start; } @@ -97,7 +97,7 @@ int main(int argc, char** argv) { clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC; printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2); } - _z_mutex_unlock(&mutex); + z_mutex_unlock(&mutex); z_free(results); z_free(data); z_undeclare_subscriber(z_subscriber_move(&sub)); diff --git a/examples/unix/c99/z_pub_ualloc.c b/examples/unix/c99/z_pub_ualloc.c new file mode 100644 index 000000000..b09c81a8e --- /dev/null +++ b/examples/unix/c99/z_pub_ualloc.c @@ -0,0 +1,198 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include +#include +#include +#include +#include +#include +#include + +// First Fit custom / user defined allocators +#define WORD uint64_t // FIXME: not true for other architectues like 32-bits architectures +#define VAS_SIZE 1 * 1024 * 1024 // Virtual address space in bytes + +typedef struct z_m_block_hdr_t { + size_t size; // Block size + struct z_m_block_t *next; // The next block in the list + _Bool is_used; // Whether this block is currently being used +} z_m_block_hdr_t; + +typedef struct z_m_block_t { + z_m_block_hdr_t hdr; + WORD data[1]; // Payload pointer. Note: this MUST always be the last member in the struct. +} z_m_block_t; + +z_mutex_t mut; +void *z_heap_start = NULL; // Heap start. It must be explicitly initialized +void *z_heap_end = NULL; // Heap end. Defined for convenience to avoid computing + // it multiple times +z_m_block_t *p_brk = NULL; // Program break identifying the top of our local heap + +void *z_minit(size_t size) { + z_heap_start = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + if (z_heap_start == MAP_FAILED) { + z_heap_start = NULL; + z_heap_end = NULL; + } else { + z_heap_end = z_heap_start + (size * sizeof(uint8_t)); + } + + z_m_block_t *new_block = z_heap_start; + if (((uint8_t *)new_block + (sizeof(z_m_block_hdr_t) + 0)) <= (uint8_t *)z_heap_end) { + *new_block = (z_m_block_t){.hdr = {.next = NULL, .is_used = true, .size = 0}}; + p_brk = (z_m_block_t *)((uint8_t *)new_block->data + new_block->hdr.size); + } + + return z_heap_start; +} + +void z_muninit(void *start, size_t size) { munmap(start, size); } + +// Align the size to the word size. +size_t z_malign(size_t size) { + size_t aligned_size = (size + sizeof(WORD) - 1) & ~(sizeof(WORD) - 1); + return aligned_size; +} + +// Allocates a block of memory of size bytes, following a first fit strategy. +// Due to allignment extra bytes might be allocated. +void *z_malloc(size_t size) { + z_mutex_lock(&mut); + WORD *ret = NULL; + size_t aligned_size = z_malign(size); + + z_m_block_t *block = z_heap_start; + _Bool found = false; + while (block->hdr.next != NULL) { + block = block->hdr.next; + if (block->hdr.is_used == false && block->hdr.size >= aligned_size) { + found = true; + break; + } + } + + if (found == true) { + ret = block->data; + block->hdr.is_used = true; + } else { + z_m_block_t *new_block = p_brk; + if (((uint8_t *)new_block + (sizeof(z_m_block_hdr_t) + aligned_size)) <= (uint8_t *)z_heap_end) { + *new_block = (z_m_block_t){.hdr = {.next = NULL, .is_used = true, .size = aligned_size}}; + ret = new_block->data; + block->hdr.next = new_block; + p_brk = (z_m_block_t *)((uint8_t *)new_block->data + new_block->hdr.size); + } + } + z_mutex_unlock(&mut); + + return ret; +} + +void z_free(void *ptr) { + z_mutex_lock(&mut); + z_m_block_t *block = ptr - sizeof(z_m_block_hdr_t); + block->hdr.is_used = false; + z_mutex_unlock(&mut); +} +// + +int main(int argc, char **argv) { + if (z_minit(VAS_SIZE) == NULL) { + printf("Failed to reserve memory\n"); + } + + const char *keyexpr = "demo/example/zenoh-pico-pub"; + const char *value = "Pub from Pico!"; + const char *mode = "client"; + char *locator = NULL; + + int opt; + while ((opt = getopt(argc, argv, "k:v:e:m:")) != -1) { + switch (opt) { + case 'k': + keyexpr = optarg; + break; + case 'v': + value = optarg; + break; + case 'e': + locator = optarg; + break; + case 'm': + mode = optarg; + break; + case '?': + if (optopt == 'k' || optopt == 'v' || optopt == 'e' || optopt == 'm') { + fprintf(stderr, "Option -%c requires an argument.\n", optopt); + } else { + fprintf(stderr, "Unknown option `-%c'.\n", optopt); + } + return 1; + default: + return -1; + } + } + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_config_move(&config)); + if (!z_session_check(&s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + printf("Declaring publisher for '%s'...\n", keyexpr); + z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&s), z_keyexpr(keyexpr), NULL); + if (!z_publisher_check(&pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + char *buf = (char *)malloc(256); + for (int idx = 0; 1; ++idx) { + sleep(1); + snprintf(buf, 256, "[%4d] %s", idx, value); + printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + z_publisher_put_options_t options = z_publisher_put_options_default(); + options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), &options); + } + + z_undeclare_publisher(z_publisher_move(&pub)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_session_loan(&s)); + zp_stop_lease_task(z_session_loan(&s)); + + z_close(z_session_move(&s)); + + free(buf); + + z_muninit(z_heap_start, VAS_SIZE); + + return 0; +} diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index fe25ed3c9..4c9c7bbfa 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -7,17 +7,17 @@ #include "zenoh-pico.h" #include "zenoh-pico/system/platform.h" -_z_condvar_t cond; -_z_mutex_t mutex; +z_condvar_t cond; +z_mutex_t mutex; void callback(const z_sample_t* sample, void* context) { (void)sample; (void)context; - _z_condvar_signal(&cond); + z_condvar_signal(&cond); } void drop(void* context) { (void)context; - _z_condvar_free(&cond); + z_condvar_free(&cond); } struct args_t { @@ -40,8 +40,8 @@ int main(int argc, char** argv) { "); return 1; } - _z_mutex_init(&mutex); - _z_condvar_init(&cond); + z_mutex_init(&mutex); + z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); if (!z_check(session)) { @@ -73,20 +73,20 @@ int main(int argc, char** argv) { for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; } - _z_mutex_lock(&mutex); + z_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000; for (clock_t now = clock(); now < warmup_end; now = clock()) { z_publisher_put(z_loan(pub), data, args.size, NULL); - _z_condvar_wait(&cond, &mutex); + z_condvar_wait(&cond, &mutex); } } clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { clock_t start = clock(); z_publisher_put(z_loan(pub), data, args.size, NULL); - _z_condvar_wait(&cond, &mutex); + z_condvar_wait(&cond, &mutex); clock_t end = clock(); results[i] = end - start; } @@ -94,7 +94,7 @@ int main(int argc, char** argv) { clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC; printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2); } - _z_mutex_unlock(&mutex); + z_mutex_unlock(&mutex); z_free(results); z_free(data); z_drop(z_move(pub)); diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 2bbb9abc2..62b9f40e2 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -271,4 +271,8 @@ #endif #endif +#ifndef Z_CUSTOM_ALLOCATOR +#define Z_CUSTOM_ALLOCATOR 0 +#endif + #endif /* INCLUDE_ZENOH_PICO_CONFIG_H */ diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 19c90adee..61724cef5 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -29,7 +29,7 @@ */ typedef struct { #if Z_MULTI_THREAD == 1 - _z_mutex_t _mutex_inner; + z_mutex_t _mutex_inner; #endif // Z_MULTI_THREAD == 1 // Zenoh-pico is considering a single transport per session. diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 8b394e844..329471ba0 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -171,8 +171,8 @@ _Z_LIST_DEFINE(_z_pending_query, _z_pending_query_t) typedef struct { #if Z_MULTI_THREAD == 1 - _z_mutex_t _mutex; - _z_condvar_t _cond_var; + z_mutex_t _mutex; + z_condvar_t _cond_var; #endif // Z_MULTI_THREAD == 1 _z_reply_data_list_t *_replies; } _z_pending_query_collect_t; diff --git a/include/zenoh-pico/system/platform.h b/include/zenoh-pico/system/platform.h index 07e1f009e..b5329f264 100644 --- a/include/zenoh-pico/system/platform.h +++ b/include/zenoh-pico/system/platform.h @@ -58,25 +58,25 @@ void z_free(void *ptr); #if Z_MULTI_THREAD == 1 /*------------------ Thread ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg); -int8_t _z_task_join(_z_task_t *task); -int8_t _z_task_cancel(_z_task_t *task); -void _z_task_free(_z_task_t **task); +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); +int8_t _z_task_join(z_task_t *task); +int8_t _z_task_cancel(z_task_t *task); +void _z_task_free(z_task_t **task); /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m); -int8_t _z_mutex_free(_z_mutex_t *m); +int8_t z_mutex_init(z_mutex_t *m); +int8_t z_mutex_free(z_mutex_t *m); -int8_t _z_mutex_lock(_z_mutex_t *m); -int8_t _z_mutex_trylock(_z_mutex_t *m); -int8_t _z_mutex_unlock(_z_mutex_t *m); +int8_t z_mutex_lock(z_mutex_t *m); +int8_t z_mutex_trylock(z_mutex_t *m); +int8_t z_mutex_unlock(z_mutex_t *m); /*------------------ CondVar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv); -int8_t _z_condvar_free(_z_condvar_t *cv); +int8_t z_condvar_init(z_condvar_t *cv); +int8_t z_condvar_free(z_condvar_t *cv); -int8_t _z_condvar_signal(_z_condvar_t *cv); -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m); +int8_t z_condvar_signal(z_condvar_t *cv); +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m); #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/include/zenoh-pico/system/platform/arduino/esp32.h b/include/zenoh-pico/system/platform/arduino/esp32.h index bac42aa72..c998e66f7 100644 --- a/include/zenoh-pico/system/platform/arduino/esp32.h +++ b/include/zenoh-pico/system/platform/arduino/esp32.h @@ -24,10 +24,10 @@ #endif // Z_MULTI_THREAD == 1 #if Z_MULTI_THREAD == 1 -typedef TaskHandle_t _z_task_t; -typedef void *_z_task_attr_t; // Not used in ESP32 -typedef pthread_mutex_t _z_mutex_t; -typedef pthread_cond_t _z_condvar_t; +typedef TaskHandle_t z_task_t; +typedef void *z_task_attr_t; // Not used in ESP32 +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/arduino/opencr.h b/include/zenoh-pico/system/platform/arduino/opencr.h index 23c43f222..2b980366f 100644 --- a/include/zenoh-pico/system/platform/arduino/opencr.h +++ b/include/zenoh-pico/system/platform/arduino/opencr.h @@ -21,10 +21,10 @@ #include "zenoh-pico/config.h" #if Z_MULTI_THREAD == 1 -typedef void *_z_task_t; -typedef void *_z_task_attr_t; -typedef void *_z_mutex_t; -typedef void *_z_condvar_t; +typedef void *z_task_t; +typedef void *z_task_attr_t; +typedef void *z_mutex_t; +typedef void *z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/emscripten.h b/include/zenoh-pico/system/platform/emscripten.h index a2f340921..70756ca33 100644 --- a/include/zenoh-pico/system/platform/emscripten.h +++ b/include/zenoh-pico/system/platform/emscripten.h @@ -21,10 +21,10 @@ #include "zenoh-pico/config.h" #if Z_MULTI_THREAD == 1 -typedef pthread_t _z_task_t; -typedef pthread_attr_t _z_task_attr_t; -typedef pthread_mutex_t _z_mutex_t; -typedef pthread_cond_t _z_condvar_t; +typedef pthread_t z_task_t; +typedef pthread_attr_t z_task_attr_t; +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef double z_clock_t; diff --git a/include/zenoh-pico/system/platform/espidf.h b/include/zenoh-pico/system/platform/espidf.h index 6c35dd1f0..2d296fc4b 100644 --- a/include/zenoh-pico/system/platform/espidf.h +++ b/include/zenoh-pico/system/platform/espidf.h @@ -23,10 +23,10 @@ #include "zenoh-pico/config.h" #if Z_MULTI_THREAD == 1 -typedef TaskHandle_t _z_task_t; -typedef void *_z_task_attr_t; // Not used in ESP32 -typedef pthread_mutex_t _z_mutex_t; -typedef pthread_cond_t _z_condvar_t; +typedef TaskHandle_t z_task_t; +typedef void *z_task_attr_t; // Not used in ESP32 +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/mbed.h b/include/zenoh-pico/system/platform/mbed.h index ca10d0459..10f95e7e9 100644 --- a/include/zenoh-pico/system/platform/mbed.h +++ b/include/zenoh-pico/system/platform/mbed.h @@ -23,11 +23,11 @@ typedef int _z_socket_t; #if Z_MULTI_THREAD == 1 -typedef void *_z_task_t; // Workaround as MBED is a C++ library -typedef void *_z_task_attr_t; // Workaround as MBED is a C++ library -typedef void *_z_mutex_t; // Workaround as MBED is a C++ library -typedef void *_z_condvar_t; // Workaround as MBED is a C++ library -#endif // Z_MULTI_THREAD == 1 +typedef void *z_task_t; // Workaround as MBED is a C++ library +typedef void *z_task_attr_t; // Workaround as MBED is a C++ library +typedef void *z_mutex_t; // Workaround as MBED is a C++ library +typedef void *z_condvar_t; // Workaround as MBED is a C++ library +#endif // Z_MULTI_THREAD == 1 typedef void *z_clock_t; // Not defined typedef struct timeval z_time_t; diff --git a/include/zenoh-pico/system/platform/unix.h b/include/zenoh-pico/system/platform/unix.h index 3faa48333..549f88d9e 100644 --- a/include/zenoh-pico/system/platform/unix.h +++ b/include/zenoh-pico/system/platform/unix.h @@ -24,10 +24,10 @@ #include "zenoh-pico/config.h" #if Z_MULTI_THREAD == 1 -typedef pthread_t _z_task_t; -typedef pthread_attr_t _z_task_attr_t; -typedef pthread_mutex_t _z_mutex_t; -typedef pthread_cond_t _z_condvar_t; +typedef pthread_t z_task_t; +typedef pthread_attr_t z_task_attr_t; +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/system/platform/void.h b/include/zenoh-pico/system/platform/void.h index 26197d375..e555aaa3c 100644 --- a/include/zenoh-pico/system/platform/void.h +++ b/include/zenoh-pico/system/platform/void.h @@ -18,10 +18,10 @@ #include "zenoh-pico/config.h" #if Z_MULTI_THREAD == 1 -typedef void *_z_task_t; -typedef void *_z_task_attr_t; -typedef void *_z_mutex_t; -typedef void *_z_condvar_t; +typedef void *z_task_t; +typedef void *z_task_attr_t; +typedef void *z_mutex_t; +typedef void *z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef void *z_clock_t; diff --git a/include/zenoh-pico/system/platform/windows.h b/include/zenoh-pico/system/platform/windows.h index f7c7d1ccb..858dcf5b5 100644 --- a/include/zenoh-pico/system/platform/windows.h +++ b/include/zenoh-pico/system/platform/windows.h @@ -21,10 +21,10 @@ #include "zenoh-pico/config.h" #if Z_MULTI_THREAD == 1 -typedef HANDLE *_z_task_t; -typedef void *_z_task_attr_t; // Not used in Windows -typedef SRWLOCK _z_mutex_t; -typedef CONDITION_VARIABLE _z_condvar_t; +typedef HANDLE *z_task_t; +typedef void *z_task_attr_t; // Not used in Windows +typedef SRWLOCK z_mutex_t; +typedef CONDITION_VARIABLE z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef struct timeb z_clock_t; diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index 4b318c9d9..07d895f93 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -31,10 +31,10 @@ #include "zenoh-pico/config.h" #if Z_MULTI_THREAD == 1 -typedef pthread_t _z_task_t; -typedef pthread_attr_t _z_task_attr_t; -typedef pthread_mutex_t _z_mutex_t; -typedef pthread_cond_t _z_condvar_t; +typedef pthread_t z_task_t; +typedef pthread_attr_t z_task_attr_t; +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef struct timespec z_clock_t; diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 1982b1b92..8295b6b31 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -58,8 +58,8 @@ typedef struct { #if Z_MULTI_THREAD == 1 // TX and RX mutexes - _z_mutex_t _mutex_rx; - _z_mutex_t _mutex_tx; + z_mutex_t _mutex_rx; + z_mutex_t _mutex_tx; #endif // Z_MULTI_THREAD == 1 _z_link_t _link; @@ -83,8 +83,8 @@ typedef struct { void *_session; #if Z_MULTI_THREAD == 1 - _z_task_t *_read_task; - _z_task_t *_lease_task; + z_task_t *_read_task; + z_task_t *_lease_task; volatile _Bool _read_task_running; volatile _Bool _lease_task_running; #endif // Z_MULTI_THREAD == 1 @@ -99,11 +99,11 @@ typedef struct { #if Z_MULTI_THREAD == 1 // TX and RX mutexes - _z_mutex_t _mutex_rx; - _z_mutex_t _mutex_tx; + z_mutex_t _mutex_rx; + z_mutex_t _mutex_tx; // Peer list mutex - _z_mutex_t _mutex_peer; + z_mutex_t _mutex_peer; #endif // Z_MULTI_THREAD == 1 _z_link_t _link; @@ -122,8 +122,8 @@ typedef struct { _z_transport_peer_entry_list_t *_peers; #if Z_MULTI_THREAD == 1 - _z_task_t *_read_task; - _z_task_t *_lease_task; + z_task_t *_read_task; + z_task_t *_lease_task; volatile _Bool _read_task_running; volatile _Bool _lease_task_running; #endif // Z_MULTI_THREAD == 1 diff --git a/src/net/session.c b/src/net/session.c index 2f2b0c93e..29a19c49a 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -191,9 +191,9 @@ int8_t _zp_send_join(_z_session_t *zn) { return _z_send_join(&zn->_tp); } int8_t _zp_start_read_task(_z_session_t *zn) { int8_t ret = _Z_RES_OK; - _z_task_t *task = (_z_task_t *)z_malloc(sizeof(_z_task_t)); + z_task_t *task = (z_task_t *)z_malloc(sizeof(z_task_t)); if (task != NULL) { - (void)memset(task, 0, sizeof(_z_task_t)); + (void)memset(task, 0, sizeof(z_task_t)); #if Z_UNICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { @@ -249,9 +249,9 @@ int8_t _zp_stop_read_task(_z_session_t *zn) { int8_t _zp_start_lease_task(_z_session_t *zn) { int8_t ret = _Z_RES_OK; - _z_task_t *task = (_z_task_t *)z_malloc(sizeof(_z_task_t)); + z_task_t *task = (z_task_t *)z_malloc(sizeof(z_task_t)); if (task != NULL) { - (void)memset(task, 0, sizeof(_z_task_t)); + (void)memset(task, 0, sizeof(z_task_t)); #if Z_UNICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { diff --git a/src/session/query.c b/src/session/query.c index 17a8667db..dffa19a6f 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -103,13 +103,13 @@ _z_pending_query_t *__unsafe__z_get_pending_query_by_id(_z_session_t *zn, const _z_pending_query_t *_z_get_pending_query_by_id(_z_session_t *zn, const _z_zint_t id) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_pending_query_t *pql = __unsafe__z_get_pending_query_by_id(zn, id); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return pql; } @@ -121,7 +121,7 @@ int8_t _z_register_pending_query(_z_session_t *zn, _z_pending_query_t *pen_qry) pen_qry->_parameters); #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_pending_query_t *pql = __unsafe__z_get_pending_query_by_id(zn, pen_qry->_id); @@ -132,7 +132,7 @@ int8_t _z_register_pending_query(_z_session_t *zn, _z_pending_query_t *pen_qry) } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return ret; @@ -144,7 +144,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, id); @@ -216,7 +216,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 // Trigger the user callback @@ -235,7 +235,7 @@ int8_t _z_trigger_query_reply_final(_z_session_t *zn, _z_zint_t id) { int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 // Final reply received for unknown query id @@ -261,7 +261,7 @@ int8_t _z_trigger_query_reply_final(_z_session_t *zn, _z_zint_t id) { } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return ret; @@ -269,24 +269,24 @@ int8_t _z_trigger_query_reply_final(_z_session_t *zn, _z_zint_t id) { void _z_unregister_pending_query(_z_session_t *zn, _z_pending_query_t *pen_qry) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 zn->_pending_queries = _z_pending_query_list_drop_filter(zn->_pending_queries, _z_pending_query_eq, pen_qry); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } void _z_flush_pending_queries(_z_session_t *zn) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_pending_query_list_free(&zn->_pending_queries); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } diff --git a/src/session/queryable.c b/src/session/queryable.c index d18c5705e..3240893ac 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -90,13 +90,13 @@ _z_questionable_sptr_list_t *__unsafe_z_get_questionable_by_key(_z_session_t *zn _z_questionable_sptr_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zint_t id) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_questionable_sptr_t *qle = __unsafe_z_get_questionable_by_id(zn, id); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return qle; @@ -104,14 +104,14 @@ _z_questionable_sptr_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zin _z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, keyexpr); _z_questionable_sptr_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return qles; @@ -122,7 +122,7 @@ _z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionab _z_questionable_sptr_t *ret = NULL; #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 ret = (_z_questionable_sptr_t *)z_malloc(sizeof(_z_questionable_sptr_t)); @@ -132,7 +132,7 @@ _z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionab } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return ret; @@ -142,7 +142,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, &q_key); @@ -150,7 +150,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons _z_questionable_sptr_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 // Build the query @@ -191,7 +191,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons _z_msg_clear(&z_msg); } else { #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 ret = _Z_ERR_KEYEXPR_UNKNOWN; @@ -202,25 +202,25 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons void _z_unregister_questionable(_z_session_t *zn, _z_questionable_sptr_t *qle) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 zn->_local_questionable = _z_questionable_sptr_list_drop_filter(zn->_local_questionable, _z_questionable_sptr_eq, qle); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } void _z_flush_questionables(_z_session_t *zn) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_questionable_sptr_list_free(&zn->_local_questionable); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } diff --git a/src/session/resource.c b/src/session/resource.c index d997b5b4b..f78467e0e 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -175,13 +175,13 @@ _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_key _z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint_t rid) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_resource_t *res = __unsafe_z_get_resource_by_id(zn, mapping, rid); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return res; @@ -192,13 +192,13 @@ _z_resource_t *_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keye return _z_get_resource_by_id(zn, _z_keyexpr_mapping_id(keyexpr), keyexpr->_id); } #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_resource_t *res = __unsafe_z_get_resource_by_key(zn, keyexpr); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return res; @@ -206,12 +206,12 @@ _z_resource_t *_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keye _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_keyexpr_t res = __unsafe_z_get_expanded_key_from_key(zn, keyexpr); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return res; @@ -225,7 +225,7 @@ int16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, ui uint16_t parent_mapping = _z_keyexpr_mapping_id(&key); #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 if (key._id != Z_RESOURCE_ID_NONE) { @@ -256,7 +256,7 @@ int16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, ui } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return ret; @@ -266,7 +266,7 @@ void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping) { _Bool is_local = mapping == _Z_KEYEXPR_MAPPING_LOCAL; _Z_DEBUG("unregistering: id %d, mapping: %d\n", id, mapping); #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_resource_list_t **parent_mut = is_local ? &zn->_local_resources : &zn->_remote_resources; while (id != 0) { @@ -307,19 +307,19 @@ void _z_unregister_resources_for_peer(_z_session_t *zn, uint16_t mapping) { _z_resource_list_drop_filter(zn->_remote_resources, _z_unregister_resource_for_peer_filter, &ctx); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } void _z_flush_resources(_z_session_t *zn) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_resource_list_free(&zn->_local_resources); _z_resource_list_free(&zn->_remote_resources); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } diff --git a/src/session/subscription.c b/src/session/subscription.c index fd057aac3..f9c48bc74 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -97,13 +97,13 @@ _z_subscription_sptr_list_t *__unsafe_z_get_subscriptions_by_key(_z_session_t *z _z_subscription_sptr_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_subscription_sptr_t *sub = __unsafe_z_get_subscription_by_id(zn, is_local, id); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return sub; @@ -111,13 +111,13 @@ _z_subscription_sptr_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_l _z_subscription_sptr_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *key) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, is_local, *key); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return subs; @@ -128,7 +128,7 @@ _z_subscription_sptr_t *_z_register_subscription(_z_session_t *zn, uint8_t is_lo _z_subscription_sptr_t *ret = NULL; #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, is_local, s->_key); @@ -145,7 +145,7 @@ _z_subscription_sptr_t *_z_register_subscription(_z_session_t *zn, uint8_t is_lo } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 return ret; @@ -156,7 +156,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _Z_DEBUG("Resolving %d - %s on mapping 0x%x\n", keyexpr._id, keyexpr._suffix, _z_keyexpr_mapping_id(&keyexpr)); @@ -166,7 +166,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, _Z_RESOURCE_IS_LOCAL, key); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 // Build the sample @@ -188,7 +188,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _z_subscription_sptr_list_free(&subs); } else { #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 ret = _Z_ERR_KEYEXPR_UNKNOWN; } @@ -198,7 +198,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_sptr_t *sub) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 if (is_local == _Z_RESOURCE_IS_LOCAL) { @@ -210,19 +210,19 @@ void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscript } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } void _z_flush_subscriptions(_z_session_t *zn) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&zn->_mutex_inner); + z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_subscription_sptr_list_free(&zn->_local_subscriptions); _z_subscription_sptr_list_free(&zn->_remote_subscriptions); #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&zn->_mutex_inner); + z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } diff --git a/src/session/utils.c b/src/session/utils.c index 215c2717f..370fcffaf 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -67,7 +67,7 @@ int8_t _z_session_init(_z_session_t *zn, _z_id_t *zid) { zn->_pending_queries = NULL; #if Z_MULTI_THREAD == 1 - ret = _z_mutex_init(&zn->_mutex_inner); + ret = z_mutex_init(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 if (ret == _Z_RES_OK) { zn->_local_zid = *zid; @@ -104,7 +104,7 @@ void _z_session_clear(_z_session_t *zn) { _z_flush_pending_queries(zn); #if Z_MULTI_THREAD == 1 - _z_mutex_free(&zn->_mutex_inner); + z_mutex_free(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 } diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index 1d823c14c..70f921036 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -60,7 +60,7 @@ void z_task_wrapper(__z_task_arg *targ) { } /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { int ret = 0; __z_task_arg *z_arg = (__z_task_arg *)z_malloc(sizeof(__z_task_arg)); @@ -77,41 +77,41 @@ int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), return ret; } -int8_t _z_task_join(_z_task_t *task) { +int8_t _z_task_join(z_task_t *task) { // Note: task/thread join not supported on FreeRTOS API, so we force its deletion instead. return _z_task_cancel(task); } -int8_t _z_task_cancel(_z_task_t *task) { +int8_t _z_task_cancel(z_task_t *task) { vTaskDelete(*task); return 0; } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, NULL); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, NULL); } -int8_t _z_mutex_free(_z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t _z_mutex_trylock(_z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } -int8_t _z_condvar_free(_z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 946392bb3..777775400 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -63,37 +63,37 @@ void z_free(void *ptr) { #error "Multi-threading not supported yet on OpenCR port. Disable it by defining Z_MULTI_THREAD=0" /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { return -1; } +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { return -1; } -int8_t _z_task_join(_z_task_t *task) { return -1; } +int8_t _z_task_join(z_task_t *task) { return -1; } -int8_t _z_task_cancel(_z_task_t *task) { return -1; } +int8_t _z_task_cancel(z_task_t *task) { return -1; } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { return -1; } +int8_t z_mutex_init(z_mutex_t *m) { return -1; } -int8_t _z_mutex_free(_z_mutex_t *m) { return -1; } +int8_t z_mutex_free(z_mutex_t *m) { return -1; } -int8_t _z_mutex_lock(_z_mutex_t *m) { return -1; } +int8_t z_mutex_lock(z_mutex_t *m) { return -1; } -int8_t _z_mutex_trylock(_z_mutex_t *m) { return -1; } +int8_t z_mutex_trylock(z_mutex_t *m) { return -1; } -int8_t _z_mutex_unlock(_z_mutex_t *m) { return -1; } +int8_t z_mutex_unlock(z_mutex_t *m) { return -1; } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { return -1; } +int8_t z_condvar_init(z_condvar_t *cv) { return -1; } -int8_t _z_condvar_free(_z_condvar_t *cv) { return -1; } +int8_t z_condvar_free(z_condvar_t *cv) { return -1; } -int8_t _z_condvar_signal(_z_condvar_t *cv) { return -1; } +int8_t z_condvar_signal(z_condvar_t *cv) { return -1; } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return -1; } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return -1; } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index 89242bd72..5636dcc03 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -44,39 +44,39 @@ void z_free(void *ptr) { free(ptr); } #if Z_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, pthread_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(z_task_t *task, pthread_attr_t *attr, void *(*fun)(void *), void *arg) { return pthread_create(task, attr, fun, arg); } -int8_t _z_task_join(_z_task_t *task) { return pthread_join(*task, NULL); } +int8_t _z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } -int8_t _z_task_cancel(_z_task_t *task) { return pthread_cancel(*task); } +int8_t _z_task_cancel(z_task_t *task) { return pthread_cancel(*task); } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } -int8_t _z_mutex_free(_z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t _z_mutex_trylock(_z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, 0); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } -int8_t _z_condvar_free(_z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index a3a4dd472..b997745a1 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -61,7 +61,7 @@ void z_task_wrapper(__z_task_arg *targ) { } /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { int ret = 0; __z_task_arg *z_arg = (__z_task_arg *)z_malloc(sizeof(__z_task_arg)); @@ -78,41 +78,41 @@ int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), return ret; } -int8_t _z_task_join(_z_task_t *task) { +int8_t _z_task_join(z_task_t *task) { // Note: task/thread join not supported on FreeRTOS API, so we force its deletion instead. return _z_task_cancel(task); } -int8_t _z_task_cancel(_z_task_t *task) { +int8_t _z_task_cancel(z_task_t *task) { vTaskDelete(*task); return 0; } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, NULL); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, NULL); } -int8_t _z_mutex_free(_z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t _z_mutex_trylock(_z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } -int8_t _z_condvar_free(_z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index 68dc20b01..8c4c01f32 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -41,67 +41,67 @@ void z_free(void *ptr) { free(ptr); } #if Z_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { *task = new Thread(); mbed::Callback c = mbed::Callback(fun, arg); return ((Thread *)*task)->start(c); } -int8_t _z_task_join(_z_task_t *task) { +int8_t _z_task_join(z_task_t *task) { int res = ((Thread *)*task)->join(); delete ((Thread *)*task); return res; } -int8_t _z_task_cancel(_z_task_t *task) { +int8_t _z_task_cancel(z_task_t *task) { int res = ((Thread *)*task)->terminate(); delete ((Thread *)*task); return res; } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { +int8_t z_mutex_init(z_mutex_t *m) { *m = new Mutex(); return 0; } -int8_t _z_mutex_free(_z_mutex_t *m) { +int8_t z_mutex_free(z_mutex_t *m) { delete ((Mutex *)*m); return 0; } -int8_t _z_mutex_lock(_z_mutex_t *m) { +int8_t z_mutex_lock(z_mutex_t *m) { ((Mutex *)*m)->lock(); return 0; } -int8_t _z_mutex_trylock(_z_mutex_t *m) { return (((Mutex *)*m)->trylock() == true) ? 0 : -1; } +int8_t z_mutex_trylock(z_mutex_t *m) { return (((Mutex *)*m)->trylock() == true) ? 0 : -1; } -int8_t _z_mutex_unlock(_z_mutex_t *m) { +int8_t z_mutex_unlock(z_mutex_t *m) { ((Mutex *)*m)->unlock(); return 0; } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { return 0; } +int8_t z_condvar_init(z_condvar_t *cv) { return 0; } -int8_t _z_condvar_free(_z_condvar_t *cv) { +int8_t z_condvar_free(z_condvar_t *cv) { delete ((ConditionVariable *)*cv); return 0; } -int8_t _z_condvar_signal(_z_condvar_t *cv) { +int8_t z_condvar_signal(z_condvar_t *cv) { ((ConditionVariable *)*cv)->notify_all(); return 0; } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { *cv = new ConditionVariable(*((Mutex *)*m)); ((ConditionVariable *)*cv)->wait(); return 0; diff --git a/src/system/unix/system.c b/src/system/unix/system.c index e5dca90e9..8bb0ddc88 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -92,47 +92,49 @@ void z_random_fill(void *buf, size_t len) { } /*------------------ Memory ------------------*/ +#if Z_CUSTOM_ALLOCATOR == 0 void *z_malloc(size_t size) { return malloc(size); } void *z_realloc(void *ptr, size_t size) { return realloc(ptr, size); } void z_free(void *ptr) { free(ptr); } +#endif // Z_CUSTOM_ALLOCATOR #if Z_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { return pthread_create(task, attr, fun, arg); } -int8_t _z_task_join(_z_task_t *task) { return pthread_join(*task, NULL); } +int8_t _z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } -int8_t _z_task_cancel(_z_task_t *task) { return pthread_cancel(*task); } +int8_t _z_task_cancel(z_task_t *task) { return pthread_cancel(*task); } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } -int8_t _z_mutex_free(_z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t _z_mutex_trylock(_z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, 0); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } -int8_t _z_condvar_free(_z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/system/windows/system.c b/src/system/windows/system.c index b813d6627..6143b82cc 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -60,7 +60,7 @@ void z_free(void *ptr) { free(ptr); } #if Z_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { (void)(attr); int8_t ret = _Z_RES_OK; *task = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fun, arg, 0, NULL); @@ -70,45 +70,45 @@ int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), return ret; } -int8_t _z_task_join(_z_task_t *task) { +int8_t _z_task_join(z_task_t *task) { int8_t ret = _Z_RES_OK; WaitForSingleObject(*task, INFINITE); return ret; } -int8_t _z_task_cancel(_z_task_t *task) { +int8_t _z_task_cancel(z_task_t *task) { int8_t ret = _Z_RES_OK; TerminateThread(*task, 0); return ret; } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; CloseHandle(*ptr); z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { +int8_t z_mutex_init(z_mutex_t *m) { int8_t ret = _Z_RES_OK; InitializeSRWLock(m); return ret; } -int8_t _z_mutex_free(_z_mutex_t *m) { +int8_t z_mutex_free(z_mutex_t *m) { (void)(m); int8_t ret = _Z_RES_OK; return ret; } -int8_t _z_mutex_lock(_z_mutex_t *m) { +int8_t z_mutex_lock(z_mutex_t *m) { int8_t ret = _Z_RES_OK; AcquireSRWLockExclusive(m); return ret; } -int8_t _z_mutex_trylock(_z_mutex_t *m) { +int8_t z_mutex_trylock(z_mutex_t *m) { int8_t ret = _Z_RES_OK; if (TryAcquireSRWLockExclusive(m) == 0) { ret = _Z_ERR_GENERIC; @@ -116,32 +116,32 @@ int8_t _z_mutex_trylock(_z_mutex_t *m) { return ret; } -int8_t _z_mutex_unlock(_z_mutex_t *m) { +int8_t z_mutex_unlock(z_mutex_t *m) { int8_t ret = _Z_RES_OK; ReleaseSRWLockExclusive(m); return ret; } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { +int8_t z_condvar_init(z_condvar_t *cv) { int8_t ret = _Z_RES_OK; InitializeConditionVariable(cv); return ret; } -int8_t _z_condvar_free(_z_condvar_t *cv) { +int8_t z_condvar_free(z_condvar_t *cv) { (void)(cv); int8_t ret = _Z_RES_OK; return ret; } -int8_t _z_condvar_signal(_z_condvar_t *cv) { +int8_t z_condvar_signal(z_condvar_t *cv) { int8_t ret = _Z_RES_OK; WakeConditionVariable(cv); return ret; } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { int8_t ret = _Z_RES_OK; SleepConditionVariableSRW(cv, m, INFINITE, 0); return ret; diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 2670ae9cb..0e138c6a5 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -71,9 +71,9 @@ K_THREAD_STACK_ARRAY_DEFINE(thread_stack_area, Z_THREADS_NUM, Z_PTHREAD_STACK_SI static int thread_index = 0; /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { - _z_task_attr_t *lattr = NULL; - _z_task_attr_t tmp; +int8_t _z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { + z_task_attr_t *lattr = NULL; + z_task_attr_t tmp; if (attr == NULL) { (void)pthread_attr_init(&tmp); (void)pthread_attr_setstack(&tmp, &thread_stack_area[thread_index++], Z_PTHREAD_STACK_SIZE_DEFAULT); @@ -83,35 +83,35 @@ int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), return pthread_create(task, lattr, fun, arg); } -int8_t _z_task_join(_z_task_t *task) { return pthread_join(*task, NULL); } +int8_t _z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } -int8_t _z_task_cancel(_z_task_t *task) { return pthread_cancel(*task); } +int8_t _z_task_cancel(z_task_t *task) { return pthread_cancel(*task); } -void _z_task_free(_z_task_t **task) { - _z_task_t *ptr = *task; +void _z_task_free(z_task_t **task) { + z_task_t *ptr = *task; z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t _z_mutex_init(_z_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } -int8_t _z_mutex_free(_z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t _z_mutex_lock(_z_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t _z_mutex_trylock(_z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t _z_mutex_unlock(_z_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t _z_condvar_init(_z_condvar_t *cv) { return pthread_cond_init(cv, 0); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } -int8_t _z_condvar_free(_z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t _z_condvar_signal(_z_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index b5d50e030..77b003444 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -54,7 +54,7 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me #if Z_MULTI_THREAD == 1 // Acquire the lock - _z_mutex_lock(&ztm->_mutex_rx); + z_mutex_lock(&ztm->_mutex_rx); #endif // Z_MULTI_THREAD == 1 size_t to_read = 0; @@ -97,7 +97,7 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztm->_mutex_rx); + z_mutex_unlock(&ztm->_mutex_rx); #endif // Z_MULTI_THREAD == 1 return ret; @@ -112,7 +112,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 // Acquire and keep the lock - _z_mutex_lock(&ztm->_mutex_peer); + z_mutex_lock(&ztm->_mutex_peer); #endif // Z_MULTI_THREAD == 1 // Mark the session that we have received data from this peer @@ -314,7 +314,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztm->_mutex_peer); + z_mutex_unlock(&ztm->_mutex_peer); #endif // Z_MULTI_THREAD == 1 return ret; diff --git a/src/transport/multicast/link/task/lease.c b/src/transport/multicast/link/task/lease.c index c653d2dea..a50c9896f 100644 --- a/src/transport/multicast/link/task/lease.c +++ b/src/transport/multicast/link/task/lease.c @@ -79,7 +79,7 @@ void *_zp_multicast_lease_task(void *ztm_arg) { _z_transport_peer_entry_list_t *it = NULL; while (ztm->_lease_task_running == true) { - _z_mutex_lock(&ztm->_mutex_peer); + z_mutex_lock(&ztm->_mutex_peer); if (next_lease <= 0) { it = ztm->_peers; @@ -138,13 +138,13 @@ void *_zp_multicast_lease_task(void *ztm_arg) { } } - _z_mutex_unlock(&ztm->_mutex_peer); + z_mutex_unlock(&ztm->_mutex_peer); // The keep alive and lease intervals are expressed in milliseconds z_sleep_ms(interval); // Decrement all intervals - _z_mutex_lock(&ztm->_mutex_peer); + z_mutex_lock(&ztm->_mutex_peer); it = ztm->_peers; while (it != NULL) { @@ -156,7 +156,7 @@ void *_zp_multicast_lease_task(void *ztm_arg) { next_keep_alive = next_keep_alive - interval; next_join = next_join - interval; - _z_mutex_unlock(&ztm->_mutex_peer); + z_mutex_unlock(&ztm->_mutex_peer); } #endif // Z_MULTI_THREAD == 1 diff --git a/src/transport/multicast/link/task/read.c b/src/transport/multicast/link/task/read.c index 2c4278426..c45b73b91 100644 --- a/src/transport/multicast/link/task/read.c +++ b/src/transport/multicast/link/task/read.c @@ -43,7 +43,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; // Acquire and keep the lock - _z_mutex_lock(&ztm->_mutex_rx); + z_mutex_lock(&ztm->_mutex_rx); // Prepare the buffer _z_zbuf_reset(&ztm->_zbuf); @@ -112,7 +112,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { _z_zbuf_set_rpos(&ztm->_zbuf, _z_zbuf_get_rpos(&ztm->_zbuf) + to_read); } - _z_mutex_unlock(&ztm->_mutex_rx); + z_mutex_unlock(&ztm->_mutex_rx); #endif // Z_MULTI_THREAD == 1 return NULL; diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index 67e4e35bd..eefd1ae00 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -45,7 +45,7 @@ int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport #if Z_MULTI_THREAD == 1 // Acquire the lock - _z_mutex_lock(&ztm->_mutex_tx); + z_mutex_lock(&ztm->_mutex_tx); #endif // Z_MULTI_THREAD == 1 // Prepare the buffer eventually reserving space for the message length @@ -64,7 +64,7 @@ int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztm->_mutex_tx); + z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_MULTI_THREAD == 1 return ret; @@ -81,11 +81,11 @@ int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_m _Bool drop = false; if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&ztm->_mutex_tx); + z_mutex_lock(&ztm->_mutex_tx); #endif // Z_MULTI_THREAD == 1 } else { #if Z_MULTI_THREAD == 1 - int8_t locked = _z_mutex_trylock(&ztm->_mutex_tx); + int8_t locked = z_mutex_trylock(&ztm->_mutex_tx); if (locked != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control\n"); // We failed to acquire the lock, drop the message @@ -146,7 +146,7 @@ int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_m } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztm->_mutex_tx); + z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_MULTI_THREAD == 1 } diff --git a/src/transport/transport.c b/src/transport/transport.c index af71c2016..d474c11d7 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -80,11 +80,11 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic #if Z_MULTI_THREAD == 1 // Initialize the mutexes - ret = _z_mutex_init(&zt->_transport._unicast._mutex_tx); + ret = z_mutex_init(&zt->_transport._unicast._mutex_tx); if (ret == _Z_RES_OK) { - ret = _z_mutex_init(&zt->_transport._unicast._mutex_rx); + ret = z_mutex_init(&zt->_transport._unicast._mutex_rx); if (ret != _Z_RES_OK) { - _z_mutex_free(&zt->_transport._unicast._mutex_tx); + z_mutex_free(&zt->_transport._unicast._mutex_tx); } } #endif // Z_MULTI_THREAD == 1 @@ -119,8 +119,8 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; #if Z_MULTI_THREAD == 1 - _z_mutex_free(&zt->_transport._unicast._mutex_tx); - _z_mutex_free(&zt->_transport._unicast._mutex_rx); + z_mutex_free(&zt->_transport._unicast._mutex_tx); + z_mutex_free(&zt->_transport._unicast._mutex_rx); #endif // Z_MULTI_THREAD == 1 _z_wbuf_clear(&zt->_transport._unicast._wbuf); @@ -179,17 +179,17 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu #if Z_MULTI_THREAD == 1 // Initialize the mutexes - ret = _z_mutex_init(&zt->_transport._multicast._mutex_tx); + ret = z_mutex_init(&zt->_transport._multicast._mutex_tx); if (ret == _Z_RES_OK) { - ret = _z_mutex_init(&zt->_transport._multicast._mutex_rx); + ret = z_mutex_init(&zt->_transport._multicast._mutex_rx); if (ret == _Z_RES_OK) { - ret = _z_mutex_init(&zt->_transport._multicast._mutex_peer); + ret = z_mutex_init(&zt->_transport._multicast._mutex_peer); if (ret != _Z_RES_OK) { - _z_mutex_free(&zt->_transport._multicast._mutex_tx); - _z_mutex_free(&zt->_transport._multicast._mutex_rx); + z_mutex_free(&zt->_transport._multicast._mutex_tx); + z_mutex_free(&zt->_transport._multicast._mutex_rx); } } else { - _z_mutex_free(&zt->_transport._multicast._mutex_tx); + z_mutex_free(&zt->_transport._multicast._mutex_tx); } } #endif // Z_MULTI_THREAD == 1 @@ -206,9 +206,9 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; #if Z_MULTI_THREAD == 1 - _z_mutex_free(&zt->_transport._multicast._mutex_tx); - _z_mutex_free(&zt->_transport._multicast._mutex_rx); - _z_mutex_free(&zt->_transport._multicast._mutex_peer); + z_mutex_free(&zt->_transport._multicast._mutex_tx); + z_mutex_free(&zt->_transport._multicast._mutex_rx); + z_mutex_free(&zt->_transport._multicast._mutex_peer); #endif // Z_MULTI_THREAD == 1 _z_wbuf_clear(&zt->_transport._multicast._wbuf); @@ -432,8 +432,8 @@ void _z_transport_unicast_clear(_z_transport_unicast_t *ztu) { } // Clean up the mutexes - _z_mutex_free(&ztu->_mutex_tx); - _z_mutex_free(&ztu->_mutex_rx); + z_mutex_free(&ztu->_mutex_tx); + z_mutex_free(&ztu->_mutex_rx); #endif // Z_MULTI_THREAD == 1 // Clean up the buffers @@ -462,9 +462,9 @@ void _z_transport_multicast_clear(_z_transport_multicast_t *ztm) { } // Clean up the mutexes - _z_mutex_free(&ztm->_mutex_tx); - _z_mutex_free(&ztm->_mutex_rx); - _z_mutex_free(&ztm->_mutex_peer); + z_mutex_free(&ztm->_mutex_tx); + z_mutex_free(&ztm->_mutex_rx); + z_mutex_free(&ztm->_mutex_peer); #endif // Z_MULTI_THREAD == 1 // Clean up the buffers diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index bac48d82e..4f63ffb11 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -32,7 +32,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 // Acquire the lock - _z_mutex_lock(&ztu->_mutex_rx); + z_mutex_lock(&ztu->_mutex_rx); #endif // Z_MULTI_THREAD == 1 size_t to_read = 0; @@ -80,7 +80,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztu->_mutex_rx); + z_mutex_unlock(&ztu->_mutex_rx); #endif // Z_MULTI_THREAD == 1 return ret; diff --git a/src/transport/unicast/link/task/read.c b/src/transport/unicast/link/task/read.c index 31bc35609..ec6980ba5 100644 --- a/src/transport/unicast/link/task/read.c +++ b/src/transport/unicast/link/task/read.c @@ -41,7 +41,7 @@ void *_zp_unicast_read_task(void *ztu_arg) { _z_transport_unicast_t *ztu = (_z_transport_unicast_t *)ztu_arg; // Acquire and keep the lock - _z_mutex_lock(&ztu->_mutex_rx); + z_mutex_lock(&ztu->_mutex_rx); // Prepare the buffer _z_zbuf_reset(&ztu->_zbuf); @@ -106,7 +106,7 @@ void *_zp_unicast_read_task(void *ztu_arg) { _z_zbuf_set_rpos(&ztu->_zbuf, _z_zbuf_get_rpos(&ztu->_zbuf) + to_read); } - _z_mutex_unlock(&ztu->_mutex_rx); + z_mutex_unlock(&ztu->_mutex_rx); #endif // Z_MULTI_THREAD == 1 return NULL; diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 3375d08e9..24dd64445 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -48,7 +48,7 @@ int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_mes #if Z_MULTI_THREAD == 1 // Acquire the lock - _z_mutex_lock(&ztu->_mutex_tx); + z_mutex_lock(&ztu->_mutex_tx); #endif // Z_MULTI_THREAD == 1 // Prepare the buffer eventually reserving space for the message length @@ -67,7 +67,7 @@ int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_mes } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztu->_mutex_tx); + z_mutex_unlock(&ztu->_mutex_tx); #endif // Z_MULTI_THREAD == 1 return ret; @@ -84,11 +84,11 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg _Bool drop = false; if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { #if Z_MULTI_THREAD == 1 - _z_mutex_lock(&ztu->_mutex_tx); + z_mutex_lock(&ztu->_mutex_tx); #endif // Z_MULTI_THREAD == 1 } else { #if Z_MULTI_THREAD == 1 - int8_t locked = _z_mutex_trylock(&ztu->_mutex_tx); + int8_t locked = z_mutex_trylock(&ztu->_mutex_tx); if (locked != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control\n"); // We failed to acquire the lock, drop the message @@ -155,7 +155,7 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg } #if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztu->_mutex_tx); + z_mutex_unlock(&ztu->_mutex_tx); #endif // Z_MULTI_THREAD == 1 }