@@ -54,6 +54,7 @@ mtapi_boolean_t embb_mtapi_thread_context_initialize_with_node_worker_and_core(
54
54
that -> core_num = core_num ;
55
55
that -> priorities = node -> attributes .max_priorities ;
56
56
that -> is_initialized = MTAPI_FALSE ;
57
+ that -> is_main_thread = (worker_index == 0 ) ? node -> attributes .reuse_main_thread : MTAPI_FALSE ;
57
58
embb_atomic_store_int (& that -> run , 0 );
58
59
59
60
that -> queue = (embb_mtapi_task_queue_t * * )embb_mtapi_alloc_allocate (
@@ -121,17 +122,29 @@ mtapi_boolean_t embb_mtapi_thread_context_start(
121
122
embb_core_set_add (& core_set , that -> core_num );
122
123
123
124
/* create thread */
124
- err = embb_thread_create (& that -> thread , & core_set , worker_func , that );
125
- if (EMBB_SUCCESS != err ) {
126
- embb_mtapi_log_error (
127
- "embb_mtapi_ThreadContext_initializeWithNodeAndCoreNumber() could not "
128
- "create thread %d on core %d\n" , that -> worker_index , that -> core_num );
129
- return MTAPI_FALSE ;
130
- }
131
-
132
- /* wait for worker to come up */
133
- while (0 == embb_atomic_load_int (& that -> run )) {
134
- embb_thread_yield ();
125
+ if (that -> is_main_thread ) {
126
+ /* reuse main thread */
127
+ that -> thread = embb_thread_current ();
128
+ err = embb_tss_create (& that -> tss_id );
129
+ if (EMBB_SUCCESS != err ) {
130
+ /* report error to scheduler */
131
+ embb_atomic_store_int (& that -> run , -1 );
132
+ return MTAPI_FALSE ;
133
+ }
134
+ embb_tss_set (& (that -> tss_id ), that );
135
+ embb_atomic_store_int (& that -> run , 1 );
136
+ } else {
137
+ err = embb_thread_create (& that -> thread , & core_set , worker_func , that );
138
+ if (EMBB_SUCCESS != err ) {
139
+ embb_mtapi_log_error (
140
+ "embb_mtapi_ThreadContext_initializeWithNodeAndCoreNumber() could not "
141
+ "create thread %d on core %d\n" , that -> worker_index , that -> core_num );
142
+ return MTAPI_FALSE ;
143
+ }
144
+ /* wait for worker to come up */
145
+ while (0 == embb_atomic_load_int (& that -> run )) {
146
+ embb_thread_yield ();
147
+ }
135
148
}
136
149
137
150
if (0 < embb_atomic_load_int (& that -> run )) {
@@ -146,7 +159,9 @@ void embb_mtapi_thread_context_stop(embb_mtapi_thread_context_t* that) {
146
159
if (0 < embb_atomic_load_int (& that -> run )) {
147
160
embb_atomic_store_int (& that -> run , 0 );
148
161
embb_condition_notify_one (& that -> work_available );
149
- embb_thread_join (& (that -> thread ), & result );
162
+ if (MTAPI_FALSE == that -> is_main_thread ) {
163
+ embb_thread_join (& (that -> thread ), & result );
164
+ }
150
165
}
151
166
}
152
167
@@ -158,6 +173,9 @@ void embb_mtapi_thread_context_finalize(embb_mtapi_thread_context_t* that) {
158
173
embb_mtapi_log_trace ("embb_mtapi_thread_context_finalize() called\n" );
159
174
160
175
if (that -> is_initialized ) {
176
+ if (that -> is_main_thread ) {
177
+ embb_tss_delete (& that -> tss_id );
178
+ }
161
179
embb_condition_destroy (& that -> work_available );
162
180
embb_mutex_destroy (& that -> work_available_mutex );
163
181
}
0 commit comments