Article Listing 1 Listing 2 Listing 3 Listing 4
Listing 5 Listing 6 Listing 7 Listing 8 Listing 9
Listing 10 Listing 11 Listing 12 Listing 13 Sidebar aug2004.tar

Listing 5 V_semaphore()

{
    # V_semaphore effectively increments the count or value of a semaphore.
    # The function returns one semaphore resource iff $pid currently has
    # one; otherwise the semaphore's number of resources is incremented by
    # one.

    local FUNC_NAME=V_semaphore
    ${TRACE:-trace $FUNC_NAME $@}

    local semaphore=$1
    integer num_resources=$2
    integer pid=$3
    integer resource_num=0

    assert_semaphore $semaphore

    ${INFO:-info "Trying to return a resource to semaphore $semaphore ..."}

    for resource_num in $(get_busy_resources $semaphore)
    do
        if return_resource $semaphore $resource_num $pid
        then
            ${INFO:-info "PID $pid returned resource $resource_num."}
            return 0
        fi
    done

    return 1
}