#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;
} test_shm_t;
TEST_SHM_T__SIZE_ERROR);
ping_start(
void *eo_context, em_eo_t eo,
const em_eo_conf_t *conf);
ping_stop(void *eo_context, em_eo_t eo);
static void
em_queue_t queue, void *queue_ctx);
static void
enter_cb1(em_eo_t eo, void **eo_context, em_event_t events[], int num,
em_queue_t *queue, void **queue_context);
static void
enter_cb2(em_eo_t eo, void **eo_context, em_event_t events[], int num,
em_queue_t *queue, void **queue_context);
static void
exit_cb1(em_eo_t eo);
static void
exit_cb2(em_eo_t eo);
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_cb2() registering failed!");
test_fatal_if(ret !=
EM_OK,
"enter_cb1() registering failed!");
test_fatal_if(ret !=
EM_OK,
"enter_cb2() registering failed!");
test_fatal_if(ret !=
EM_OK,
"enter_cb2() unregistering failed!");
test_fatal_if(ret !=
EM_OK,
"exit_cb2() registering failed!");
test_fatal_if(ret !=
EM_OK,
"exit_cb1() registering failed!");
test_fatal_if(ret !=
EM_OK,
"exit_cb2() registering failed!");
test_fatal_if(ret !=
EM_OK,
"exit_cb2() unregistering failed!");
test_fatal_if(ret !=
EM_OK || eo_start_ret !=
EM_OK,
"em_eo_start(EO A) failed! EO:%" PRI_EO "\n"
"ret:%" PRI_STAT ", EO-start-ret:%" PRI_STAT "",
eo_a, ret, eo_start_ret);
test_fatal_if(ret !=
EM_OK || eo_start_ret !=
EM_OK,
"em_eo_start(EO B) failed! EO:%" PRI_EO "\n"
"ret:%" PRI_STAT ", EO-start-ret:%" PRI_STAT "",
eo_b, ret, eo_start_ret);
}
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);
stat =
test_fatal_if(stat !=
EM_OK,
"enter_cb2() unregistering failed!");
stat =
test_fatal_if(stat !=
EM_OK,
"enter_cb2() unregistering failed!");
stat =
test_fatal_if(stat !=
EM_OK,
"exit_cb2() unregistering failed!");
stat =
test_fatal_if(stat !=
EM_OK,
"exit_cb2() unregistering 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_context, em_eo_t eo,
const em_eo_conf_t *conf)
{
my_eo_context_t *eo_ctx = eo_context;
em_queue_t queue;
my_queue_context_t *q_ctx;
const char *queue_name;
(void)conf;
if (eo_ctx->is_a) {
queue_name = "queue A";
q_ctx = &test_shm->queue_context_a;
} else {
queue_name = "queue B";
q_ctx = &test_shm->queue_context_b;
}
NULL);
queue_name);
eo_ctx->my_queue = queue;
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"
eo_ctx->name, eo, queue);
if (eo_ctx->is_a) {
test_shm->queue_a = queue;
} else {
em_event_t event;
ping_event_t *ping;
test_shm->pool);
"Event allocation failed!");
ping->dest = queue;
ping->seq = 0;
status =
em_send(event, test_shm->queue_a);
test_fatal_if(status !=
EM_OK,
"em_send():%" PRI_STAT "\n"
status, eo, test_shm->queue_a);
}
}
ping_stop(void *eo_context, em_eo_t eo)
{
my_eo_context_t *eo_ctx = eo_context;
em_queue_t queue = eo_ctx->my_queue;
APPL_PRINT(
"Dispatcher callback example stop (%s, eo id %" PRI_EO ")\n",
eo_ctx->name, eo);
return status;
return status;
}
static void
em_queue_t queue, void *queue_context)
{
my_eo_context_t *eo_ctx = eo_context;
my_queue_context_t *q_ctx = queue_context;
em_queue_t dest;
ping_event_t *ping;
(void)type;
if (unlikely(appl_shm->exit_flag)) {
return;
}
dest = ping->dest;
ping->dest = queue;
APPL_PRINT("Ping from %s!\t"
"Queue: %" PRI_QUEUE " on core %02i. Event seq: %u.\n",
eo_ctx->name, q_ctx->queue,
em_core_id(), ping->seq++);
delay_spin(SPIN_COUNT);
if (unlikely(status !=
EM_OK)) {
test_fatal_if(!appl_shm->exit_flag,
"em_send():%" PRI_STAT "\n"
status, eo_ctx->this_eo, dest);
}
}
static void
enter_cb1(em_eo_t eo, void **eo_context, em_event_t events[], int num,
em_queue_t *queue, void **queue_context)
{
my_eo_context_t *eo_ctx = *eo_context;
my_queue_context_t *q_ctx = *queue_context;
(void)num;
(void)queue;
APPL_PRINT(
"++ Dispatcher enter callback 1 for EO: %" PRI_EO " (%s)\t"
"Queue: %" PRI_QUEUE " on core %02i. Event seq: %u.\n",
ping->seq);
}
static void
enter_cb2(em_eo_t eo, void **eo_context, em_event_t events[], int num,
em_queue_t *queue, void **queue_context)
{
my_eo_context_t *eo_ctx = *eo_context;
my_queue_context_t *q_ctx = *queue_context;
(void)num;
(void)queue;
APPL_PRINT(
"++ Dispatcher enter callback 2 for EO: %" PRI_EO " (%s)\t"
"Queue: %" PRI_QUEUE " on core %02i. Event seq: %u.\n",
ping->seq);
}
static void
exit_cb1(em_eo_t eo)
{
APPL_PRINT(
"-- Dispatcher exit callback 1 for EO: %" PRI_EO "\n", eo);
}
static void
exit_cb2(em_eo_t eo)
{
APPL_PRINT(
"-- Dispatcher exit callback 2 for EO: %" PRI_EO "\n", eo);
}