Beispiel #1
0
/**
 * RefreshCredential: Stop thread.
 */
static int
ngislRefreshCredentialThreadStop(
    ngisiContext_t *context,
    ngisiRefreshCredential_t *ref,
    int *error)
{
    static const char fName[] = "ngislRefreshCredentialThreadStop";
    int mutexLocked, result;

    /* Check the arguments */
    assert(context != NULL);
    assert(ref != NULL);

    mutexLocked = 0;

    if (ref->ngisrc_working == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName,
            "Refresh Credential thread is already stopped.\n");
        return 1;
    }


    /* Check if the flag is valid */
    assert(ref->ngisrc_continue != 0);

    /**
     * Tell the Refresh Credential thread to stop.
     */

    /* Lock */
    result = ngisiMutexLock(context, &ref->ngisrc_mutex, error);
    if (result == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
            "Lock the mutex failed.\n");
        goto error;
    }
    mutexLocked = 1;

    /* Set the status */
    ref->ngisrc_continue = 0; /* to stop */

    /* Notify signal */
    result = ngisiCondBroadcast(
        context, &ref->ngisrc_cond, error);
    if (result == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
            "Cond signal failed.\n");
        goto error;
    }

    /**
     * Suppress unlock and lock, to ignore CondBroadcast(!working) issue,
     * before the CondWait(!working) starts its proceedings.
     */

    /**
     * Wait the Refresh Credential thread to stop.
     */

    /* Suppress lock. already locked */

    /* Wait the status */
    while (ref->ngisrc_working != 0) {
        result = ngisiCondWait(context,
            &ref->ngisrc_cond, &ref->ngisrc_mutex,
            error);
        if (result == 0) {
            ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
                "Can't wait the Condition Variable.\n");
            goto error;
        }
    }

    /* Unlock */
    mutexLocked = 0;
    result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, error);
    if (result == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
            "Unlock the mutex failed.\n");
        goto error;
    }

    /* Success */
    return 1;

    /* Error occurred */
error:
    /* Unlock */
    if (mutexLocked != 0) {
        mutexLocked = 0;
        result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, NULL);
        if (result == 0) {
            ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
                "Unlock the mutex failed.\n");
            return 0;
        }
    }

    /* Failed */
    return 0;
}
Beispiel #2
0
/**
 * Request Reader Wait Done
 */
int
ngisiRequestReaderWaitDone(
    ngisiContext_t *context,
    int *error)
{
    static const char fName[] = "ngisiRequestReaderWaitDone";
    ngisiRequestReader_t *requestReader;
    int result, locked;

    /* Check the arguments */
    assert(context != NULL);

    requestReader = &context->ngisc_requestReader;
    locked = 0;

    /* Lock the mutex */
    result = ngisiMutexLock(
        context, &requestReader->ngisr_mutex, error);
    if (result == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
            "Lock the Request Reader mutex failed.\n");
        goto error;
    }
    locked = 1;

    /* Wait done */
    while ((requestReader->ngisr_status < NGISI_REQUEST_READER_STATUS_DONE)
        && (requestReader->ngisr_working != 0)) {
        result = ngisiCondWait(context,
            &requestReader->ngisr_cond,
            &requestReader->ngisr_mutex, error);
        if (result == 0) {
            ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
                "Cond Wait the Request Reader failed.\n");
            goto error;
        }
    }

    /* Unlock the mutex */
    locked = 0;
    result = ngisiMutexUnlock(
        context, &requestReader->ngisr_mutex, error);
    if (result == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
            "Unlock the Request Reader mutex failed.\n");
        goto error;
    }

    /* Success */
    return 1;

    /* Error occurred */
error:

    if (locked != 0) {
        locked = 0;
        result = ngisiMutexUnlock(
            context, &requestReader->ngisr_mutex, NULL);
        if (result == 0) {
            ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
                "Unlock the Request Reader mutex failed.\n");
        }
    }

    /* Failed */
    return 0;
}