Skip to content

Commit

Permalink
feat: make all sync mechanisms public
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jan 8, 2024
1 parent 991fb93 commit 0e9b446
Show file tree
Hide file tree
Showing 52 changed files with 373 additions and 375 deletions.
4 changes: 2 additions & 2 deletions examples/freertos_plus_tcp/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void app_main(void) {
static StackType_t read_task_stack[1000];
static StaticTask_t read_task_buffer;

_z_task_attr_t read_task_attr = {
z_task_attr_t read_task_attr = {
.name = "ZenohReadTask",
.priority = 10,
.stack_depth = 1000,
Expand All @@ -62,7 +62,7 @@ void app_main(void) {
static StackType_t lease_task_stack[1000];
static StaticTask_t lease_task_buffer;

_z_task_attr_t lease_task_attr = {
z_task_attr_t lease_task_attr = {
.name = "ZenohLeaseTask",
.priority = 10,
.stack_depth = 1000,
Expand Down
22 changes: 10 additions & 12 deletions examples/unix/c11/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@
#define DEFAULT_PING_NB 100
#define DEFAULT_WARMUP_MS 1000

// 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;
static z_condvar_t cond;
static 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 {
Expand All @@ -63,8 +61,8 @@ int main(int argc, char** argv) {
DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS);
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)) {
Expand Down Expand Up @@ -97,28 +95,28 @@ 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);
z_clock_t warmup_start = z_clock_now();
unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
elapsed_us = z_clock_elapsed_us(&warmup_start);
}
}
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (unsigned int i = 0; i < args.number_of_pings; i++) {
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
results[i] = z_clock_elapsed_us(&measure_start);
}
for (unsigned int i = 0; i < args.number_of_pings; i++) {
printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2);
}
_z_mutex_unlock(&mutex);
z_mutex_unlock(&mutex);
z_free(results);
z_free(data);
z_drop(z_move(pub));
Expand Down
20 changes: 10 additions & 10 deletions examples/unix/c99/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
#define DEFAULT_PING_NB 100
#define DEFAULT_WARMUP_MS 1000

_z_condvar_t cond;
_z_mutex_t mutex;
static z_condvar_t cond;
static 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 {
Expand All @@ -62,8 +62,8 @@ int main(int argc, char** argv) {
DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS);
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)) {
Expand Down Expand Up @@ -97,28 +97,28 @@ 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);
z_clock_t warmup_start = z_clock_now();
unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
elapsed_us = z_clock_elapsed_us(&warmup_start);
}
}
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (unsigned int i = 0; i < args.number_of_pings; i++) {
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
results[i] = z_clock_elapsed_us(&measure_start);
}
for (unsigned int i = 0; i < args.number_of_pings; i++) {
printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2);
}
_z_mutex_unlock(&mutex);
z_mutex_unlock(&mutex);
z_free(results);
z_free(data);
z_undeclare_subscriber(z_subscriber_move(&sub));
Expand Down
20 changes: 10 additions & 10 deletions examples/windows/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
#define DEFAULT_PING_NB 100
#define DEFAULT_WARMUP_MS 1000

