示例#1
0
/**
 * RefreshCredential: thread.
 */
static void *
ngislRefreshCredentialThread(
    void *arg)
{
    static const char fName[] = "ngislRefreshCredentialThread";
    ngisiRefreshCredential_t *ref;
    int sleepSec, wasTimeout;
    ngisiContext_t *context;
    int mutexLocked, result;
    int *error, errorEntity;
    time_t now, timeoutTime;

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

    context = (ngisiContext_t *)arg;
    ref = &context->ngisc_refreshCredential;
    error = &errorEntity;
    *error = 0;

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

    mutexLocked = 0;
    ref->ngisrc_working = 1; /* TRUE */
    sleepSec = 0;

    /* Log */
    ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName,
        "Refresh Credential thread invoked.\n");

    /* 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;

    sleepSec = 0; /* Do not wait at first. */

    /* Wait the status */
    while (ref->ngisrc_continue != 0) {
        wasTimeout = 0;

        /* Wait the event time. */
        if (sleepSec > 0) {
            ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName,
                "sleep %d sec for next update.\n", sleepSec);

            result = ngisiCondTimedWait(context,
                &ref->ngisrc_cond, &ref->ngisrc_mutex,
                sleepSec, &wasTimeout, error);
            if (result == 0) {
                ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
                    "Can't wait the Condition Variable.\n");
                goto error;
            }
        }

        now = time(NULL);
        timeoutTime = ref->ngisrc_nextEventTime;

        /* Perform refresh credential. */
        if ((timeoutTime > 0) && (now >= timeoutTime)) {

            result = ngisiJobRefreshCredential(context, error);
            if (result == 0) {
                ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
                    "Update the credential failed.\n");
                /* Not return */
            }

            if (ref->ngisrc_updateInterval > 0) {
                ref->ngisrc_nextEventTime = now + ref->ngisrc_updateInterval;
            } else {
                ref->ngisrc_nextEventTime = 0;
            }
        } 

        timeoutTime = ref->ngisrc_nextEventTime;
        sleepSec = NGISL_REFRESH_CREDENTIALS_SLEEP_WAIT_SEC;
        if (timeoutTime > 0) {
            sleepSec = timeoutTime - time(NULL);
        }
    }

    /* 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;
    }

    /**
     * Tell the Main Thread that, Refresh Credential thread was stopped.
     */

    /* Log */
    ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName,
        "Refresh Credential thread exiting.\n");

    /* 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_working = 0; /* FALSE */

    /* 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;
    }

    /* 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 NULL;

    /* Error occurred */
error:
    /* Set the status */
    ref->ngisrc_working = 0; /* FALSE */

    /* Log */
    ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName,
        "Refresh Credential thread exiting by error.\n");

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

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

    /* Failed */
    return NULL;
}
示例#2
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;
}
示例#3
0
/**
 * RefreshCredential: Set update interval.
 */
int
ngisiRefreshCredentialUpdateIntervalSet(
    ngisiContext_t *context,
    ngisiRefreshCredential_t *ref,
    int newInterval,
    int *error)
{
    static const char fName[] = "ngisiRefreshCredentialUpdateIntervalSet";
    time_t oldEventTime, nextEventTime;
    int mutexLocked, result, notify;
    int oldInterval;

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

    mutexLocked = 0;
    notify = 0;

    /* 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;

    oldInterval = ref->ngisrc_updateInterval;
    oldEventTime = ref->ngisrc_nextEventTime;

    if (newInterval != oldInterval) {
        notify = 1;

        if (newInterval > 0) {
            ref->ngisrc_updateInterval = newInterval;

            /* Select the early event time */
            nextEventTime = time(NULL) + newInterval;
            if ((oldEventTime > 0) && (nextEventTime > oldEventTime)) {
                nextEventTime = oldEventTime;
            }
            ref->ngisrc_nextEventTime = nextEventTime;
        } else {
            ref->ngisrc_updateInterval = 0;
            ref->ngisrc_nextEventTime = 0;
        }

        /* 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;
        }
    }

    /* 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;
    }

    if ((notify != 0) && (newInterval > 0) &&
        (ref->ngisrc_continue == 0) && (ref->ngisrc_working == 0)) {
        /* Start the thread */
        result = ngislRefreshCredentialThreadStart(
            context, ref, error);
        if (result == 0) {
            ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
                "Can't start the Refresh Credential thread.\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;
}
示例#4
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;
}
示例#5
0
/**
 * Request Reader Set Done
 */
int
ngisiRequestReaderSetDone(
    ngisiContext_t *context,
    int *error)
{
    static const char fName[] = "ngisiRequestReaderSetDone";
    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;

    /* Set done */
    requestReader->ngisr_working = 0;

    result = ngisiCondBroadcast(context,
        &requestReader->ngisr_cond, error);
    if (result == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
            "Broadcast the cond 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;
}
示例#6
0
/**
 * Request Reader Status Set
 */
static int
ngislRequestReaderStatusSet(
    ngisiContext_t *context,
    ngisiRequestReaderStatus_t status,
    int *error)
{
    static const char fName[] = "ngislRequestReaderStatusSet";
    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;

    /* Update the status */
    requestReader->ngisr_status = status;

    /* Signal */
    result = ngisiCondBroadcast(
        context, &requestReader->ngisr_cond, error);
    if (result == 0) {
        ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName,
            "Cond Signal 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;
}