ResultCode timedJoinAndDeleteThread()

in agent/native/ext/platform_threads_linux.cpp [80:146]


ResultCode timedJoinAndDeleteThread( Thread** threadOutPtr, void** threadFuncRetVal, const TimeSpec* timeoutAbsUtc, bool isCreatedByThisProcess, /* out */ bool* hasTimedOut, String dbgDesc )
{
    ELASTIC_APM_ASSERT_VALID_IN_PTR_TO_PTR( threadOutPtr );
    ELASTIC_APM_ASSERT_VALID_OUT_PTR_TO_PTR( threadFuncRetVal );
    // ELASTIC_APM_ASSERT_VALID_PTR( timeoutAbsUtc ); <- timeoutAbsUtc can be NULL
    ELASTIC_APM_ASSERT_VALID_PTR( hasTimedOut );

    ResultCode resultCode;
    char txtOutStreamBuf[ ELASTIC_APM_TEXT_OUTPUT_STREAM_ON_STACK_BUFFER_SIZE ];
    TextOutputStream txtOutStream = ELASTIC_APM_TEXT_OUTPUT_STREAM_FROM_STATIC_BUFFER( txtOutStreamBuf );
    String dbgFuncDescSuffix = ( timeoutAbsUtc == NULL ? "" : " with timeout" );
    String dbgTimeoutAsLocal = ( timeoutAbsUtc == NULL ? "NULL" : streamUtcTimeSpecAsLocal( timeoutAbsUtc, &txtOutStream ) );
//    String dbgPThreadsFuncDesc = ( timeoutAbsUtc == NULL ? "pthread_join" : "pthread_timedjoin_np" );
    String dbgPThreadsFuncDesc = "pthread_join";

    ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG(
            "Join and delete thread%s"
            "; timeoutAbsUtc: %s; thread dbg desc: `%s'; call dbg desc: `%s'"
            "; isCreatedByThisProcess: %s"
            , dbgFuncDescSuffix
            , dbgTimeoutAsLocal, ( *threadOutPtr )->dbgDesc, dbgDesc
            , boolToString( isCreatedByThisProcess ) );

    if ( isCreatedByThisProcess )
    {
//    int pthreadResultCode = ( timeoutAbsUtc == NULL
//                              ? pthread_join( (*threadOutPtr)->thread, threadFuncRetVal )
//                              : pthread_timedjoin_np( (*threadOutPtr)->thread, threadFuncRetVal, timeoutAbsUtc ) );
        int pthreadResultCode = pthread_join( (*threadOutPtr)->thread, threadFuncRetVal );

        if ( pthreadResultCode == 0 )
        {
            *hasTimedOut = false;
        }
        else if ( pthreadResultCode == ETIMEDOUT )
        {
            *hasTimedOut = true;
        }
        else
        {
            ELASTIC_APM_LOG_ERROR(
                    "%s failed with error: `%s'"
                    "; timeoutAbsUtc: %s; thread dbg desc: `%s'; call dbg desc: `%s'"
                    , dbgPThreadsFuncDesc , streamErrNo( pthreadResultCode, &txtOutStream )
                    , dbgTimeoutAsLocal, (*threadOutPtr)->dbgDesc, dbgDesc );
            ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
        }
    }

    resultCode = resultSuccess;
    ELASTIC_APM_FREE_INSTANCE_AND_SET_TO_NULL( Thread, *threadOutPtr );

    finally:
    ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT_MSG(
            "Join and delete thread%s"
            "; hasTimedOut: %s"
            "; timeoutAbsUtc: %s; call dbg desc: `%s'"
            "; isCreatedByThisProcess: %s"
            , dbgFuncDescSuffix
            , resultCode == resultSuccess ? boolToString( *hasTimedOut ) : "N/A"
            , dbgTimeoutAsLocal, dbgDesc
            , boolToString( isCreatedByThisProcess ) );
    return resultCode;

    failure:
    goto finally;
}