_z_condvar_t cond;
_z_mutex_t mutex;
static z_condvar_t cond;
static 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 {
Expand All @@ -61,8 +61,8 @@ int main(int argc, char** argv) {
DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS);
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)) {
Expand Down Expand Up @@ -94,28 +94,28 @@ 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);
z_clock_t warmup_start = z_clock_now();
unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
elapsed_us = z_clock_elapsed_us(&warmup_start);
}
}
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (unsigned int i = 0; i < args.number_of_pings; i++) {
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
results[i] = z_clock_elapsed_us(&measure_start);
}
for (unsigned int i = 0; i < args.number_of_pings; i++) {
printf("%d bytes: seq=%d rtt=%luus, lat=%luus\n", args.size, i, results[i], results[i] / 2);
}
_z_mutex_unlock(&mutex);
z_mutex_unlock(&mutex);
z_free(results);
z_free(data);
z_drop(z_move(pub));
Expand Down
4 changes: 2 additions & 2 deletions include/zenoh-pico/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ typedef struct {
*/
typedef struct {
#if Z_FEATURE_MULTI_THREAD == 1
_z_task_attr_t *task_attributes;
z_task_attr_t *task_attributes;
#else
uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior
#endif
Expand All @@ -356,7 +356,7 @@ typedef struct {
*/
typedef struct {
#if Z_FEATURE_MULTI_THREAD == 1
_z_task_attr_t *task_attributes;
z_task_attr_t *task_attributes;
#else
uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior
#endif
Expand Down
6 changes: 3 additions & 3 deletions include/zenoh-pico/net/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
typedef struct {
#if Z_FEATURE_MULTI_THREAD == 1
_z_mutex_t _mutex_inner;
z_mutex_t _mutex_inner;
#endif // Z_FEATURE_MULTI_THREAD == 1

// Zenoh-pico is considering a single transport per session.
Expand Down Expand Up @@ -139,7 +139,7 @@ int8_t _zp_send_join(_z_session_t *z);
* Returns:
* ``0`` in case of success, ``-1`` in case of failure.
*/
int8_t _zp_start_read_task(_z_session_t *z, _z_task_attr_t *attr);
int8_t _zp_start_read_task(_z_session_t *z, z_task_attr_t *attr);

/**
* Stop the read task. This may result in stopping a thread or a process depending
Expand All @@ -166,7 +166,7 @@ int8_t _zp_stop_read_task(_z_session_t *z);
* Returns:
* ``0`` in case of success, ``-1`` in case of failure.
*/
int8_t _zp_start_lease_task(_z_session_t *z, _z_task_attr_t *attr);
int8_t _zp_start_lease_task(_z_session_t *z, z_task_attr_t *attr);

/**
* Stop the lease task. This may result in stopping a thread or a process depending
Expand Down
4 changes: 2 additions & 2 deletions include/zenoh-pico/session/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ _Z_LIST_DEFINE(_z_pending_query, _z_pending_query_t)

typedef struct {
#if Z_FEATURE_MULTI_THREAD == 1
_z_mutex_t _mutex;
_z_condvar_t _cond_var;
z_mutex_t _mutex;
z_condvar_t _cond_var;
#endif // Z_FEATURE_MULTI_THREAD == 1
_z_reply_data_list_t *_replies;
} _z_pending_query_collect_t;
Expand Down
26 changes: 13 additions & 13 deletions include/zenoh-pico/system/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,25 @@ void z_free(void *ptr);

#if Z_FEATURE_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_FEATURE_MULTI_THREAD == 1

/*------------------ Sleep ------------------*/
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/system/platform/arduino/esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#endif // Z_FEATURE_MULTI_THREAD == 1

#if Z_FEATURE_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_FEATURE_MULTI_THREAD == 1

typedef struct timespec z_clock_t;
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/system/platform/arduino/opencr.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
#include "zenoh-pico/config.h"

#if Z_FEATURE_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_FEATURE_MULTI_THREAD == 1

typedef struct timespec z_clock_t;
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/system/platform/emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#if Z_FEATURE_MULTI_THREAD == 1
#include <pthread.h>

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_FEATURE_MULTI_THREAD == 1

typedef double z_clock_t;
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/system/platform/espidf.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#if Z_FEATURE_MULTI_THREAD == 1
#include <pthread.h>

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_FEATURE_MULTI_THREAD == 1

typedef struct timespec z_clock_t;
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/system/platform/freertos_plus_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ typedef struct {
StackType_t *stack_buffer;
StaticTask_t *task_buffer;
#endif /* SUPPORT_STATIC_ALLOCATION */
} _z_task_attr_t;
} z_task_attr_t;

typedef struct {
TaskHandle_t handle;
EventGroupHandle_t join_event;
} _z_task_t;
} z_task_t;

typedef SemaphoreHandle_t _z_mutex_t;
typedef void *_z_condvar_t;
typedef SemaphoreHandle_t z_mutex_t;
typedef void *z_condvar_t;
#endif // Z_MULTI_THREAD == 1

typedef TickType_t z_clock_t;
Expand Down
Loading

0 comments on commit 0e9b446

Please sign in to comment.