Skip to content

Commit

Permalink
Merge pull request #507 from lf-lang/reactor-names
Browse files Browse the repository at this point in the history
Make reactor names available at runtime
  • Loading branch information
edwardalee authored Dec 30, 2024
2 parents 03137a0 + 6306c11 commit c2437e1
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
32 changes: 32 additions & 0 deletions core/reactor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ void lf_set_stop_tag(environment_t* env, tag_t tag) {
}
}

const char* lf_reactor_name(self_base_t* self) { return self->name; }

const char* lf_reactor_full_name(self_base_t* self) {
if (self->full_name != NULL) {
return self->full_name;
}
// First find the length of the full name.
size_t name_len = strlen(self->name);
size_t len = name_len;
self_base_t* parent = self->parent;
while (parent != NULL) {
len++; // For the dot.
len += strlen(parent->name);
parent = parent->parent;
}
self->full_name = (char*)lf_allocate(len + 1, sizeof(char), &self->allocations);
self->full_name[len] = '\0'; // Null terminate the string.

size_t location = len - name_len;
memcpy(&self->full_name[location], self->name, name_len);
parent = self->parent;
while (parent != NULL) {
location--;
self->full_name[location] = '.';
size_t parent_len = strlen(parent->name);
location -= parent_len;
memcpy(&self->full_name[location], parent->name, parent_len);
parent = parent->parent;
}
return self->full_name;
}

#ifdef FEDERATED_DECENTRALIZED

interval_t lf_get_stp_offset() { return lf_fed_STA_offset; }
Expand Down
21 changes: 21 additions & 0 deletions include/api/reaction_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,25 @@
*/
#define lf_time_logical_elapsed() lf_time_logical_elapsed(self->base.environment)

/**
* @brief Return the instance name of the reactor.
*
* The instance name is the name of given to the instance created by the `new` operator in LF.
* If the instance is in a bank, then the name will have a suffix of the form `[bank_index]`.
*
* @param reactor The reactor to get the name of.
*/
#define lf_reactor_name(reactor) lf_reactor_name(&reactor->base)

/**
* @brief Return the fully qualified name of the reactor.
*
* The fully qualified name of a reactor is the instance name of the reactor concatenated with the names of all
* of its parents, separated by dots. If the reactor or any of its parents is a bank, then the name
* will have a suffix of the form `[bank_index]`.
*
* @param reactor The reactor to get the name of.
*/
#define lf_reactor_full_name(reactor) lf_reactor_full_name(&reactor->base)

#endif // REACTION_MACROS_H
3 changes: 3 additions & 0 deletions include/api/reaction_macros_undef.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
#undef lf_time_logical
#undef lf_time_logical_elapsed

#undef lf_reactor_name
#undef lf_reactor_full_name

#endif // REACTION_MACROS_H
7 changes: 5 additions & 2 deletions include/core/lf_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ struct trigger_t {
* An allocation record that is used by a destructor for a reactor
* to free memory that has been dynamically allocated for the particular
* instance of the reactor. This will be an element of linked list.
* If the indirect field is true, then the allocated pointer points to
* pointer to allocated memory, rather than directly to the allocated memory.
* The `allocated` pointer points to the allocated memory, and the `next`
* pointer points to the next allocation record (or NULL if there are no more).
*/
typedef struct allocation_record_t {
void* allocated;
Expand All @@ -277,6 +277,9 @@ typedef struct self_base_t {
struct allocation_record_t* allocations;
struct reaction_t* executing_reaction; // The currently executing reaction of the reactor.
environment_t* environment;
char* name; // The name of the reactor. If a bank, appended with [index].
char* full_name; // The full name of the reactor or NULL if lf_reactor_full_name() is not called.
self_base_t* parent; // The parent of this reactor.
#if !defined(LF_SINGLE_THREADED)
void* reactor_mutex; // If not null, this is expected to point to an lf_mutex_t.
// It is not declared as such to avoid a dependence on platform.h.
Expand Down
21 changes: 21 additions & 0 deletions include/core/reactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,26 @@ void lf_free_all_reactors(void);
*/
void lf_free_reactor(self_base_t* self);

/**
* @brief Return the instance name of the reactor.
*
* The instance name is the name of given to the instance created by the `new` operator in LF.
* If the instance is in a bank, then the name will have a suffix of the form `[bank_index]`.
*
* @param self The self struct of the reactor.
*/
const char* lf_reactor_name(self_base_t* self);

/**
* @brief Return the full name of the reactor.
*
* The fully qualified name of a reactor is the instance name of the reactor concatenated with the names of all
* of its parents, separated by dots. If the reactor or any of its parents is a bank, then the name
* will have a suffix of the form `[bank_index]`.
*
* @param self The self struct of the reactor.
*/
const char* lf_reactor_full_name(self_base_t* self);

#endif /* REACTOR_H */
/** @} */

0 comments on commit c2437e1

Please # to comment.