@@ -344,56 +344,53 @@ int event_handler_manager::start_thread()
344344{
345345 cpu_set_t cpu_set;
346346 pthread_attr_t tattr;
347+ int ret;
348+ bool affinity_requested = false ;
347349
348350 if (!m_b_continue_running) {
351+ errno = ECANCELED;
349352 return -1 ;
350353 }
351-
352354 if (m_event_handler_tid != 0 ) {
353355 return 0 ;
354356 }
355357
356- // m_reg_action_q.reserve(); //todo change to vector and reserve
357-
358- BULLSEYE_EXCLUDE_BLOCK_START
359- if (pthread_attr_init (&tattr)) {
360- evh_logpanic (" Failed to initialize thread attributes" );
358+ ret = pthread_attr_init (&tattr);
359+ if (ret != 0 ) {
360+ return -1 ;
361361 }
362- BULLSEYE_EXCLUDE_BLOCK_END
363362
364363 cpu_set = safe_mce_sys ().internal_thread_affinity ;
365364 if (strcmp (safe_mce_sys ().internal_thread_affinity_str , " -1" ) &&
366- !strcmp (safe_mce_sys ().internal_thread_cpuset ,
367- MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) { // no affinity
368- BULLSEYE_EXCLUDE_BLOCK_START
369- if (pthread_attr_setaffinity_np (&tattr, sizeof (cpu_set), &cpu_set)) {
370- evh_logpanic (" Failed to set CPU affinity" );
365+ !strcmp (safe_mce_sys ().internal_thread_cpuset , MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) {
366+ ret = pthread_attr_setaffinity_np (&tattr, sizeof (cpu_set), &cpu_set);
367+ if (ret != 0 ) {
368+ evh_logwarn (" Failed to set event handler thread affinity. (errno=%d)" , errno);
371369 }
372- BULLSEYE_EXCLUDE_BLOCK_END
370+ affinity_requested = (ret == 0 );
373371 } else {
374372 evh_logdbg (" Internal thread affinity not set." );
375373 }
376374
377- int ret = pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this );
378- if (ret) {
379- // maybe it's the cset issue? try without affinity
380- evh_logwarn (" Failed to start event handler thread with thread affinity - trying without. "
381- " [errno=%d %s]" ,
382- ret, strerror (ret));
383- BULLSEYE_EXCLUDE_BLOCK_START
384- if (pthread_attr_init (&tattr)) {
385- evh_logpanic (" Failed to initialize thread attributes" );
386- }
387- if (pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this )) {
388- evh_logpanic (" Failed to start event handler thread" );
389- }
390- BULLSEYE_EXCLUDE_BLOCK_END
375+ ret = pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this );
376+ if (ret != 0 && affinity_requested) {
377+ // Try without affinity in case this is a cset issue.
378+ evh_logwarn (" Failed to start event handler thread with a thread affinity. Trying default "
379+ " thread affinity. (errno=%d)" ,
380+ errno);
381+ pthread_attr_destroy (&tattr);
382+ ret = pthread_attr_init (&tattr)
383+ ?: pthread_create (&m_event_handler_tid, &tattr, event_handler_thread, this );
391384 }
392-
385+ // Destroy will either succeed or return EINVAL if the init fails in the above block.
393386 pthread_attr_destroy (&tattr);
394387
395- evh_logdbg (" Started event handler thread" );
396- return 0 ;
388+ if (ret == 0 ) {
389+ evh_logdbg (" Started event handler thread." );
390+ } else {
391+ evh_logerr (" Failed to start event handler thread. (errno=%d)" , errno);
392+ }
393+ return ret;
397394}
398395
399396void event_handler_manager::stop_thread ()
@@ -483,15 +480,11 @@ void event_handler_manager::post_new_reg_action(reg_action_t ®_action)
483480 return ;
484481 }
485482
486- start_thread ();
487-
488483 evh_logfunc (" add event action %s (%d)" , reg_action_str (reg_action.type ), reg_action.type );
489484
490- bool is_empty = false ;
485+ bool is_empty;
491486 m_reg_action_q_lock.lock ();
492- if (m_p_reg_action_q_to_push_to->empty ()) {
493- is_empty = true ;
494- }
487+ is_empty = m_p_reg_action_q_to_push_to->empty ();
495488 m_p_reg_action_q_to_push_to->push_back (reg_action);
496489 m_reg_action_q_lock.unlock ();
497490 if (is_empty) {
0 commit comments