Skip to content

Commit 376694e

Browse files
committed
Merge branch 'main' of github.com:lf-lang/reactor-c into netdriver
2 parents c2f6f0a + 85d54a4 commit 376694e

23 files changed

+348
-312
lines changed

core/environment.c

+17-23
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,14 @@ static void environment_init_threaded(environment_t* env, int num_workers) {
4545
#if !defined(LF_SINGLE_THREADED)
4646
env->num_workers = num_workers;
4747
env->thread_ids = (lf_thread_t*)calloc(num_workers, sizeof(lf_thread_t));
48-
LF_ASSERT(env->thread_ids, "Out of memory");
48+
LF_ASSERT_NON_NULL(env->thread_ids);
4949
env->barrier.requestors = 0;
5050
env->barrier.horizon = FOREVER_TAG;
5151

5252
// Initialize synchronization objects.
53-
if (lf_mutex_init(&env->mutex) != 0) {
54-
lf_print_error_and_exit("Could not initialize environment mutex");
55-
}
56-
if (lf_cond_init(&env->event_q_changed, &env->mutex) != 0) {
57-
lf_print_error_and_exit("Could not initialize environment event queue condition variable");
58-
}
59-
if (lf_cond_init(&env->global_tag_barrier_requestors_reached_zero, &env->mutex)) {
60-
lf_print_error_and_exit("Could not initialize environment tag barrier condition variable");
61-
}
62-
53+
LF_MUTEX_INIT(&env->mutex);
54+
LF_COND_INIT(&env->event_q_changed, &env->mutex);
55+
LF_COND_INIT(&env->global_tag_barrier_requestors_reached_zero, &env->mutex);
6356

6457
#endif
6558
}
@@ -84,16 +77,16 @@ static void environment_init_modes(environment_t* env, int num_modes, int num_st
8477
#ifdef MODAL_REACTORS
8578
if (num_modes > 0) {
8679
mode_environment_t* modes = (mode_environment_t *) calloc(1, sizeof(mode_environment_t));
87-
LF_ASSERT(modes, "Out of memory");
80+
LF_ASSERT_NON_NULL(modes);
8881
modes->modal_reactor_states = (reactor_mode_state_t**) calloc(num_modes, sizeof(reactor_mode_state_t*));
89-
LF_ASSERT(modes->modal_reactor_states, "Out of memory");
82+
LF_ASSERT_NON_NULL(modes->modal_reactor_states);
9083
modes->modal_reactor_states_size = num_modes;
9184
modes->triggered_reactions_request = 0;
9285

9386
modes->state_resets_size = num_state_resets;
9487
if (modes->state_resets_size > 0) {
9588
modes->state_resets = (mode_state_variable_reset_data_t *) calloc(modes->state_resets_size, sizeof(mode_state_variable_reset_data_t));
96-
LF_ASSERT(modes->state_resets, "Out of memory");
89+
LF_ASSERT_NON_NULL(modes->state_resets);
9790
} else {
9891
modes->state_resets = NULL;
9992
}
@@ -113,10 +106,11 @@ static void environment_init_federated(environment_t* env, int num_is_present_fi
113106
#ifdef FEDERATED_DECENTRALIZED
114107
if (num_is_present_fields > 0) {
115108
env->_lf_intended_tag_fields = (tag_t**) calloc(num_is_present_fields, sizeof(tag_t*));
116-
LF_ASSERT(env->_lf_intended_tag_fields, "Out of memory");
109+
LF_ASSERT_NON_NULL(env->_lf_intended_tag_fields);
117110
env->_lf_intended_tag_fields_size = num_is_present_fields;
118111
} else {
119-
env->_lf_intended_tag_fields_size = NULL;
112+
env->_lf_intended_tag_fields = NULL;
113+
env->_lf_intended_tag_fields_size = 0;
120114
}
121115
#endif
122116
}
@@ -198,7 +192,7 @@ int environment_init(
198192
) {
199193

200194
env->name = malloc(strlen(name) + 1); // +1 for the null terminator
201-
LF_ASSERT(env->name, "Out of memory");
195+
LF_ASSERT_NON_NULL(env->name);
202196
strcpy(env->name, name);
203197

204198
env->id = id;
@@ -207,31 +201,31 @@ int environment_init(
207201
env->timer_triggers_size=num_timers;
208202
if(env->timer_triggers_size > 0) {
209203
env->timer_triggers = (trigger_t **) calloc(num_timers, sizeof(trigger_t));
210-
LF_ASSERT(env->timer_triggers, "Out of memory");
204+
LF_ASSERT_NON_NULL(env->timer_triggers);
211205
} else {
212206
env->timer_triggers = NULL;
213207
}
214208

215209
env->startup_reactions_size=num_startup_reactions;
216210
if (env->startup_reactions_size > 0) {
217211
env->startup_reactions = (reaction_t **) calloc(num_startup_reactions, sizeof(reaction_t));
218-
LF_ASSERT(env->startup_reactions, "Out of memory");
212+
LF_ASSERT_NON_NULL(env->startup_reactions);
219213
} else {
220214
env->startup_reactions = NULL;
221215
}
222216

223217
env->shutdown_reactions_size=num_shutdown_reactions;
224218
if(env->shutdown_reactions_size > 0) {
225219
env->shutdown_reactions = (reaction_t **) calloc(num_shutdown_reactions, sizeof(reaction_t));
226-
LF_ASSERT(env->shutdown_reactions, "Out of memory");
220+
LF_ASSERT_NON_NULL(env->shutdown_reactions);
227221
} else {
228222
env->shutdown_reactions = NULL;
229223
}
230224

231225
env->reset_reactions_size=num_reset_reactions;
232226
if (env->reset_reactions_size > 0) {
233227
env->reset_reactions = (reaction_t **) calloc(num_reset_reactions, sizeof(reaction_t));
234-
LF_ASSERT(env->reset_reactions, "Out of memory");
228+
LF_ASSERT_NON_NULL(env->reset_reactions);
235229
} else {
236230
env->reset_reactions = NULL;
237231
}
@@ -241,9 +235,9 @@ int environment_init(
241235

242236
if (env->is_present_fields_size > 0) {
243237
env->is_present_fields = (bool**)calloc(num_is_present_fields, sizeof(bool*));
244-
LF_ASSERT(env->is_present_fields, "Out of memory");
238+
LF_ASSERT_NON_NULL(env->is_present_fields);
245239
env->is_present_fields_abbreviated = (bool**)calloc(num_is_present_fields, sizeof(bool*));
246-
LF_ASSERT(env->is_present_fields_abbreviated, "Out of memory");
240+
LF_ASSERT_NON_NULL(env->is_present_fields_abbreviated);
247241
} else {
248242
env->is_present_fields = NULL;
249243
env->is_present_fields_abbreviated = NULL;

core/federated/RTI/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ int main(int argc, const char* argv[]) {
317317
if (rti.base.tracing_enabled) {
318318
_lf_number_of_workers = rti.base.number_of_scheduling_nodes;
319319
rti.base.trace = trace_new(NULL, rti_trace_file_name);
320-
LF_ASSERT(rti.base.trace, "Out of memory");
320+
LF_ASSERT_NON_NULL(rti.base.trace);
321321
start_trace(rti.base.trace);
322322
lf_print("Tracing the RTI execution in %s file.", rti_trace_file_name);
323323
}

core/federated/RTI/rti_common.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void initialize_scheduling_node(scheduling_node_t* e, uint16_t id) {
6060
void _logical_tag_complete(scheduling_node_t* enclave, tag_t completed) {
6161
// FIXME: Consolidate this message with NET to get NMR (Next Message Request).
6262
// Careful with handling startup and shutdown.
63-
lf_mutex_lock(rti_common->mutex);
63+
LF_MUTEX_LOCK(rti_common->mutex);
6464

6565
enclave->completed = completed;
6666

@@ -78,7 +78,7 @@ void _logical_tag_complete(scheduling_node_t* enclave, tag_t completed) {
7878
free(visited);
7979
}
8080

81-
lf_mutex_unlock(rti_common->mutex);
81+
LF_MUTEX_UNLOCK(rti_common->mutex);
8282
}
8383

8484
tag_t earliest_future_incoming_message_tag(scheduling_node_t* e) {

core/federated/RTI/rti_local.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ lf_mutex_t rti_mutex;
3737

3838
void initialize_local_rti(environment_t *envs, int num_envs) {
3939
rti_local = (rti_local_t*)calloc(1, sizeof(rti_local_t));
40-
LF_ASSERT(rti_local, "Out of memory");
40+
LF_ASSERT_NON_NULL(rti_local);
4141

4242
initialize_rti_common(&rti_local->base);
43-
LF_ASSERT(lf_mutex_init(&rti_mutex) == 0, "Could not create mutex");
43+
LF_MUTEX_INIT(&rti_mutex);
4444
rti_local->base.mutex = &rti_mutex;
4545
rti_local->base.number_of_scheduling_nodes = num_envs;
4646
rti_local->base.tracing_enabled = (envs[0].trace != NULL);
@@ -73,7 +73,7 @@ void initialize_enclave_info(enclave_info_t* enclave, int idx, environment_t * e
7373
enclave->env = env;
7474

7575
// Initialize the next event condition variable.
76-
LF_ASSERT(lf_cond_init(&enclave->next_event_condition, &rti_mutex) == 0, "Could not create cond var");
76+
LF_COND_INIT(&enclave->next_event_condition, &rti_mutex);
7777
}
7878

7979
tag_t rti_next_event_tag_locked(enclave_info_t* e, tag_t next_event_tag) {
@@ -86,8 +86,8 @@ tag_t rti_next_event_tag_locked(enclave_info_t* e, tag_t next_event_tag) {
8686
}
8787
// This is called from a critical section within the source enclave. Leave
8888
// this critical section and acquire the RTI mutex.
89-
LF_ASSERT(lf_mutex_unlock(&e->env->mutex) == 0, "Could not unlock mutex");
90-
LF_ASSERT(lf_mutex_lock(rti_local->base.mutex) == 0, "Could not lock mutex");
89+
LF_MUTEX_UNLOCK(&e->env->mutex);
90+
LF_MUTEX_LOCK(rti_local->base.mutex);
9191
tracepoint_federate_to_rti(e->env->trace, send_NET, e->base.id, &next_event_tag);
9292
// First, update the enclave data structure to record this next_event_tag,
9393
// and notify any downstream scheduling_nodes, and unblock them if appropriate.
@@ -105,8 +105,8 @@ tag_t rti_next_event_tag_locked(enclave_info_t* e, tag_t next_event_tag) {
105105
next_event_tag.time - lf_time_start(), next_event_tag.microstep);
106106
tracepoint_federate_from_rti(e->env->trace, receive_TAG, e->base.id, &next_event_tag);
107107
// Release RTI mutex and re-enter the critical section of the source enclave before returning.
108-
LF_ASSERT(lf_mutex_unlock(rti_local->base.mutex) == 0, "Could not unlock mutex");
109-
LF_ASSERT(lf_mutex_lock(&e->env->mutex) == 0, "Could not lock mutex");
108+
LF_MUTEX_UNLOCK(rti_local->base.mutex);
109+
LF_MUTEX_LOCK(&e->env->mutex);
110110
return next_event_tag;
111111
}
112112

@@ -136,8 +136,8 @@ tag_t rti_next_event_tag_locked(enclave_info_t* e, tag_t next_event_tag) {
136136
e->base.id, e->base.next_event.time - lf_time_start(), e->base.next_event.microstep);
137137
tracepoint_federate_from_rti(e->env->trace, receive_TAG, e->base.id, &result.tag);
138138
// Release RTI mutex and re-enter the critical section of the source enclave.
139-
LF_ASSERT(lf_mutex_unlock(rti_local->base.mutex) == 0, "Could not unlock mutex");
140-
LF_ASSERT(lf_mutex_lock(&e->env->mutex) == 0, "Could not lock mutex");
139+
LF_MUTEX_UNLOCK(rti_local->base.mutex);
140+
LF_MUTEX_LOCK(&e->env->mutex);
141141
return result.tag;
142142
}
143143

@@ -146,24 +146,24 @@ void rti_logical_tag_complete_locked(enclave_info_t* enclave, tag_t completed) {
146146
return;
147147
}
148148
// Release the enclave mutex while doing the local RTI work.
149-
LF_ASSERT(lf_mutex_unlock(&enclave->env->mutex) == 0, "Could not unlock mutex");
149+
LF_MUTEX_UNLOCK(&enclave->env->mutex);
150150
tracepoint_federate_to_rti(enclave->env->trace, send_LTC, enclave->base.id, &completed);
151151
_logical_tag_complete(&enclave->base, completed);
152152
// Acquire the enclave mutex again before returning.
153-
LF_ASSERT(lf_mutex_lock(&enclave->env->mutex) == 0, "Could not lock mutex");
153+
LF_MUTEX_LOCK(&enclave->env->mutex);
154154
}
155155

156156
void rti_update_other_net_locked(enclave_info_t* src, enclave_info_t * target, tag_t net) {
157157
// Here we do NOT leave the critical section of the target enclave before we
158158
// acquire the RTI mutex. This means that we cannot block within this function.
159-
LF_ASSERT(lf_mutex_lock(rti_local->base.mutex) == 0, "Could not lock mutex");
159+
LF_MUTEX_LOCK(rti_local->base.mutex);
160160
tracepoint_federate_to_federate(src->env->trace, send_TAGGED_MSG, src->base.id, target->base.id, &net);
161161

162162
// If our proposed NET is less than the current NET, update it.
163163
if (lf_tag_compare(net, target->base.next_event) < 0) {
164164
target->base.next_event = net;
165165
}
166-
LF_ASSERT(lf_mutex_unlock(rti_local->base.mutex) == 0, "Could not unlock mutex");
166+
LF_MUTEX_UNLOCK(rti_local->base.mutex);
167167
}
168168

169169
///////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)