SRW_LOCK_HANDLE srw_lock_create()

in win32/src/srw_lock_win32.c [105:178]


SRW_LOCK_HANDLE srw_lock_create(bool do_statistics, const char* lock_name)
{
    SRW_LOCK_HANDLE result;
    /*Codes_SRS_SRW_LOCK_02_001: [ srw_lock_create shall allocate memory for SRW_LOCK_HANDLE. ]*/
    result = malloc(sizeof(SRW_LOCK_HANDLE_DATA));
    if (result == NULL)
    {
        /*return as is*/
        LogError("failure in malloc(sizeof(SRW_LOCK_HANDLE_DATA)=%zu)", sizeof(SRW_LOCK_HANDLE_DATA));
    }
    else
    {
        /*Codes_SRS_SRW_LOCK_02_023: [ If do_statistics is true then srw_lock_create shall copy lock_name. ]*/
        if (do_statistics &&
            ((result->lockName= sprintf_char("%s", MU_P_OR_NULL(lock_name))) == NULL)
        )
        {
            LogError("failure in sprintf_char(\"%%s\", lock_name=%s)", MU_P_OR_NULL(lock_name));
        }
        else
        {
            /*Codes_SRS_SRW_LOCK_02_024: [ If do_statistics is true then srw_lock_create shall create a new TIMER_HANDLE by calling timer_create_new. ]*/
            if(
                do_statistics &&
                ((result->timer = timer_create_new())==NULL)
                )
            {
                LogError("failure in timer_create_new()");
            }
            else
            {
                result->doStatistics = do_statistics;
                /*Codes_SRS_SRW_LOCK_02_015: [ srw_lock_create shall call InitializeSRWLock. ]*/

                InitializeSRWLock(&result->lock);

                (void)QueryPerformanceFrequency(&result->freq);

                (void)InterlockedExchange64(&result->nCalls_AcquireSRWLockExclusive, 0);
                (void)InterlockedExchange64(&result->nCalls_AcquireSRWLockShared, 0);

                (void)InterlockedExchange64(&result->totalCounts_AcquireSRWLockExclusive, 0);
                (void)InterlockedExchange64(&result->totalCounts_ReleaseSRWLockExclusive, 0);

                (void)InterlockedExchange64(&result->totalCounts_AcquireSRWLockShared, 0);
                (void)InterlockedExchange64(&result->totalCounts_ReleaseSRWLockShared, 0);

                (void)InterlockedExchange64(&result->totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive, 0);

                (void)InterlockedExchange(&result->nSharedReaders, 0);
                (void)InterlockedExchange64(&result->totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared, 0);


                (void)QueryPerformanceCounter(&result->handleCreateCount);

                if (do_statistics)
                {
                    LogInfo("srw_lock_create returns %p for lock_name=%s", result, MU_P_OR_NULL(lock_name));
                }
                /*Codes_SRS_SRW_LOCK_02_003: [ srw_lock_create shall succeed and return a non-NULL value. ]*/
                goto allOk;

                //timer_destroy(result->timer);
            }
            /*Codes_SRS_SRW_LOCK_02_004: [ If there are any failures then srw_lock_create shall fail and return NULL. ]*/
            do_statistics ? free(result->lockName) : (void)0;
        }
        free(result);
        result = NULL;
    }
allOk:;
    return result;
    
}