#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "cm_setup.h"
#include "cm_error_handler.h"
#define SPIN_COUNT 50000000
typedef struct {
em_queue_t dest;
unsigned int seq;
} ping_event_t;
typedef struct {
em_eo_t this_eo;
em_eo_t other_eo;
em_queue_t my_queue;
int is_a;
char name[16];
} my_eo_context_t;
typedef struct {
em_queue_t queue;
} my_queue_context_t;
typedef struct {
em_pool_t pool;
my_eo_context_t eo_context_a;
my_eo_context_t eo_context_b;
my_queue_context_t queue_context_a;
my_queue_context_t queue_context_b;
em_queue_t queue_a;
em_queue_t queue_b;
} test_shm_t;
TEST_SHM_T__SIZE_ERROR);
ping_start(
void *eo_ctx, em_eo_t eo,
const em_eo_conf_t *conf);
ping_stop(void *eo_ctx, em_eo_t eo);
static void
em_queue_t queue, void *q_ctx);
static void
enter_cb(em_eo_t eo, void **eo_ctx, em_event_t events[], int num,
em_queue_t *queue, void **q_ctx);
static void
exit_cb(em_eo_t eo);
static void
alloc_hook(const em_event_t events[], int num_act, int num_req,
static void
free_hook(const em_event_t events[], int num);
static void
send_hook(const em_event_t events[], int num,
em_queue_t queue, em_event_group_t event_group);
int main(int argc, char *argv[])
{
return cm_setup(argc, argv);
}
void test_init(const appl_conf_t *appl_conf)
{
(void)appl_conf;
if (core == 0) {
test_shm = env_shared_reserve("TestSharedMem",
sizeof(test_shm_t));
} else {
test_shm = env_shared_lookup("TestSharedMem");
}
if (test_shm == NULL) {
"Dispatcher callback init failed on EM-core: %u\n",
} else if (core == 0) {
memset(test_shm, 0, sizeof(test_shm_t));
}
}
void test_start(const appl_conf_t *appl_conf)
{
em_eo_t eo_a, eo_b;
if (appl_conf->num_pools >= 1)
test_shm->pool = appl_conf->pools[0];
else
APPL_PRINT("\n"
"***********************************************************\n"
"EM APPLICATION: '%s' initializing:\n"
" %s: %s() - EM-core:%d\n"
" Application running on %u EM-cores (procs:%u, threads:%u)\n"
"***********************************************************\n"
"\n",
appl_conf->name, NO_PATH(__FILE__), __func__,
em_core_id(),
appl_conf->core_count, appl_conf->num_procs, appl_conf->num_threads,
test_shm->pool);
"Undefined application event pool!");
eo_a =
em_eo_create(
"EO A", ping_start, NULL, ping_stop, NULL,
ping_receive, &test_shm->eo_context_a);
test_fatal_if(eo_a ==
EM_EO_UNDEF,
"EO A creation failed!");
eo_b =
em_eo_create(
"EO B", ping_start, NULL, ping_stop, NULL,
ping_receive, &test_shm->eo_context_b);
test_fatal_if(eo_b ==
EM_EO_UNDEF,
"EO B creation failed!");
test_shm->eo_context_a.this_eo = eo_a;
test_shm->eo_context_a.other_eo = eo_b;
test_shm->eo_context_a.is_a = 1;
test_shm->eo_context_b.this_eo = eo_b;
test_shm->eo_context_b.other_eo = eo_a;
test_shm->eo_context_b.is_a = 0;
test_fatal_if(ret !=
EM_OK,
"enter_cb() register failed!");
test_fatal_if(ret !=
EM_OK,
"enter_cb() register failed!");
test_fatal_if(ret !=
EM_OK,
"enter_cb() unregister failed!");
test_fatal_if(ret !=
EM_OK,
"exit_cb() register failed!");
test_fatal_if(ret !=
EM_OK,
"exit_cb() register failed!");
test_fatal_if(ret !=
EM_OK,
"exit_cb() unregister failed!");
test_fatal_if(ret !=
EM_OK,
"alloc_hook() register failed!");
test_fatal_if(ret !=
EM_OK,
"alloc_hook() register failed!");
test_fatal_if(ret !=
EM_OK,
"alloc_hook() unregister failed!");
test_fatal_if(ret !=
EM_OK,
"free_hook() register failed!");
test_fatal_if(ret !=
EM_OK,
"free_hook() register failed!");
test_fatal_if(ret !=
EM_OK,
"send_hook() register failed!");
test_fatal_if(ret !=
EM_OK,
"send_hook() register failed!");
test_fatal_if(ret !=
EM_OK,
"send_hook() unregister failed!");
test_fatal_if(ret !=
EM_OK,
"em_eo_start_sync(eo_a) failed!");
test_fatal_if(ret !=
EM_OK,
"em_eo_start_sync(eo_b) failed!");
em_event_t event;
ping_event_t *ping;
test_shm->pool);
ping->dest = test_shm->queue_b;
ping->seq = 0;
ret =
em_send(event, test_shm->queue_a);
test_fatal_if(ret !=
EM_OK,
"em_send():%" PRI_STAT
" Queue:%" PRI_QUEUE "",
ret, test_shm->queue_a);
}
void test_stop(const appl_conf_t *appl_conf)
{
const em_eo_t eo_a = test_shm->eo_context_a.this_eo;
const em_eo_t eo_b = test_shm->eo_context_b.this_eo;
(void)appl_conf;
APPL_PRINT("%s() on EM-core %d\n", __func__, core);
test_fatal_if(stat !=
EM_OK,
"enter_cb() unregister failed!");
test_fatal_if(stat !=
EM_OK,
"exit_cb() unregister failed!");
test_fatal_if(stat !=
EM_OK,
"alloc_hook() unregister failed!");
test_fatal_if(stat !=
EM_OK,
"free_hook() unregister failed!");
test_fatal_if(stat !=
EM_OK,
"send_hook() unregister failed!");
APPL_EXIT_FAILURE("EO A stop failed!");
APPL_EXIT_FAILURE("EO B stop failed!");
APPL_EXIT_FAILURE("EO A delete failed!");
APPL_EXIT_FAILURE("EO B delete failed!");
}
void test_term(const appl_conf_t *appl_conf)
{
(void)appl_conf;
APPL_PRINT("%s() on EM-core %d\n", __func__, core);
if (core == 0) {
env_shared_free(test_shm);
}
}
ping_start(
void *eo_ctx, em_eo_t eo,
const em_eo_conf_t *conf)
{
my_eo_context_t *const my_eo_ctx = eo_ctx;
em_queue_t queue;
my_queue_context_t *my_q_ctx;
const char *queue_name;
(void)conf;
if (my_eo_ctx->is_a) {
queue_name = "queue A";
my_q_ctx = &test_shm->queue_context_a;
} else {
queue_name = "queue B";
my_q_ctx = &test_shm->queue_context_b;
}
NULL);
"%s creation failed!", queue_name);
my_eo_ctx->my_queue = queue;
my_q_ctx->queue = queue;
test_fatal_if(status !=
EM_OK,
"Set queue context:%" PRI_STAT "\n"
test_fatal_if(status !=
EM_OK,
"EO add queue:%" PRI_STAT "\n"
my_eo_ctx->name, eo, queue);
if (my_eo_ctx->is_a)
test_shm->queue_a = queue;
else
test_shm->queue_b = queue;
}
ping_stop(void *eo_ctx, em_eo_t eo)
{
my_eo_context_t *const my_eo_ctx = eo_ctx;
em_queue_t queue = my_eo_ctx->my_queue;
APPL_PRINT(
"Dispatcher callback example stop (%s, eo id %" PRI_EO ")\n",
my_eo_ctx->name, eo);
return status;
return status;
}
static void
em_queue_t queue, void *q_ctx)
{
my_eo_context_t *const my_eo_ctx = eo_ctx;
em_queue_t dest;
ping_event_t *ping, *new_ping;
em_event_t new_event;
(void)type;
(void)q_ctx;
if (unlikely(appl_shm->exit_flag)) {
return;
}
dest = ping->dest;
ping->dest = queue;
APPL_PRINT(
"** EO-rcv: Ping from EO:'%s'(%" PRI_EO ") on core%02d!\t"
my_eo_ctx->name, my_eo_ctx->this_eo,
em_core_id(),
queue, event, ping->seq++);
test_shm->pool);
memcpy(new_ping, ping, sizeof(ping_event_t));
delay_spin(SPIN_COUNT);
if (unlikely(status !=
EM_OK)) {
test_fatal_if(!appl_shm->exit_flag,
"em_send():%" PRI_STAT
"EO:%" PRI_EO "\n"
status, my_eo_ctx->this_eo, queue, dest);
}
}
static void
enter_cb(em_eo_t eo, void **eo_ctx, em_event_t events[], int num,
em_queue_t *queue, void **q_ctx)
{
my_eo_context_t *my_eo_ctx = *eo_ctx;
my_queue_context_t *my_q_ctx = *q_ctx;
ping_event_t *ping;
em_event_t event = events[0];
(void)num;
(void)queue;
APPL_PRINT("\n"
"+ Dispatch enter callback EO:'%s'(%" PRI_EO ")\t"
my_eo_ctx->name, eo, my_q_ctx->queue,
em_core_id(),
event, ping->seq);
}
static void
exit_cb(em_eo_t eo)
{
APPL_PRINT(
"- Dispatch exit callback EO:'%s'(%" PRI_EO ")\n",
my_eo_ctx->name, eo);
}
static void
alloc_hook(const em_event_t events[], int num_act, int num_req,
{
em_eo_t eo, eo_a, eo_b;
void *eo_ctx;
my_eo_context_t *my_eo_ctx;
(void)num_req;
return;
if (unlikely(eo_ctx == NULL))
return;
if (unlikely(test_shm == NULL))
return;
eo_a = test_shm->eo_context_a.this_eo;
eo_b = test_shm->eo_context_a.other_eo;
if (eo != eo_a && eo != eo_b)
return;
my_eo_ctx = eo_ctx;
APPL_PRINT(
" Alloc-hook EO:'%s'(%" PRI_EO ")\t"
"sz:%u type:0x%x pool:%" PRI_POOL "\t\t"
"Events[%d]:",
my_eo_ctx->name, eo, size, type, pool,
num_act);
for (int i = 0; i < num_act; i++)
APPL_PRINT("\n");
}
static void
free_hook(const em_event_t events[], int num)
{
em_eo_t eo, eo_a, eo_b;
void *eo_ctx;
my_eo_context_t *my_eo_ctx;
return;
if (unlikely(eo_ctx == NULL))
return;
if (unlikely(test_shm == NULL))
return;
eo_a = test_shm->eo_context_a.this_eo;
eo_b = test_shm->eo_context_a.other_eo;
if (eo != eo_a && eo != eo_b)
return;
my_eo_ctx = eo_ctx;
APPL_PRINT(
" Free-hook EO:'%s'(%" PRI_EO ")\t\t\t\t\t\t"
"Events[%d]:", my_eo_ctx->name, eo, num);
for (int i = 0; i < num; i++)
APPL_PRINT("\n");
}
static void
send_hook(const em_event_t events[], int num,
em_queue_t queue, em_event_group_t event_group)
{
em_eo_t eo, eo_a, eo_b;
void *eo_ctx;
my_eo_context_t *my_eo_ctx;
(void)events;
(void)event_group;
return;
if (unlikely(eo_ctx == NULL))
return;
if (unlikely(test_shm == NULL))
return;
eo_a = test_shm->eo_context_a.this_eo;
eo_b = test_shm->eo_context_a.other_eo;
if (eo != eo_a && eo != eo_b)
return;
my_eo_ctx = eo_ctx;
APPL_PRINT(
" Send-hook EO:'%s'(%" PRI_EO ")\t"
"Events[%d]:",
for (int i = 0; i < num; i++)
APPL_PRINT("\n");
}