@@ -307,56 +307,53 @@ int event_handler_manager::start_thread()
307307{
308308 cpu_set_t cpu_set;
309309 pthread_attr_t tattr;
310+ int ret;
311+ bool affinity_requested = false ;
310312
311313 if (!m_b_continue_running) {
314+ errno = ECANCELED;
312315 return -1 ;
313316 }
314-
315317 if (m_event_handler_tid != 0 ) {
316318 return 0 ;
317319 }
318320
319- // m_reg_action_q.reserve(); //todo change to vector and reserve
320-
321- BULLSEYE_EXCLUDE_BLOCK_START
322- if (pthread_attr_init (&tattr)) {
323- evh_logpanic (" Failed to initialize thread attributes" );
321+ ret = pthread_attr_init (&tattr);
322+ if (ret != 0 ) {
323+ return -1 ;
324324 }
325- BULLSEYE_EXCLUDE_BLOCK_END
326325
327326 cpu_set = safe_mce_sys ().internal_thread_affinity ;
328327 if (strcmp (safe_mce_sys ().internal_thread_affinity_str , " -1" ) &&
329- !strcmp (safe_mce_sys ().internal_thread_cpuset ,
330- MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) { // no affinity
331- BULLSEYE_EXCLUDE_BLOCK_START
332- if (pthread_attr_setaffinity_np (&tattr, sizeof (cpu_set), &cpu_set)) {
333- evh_logpanic (" Failed to set CPU affinity" );
328+ !strcmp (safe_mce_sys ().internal_thread_cpuset , MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) {
329+ ret = pthread_attr_setaffinity_np (&tattr, sizeof (cpu_set), &cpu_set);
330+ if (ret != 0 ) {
331+ evh_logwarn (" Failed to set event handler thread affinity. (errno=%d)" , errno);
334332 }
335- BULLSEYE_EXCLUDE_BLOCK_END
333+ affinity_requested = (ret == 0 );
336334 } else {
337335 evh_logdbg (" Internal thread affinity not set." );
338336 }
339337
340- int ret = pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this );
341- if (ret) {
342- // maybe it's the cset issue? try without affinity
343- evh_logwarn (" Failed to start event handler thread with thread affinity - trying without. "
344- " [errno=%d %s]" ,
345- ret, strerror (ret));
346- BULLSEYE_EXCLUDE_BLOCK_START
347- if (pthread_attr_init (&tattr)) {
348- evh_logpanic (" Failed to initialize thread attributes" );
349- }
350- if (pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this )) {
351- evh_logpanic (" Failed to start event handler thread" );
352- }
353- BULLSEYE_EXCLUDE_BLOCK_END
338+ ret = pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this );
339+ if (ret != 0 && affinity_requested) {
340+ // Try without affinity in case this is a cset issue.
341+ evh_logwarn (" Failed to start event handler thread with a thread affinity. Trying default "
342+ " thread affinity. (errno=%d)" ,
343+ errno);
344+ pthread_attr_destroy (&tattr);
345+ ret = pthread_attr_init (&tattr)
346+ ?: pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this );
354347 }
355-
348+ // Destroy will either succeed or return EINVAL if the init fails in the above block.
356349 pthread_attr_destroy (&tattr);
357350
358- evh_logdbg (" Started event handler thread" );
359- return 0 ;
351+ if (ret == 0 ) {
352+ evh_logdbg (" Started event handler thread." );
353+ } else {
354+ evh_logerr (" Failed to start event handler thread. (errno=%d)" , errno);
355+ }
356+ return ret;
360357}
361358
362359void event_handler_manager::stop_thread ()
@@ -448,15 +445,11 @@ void event_handler_manager::post_new_reg_action(reg_action_t ®_action)
448445 return ;
449446 }
450447
451- start_thread ();
452-
453448 evh_logfunc (" add event action %s (%d)" , reg_action_str (reg_action.type ), reg_action.type );
454449
455- bool is_empty = false ;
450+ bool is_empty;
456451 m_reg_action_q_lock.lock ();
457- if (m_p_reg_action_q_to_push_to->empty ()) {
458- is_empty = true ;
459- }
452+ is_empty = m_p_reg_action_q_to_push_to->empty ();
460453 m_p_reg_action_q_to_push_to->push_back (reg_action);
461454 m_reg_action_q_lock.unlock ();
462455 if (is_empty) {
0 commit comments