Exemple #1
0
ClRcT 
__cosPosixMutexUnlock (ClOsalMutexIdT mutexId, ClBoolT verbose)
{
    ClRcT rc = CL_OK;
    ClUint32T retCode = 0;
    ClOsalMutexT* pMutex = (ClOsalMutexT*) mutexId;
    ClInt32T err = 0;
   
    CL_FUNC_ENTER();
    
    if (NULL == pMutex)
	{
        retCode = CL_OSAL_RC(CL_ERR_NULL_POINTER);
        if(verbose)
        {
            clLogError(CL_LOG_AREA_UNSPECIFIED, CL_LOG_CONTEXT_UNSPECIFIED,"Mutex Unlock : FAILED, mutex is NULL (used after delete?)");
            clDbgPause();
        }
        CL_FUNC_EXIT();
        return(retCode);
	}

    retry:
    err = sem_post(&pMutex->shared_lock.sem.posSem);
    if(err < 0)
    {
        if(errno == EINTR)
        {
            goto retry;
        }
        rc = CL_OSAL_RC(CL_ERR_LIBRARY);
        if(verbose)
        {
            clDbgCodeError(rc,("sem_post unlock returned [%s]\n",strerror(errno)));
        }
    }

    CL_FUNC_EXIT();
    return (rc);
}
ClRcT
clHandleMove(
    ClHandleDatabaseHandleT databaseHandle,
    ClHandleT oldHandle,
    ClHandleT newHandle)
{
	void           *instance = NULL;
    ClRcT          rc        = CL_OK;
    ClHdlDatabaseT *hdbp     = (ClHdlDatabaseT*) databaseHandle;
    ClRcT          ec        = CL_OK;

    hdlDbValidityChk(hdbp);
    /* We allow handles to be created that are pointing to some other handle DB and node hdlValidityChk(handle,hdbp); */
    oldHandle = CL_HDL_IDX(oldHandle); /* once we've verified it, we only care about the index */
    newHandle = CL_HDL_IDX(newHandle);
    
    if (CL_HANDLE_INVALID_VALUE == oldHandle 
        ||
        CL_HANDLE_INVALID_VALUE == newHandle)
    {
        clLogError("HDL", "MOVE",
                   "Passed [%s] handle is invalid",
                   oldHandle ? "new" : "old");
        clDbgPause();
        return CL_HANDLE_RC(CL_ERR_INVALID_HANDLE); /* 0 no longer allowed */
    }
    /*
     * Decrementing handle to ensure the non-zero handle interface.
     */
    --oldHandle;
    --newHandle;
    ec = pthread_mutex_lock (&hdbp->mutex);
    if (ec != 0)
    {
        return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR);
    }

    if(oldHandle >= hdbp->n_handles)
    {
        pthread_mutex_unlock(&hdbp->mutex);
        clLogError("HDL", "MOVE", "Old handle [%#llx] passed is invalid", oldHandle);
        return CL_HANDLE_RC(CL_ERR_INVALID_HANDLE);
    }

    if(hdbp->handles[oldHandle].ref_count != 1)
    {
        pthread_mutex_unlock(&hdbp->mutex);
        clLogError("HDL", "MOVE", "Old handle [%#llx] is %s", 
                   oldHandle, hdbp->handles[oldHandle].ref_count < 1 ? "invalid" : "in use");
        return CL_HANDLE_RC(CL_ERR_INVALID_STATE);
    }
    
    if(newHandle < hdbp->n_handles && hdbp->handles[newHandle].ref_count > 0)
    {
        pthread_mutex_unlock(&hdbp->mutex);
        clLogError("HDL", "MOVE", "New handle [%#llx] is in use", newHandle);
        return CL_HANDLE_RC(CL_ERR_ALREADY_EXIST);
    }

    instance = hdbp->handles[oldHandle].instance;
    if(!instance)
    {
        pthread_mutex_unlock(&hdbp->mutex);
        clLogError("HDL", "MOVE", "Old handle [%#llx] instance is NULL", oldHandle);
        return CL_HANDLE_RC(CL_ERR_INVALID_STATE);
    }

    if(newHandle >= hdbp->n_handles)
    {
        /*
         * Allocating space in Excess of to accomodate the value specified by the user.
         * NOTE: User should ensure that a sane value is supplied for handle.
         */
        ClHandleT excess_handles = newHandle - hdbp->n_handles + 1;

		ClHdlEntryT *new_handles = (ClHdlEntryT *)realloc (hdbp->handles,
                                                           sizeof (ClHdlEntryT) * (hdbp->n_handles+excess_handles));
		if (new_handles == NULL) 
        {
			ec = pthread_mutex_unlock (&hdbp->mutex);
            if (ec != 0)
            {
                return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR); /* This can be very bad */
            }
			return CL_HANDLE_RC(CL_ERR_NO_MEMORY);
		}

        /* 
         * Initialize the excess space. HANDLE_STATE_EMPTY will remain 0 
         * is assumed else need to set the excess entries explicitly in a 
         * loop. 
         */
        memset(&new_handles[hdbp->n_handles], 0, sizeof (ClHdlEntryT) * excess_handles);

        /* Update the values if success */
		hdbp->n_handles += excess_handles;
		hdbp->handles = new_handles;
	}
    /*
     * Reset the old handle
     */
    memset(&hdbp->handles[oldHandle], 0, sizeof(hdbp->handles[oldHandle]));
	hdbp->handles[newHandle].state = HANDLE_STATE_USED;
	hdbp->handles[newHandle].instance = instance;
	hdbp->handles[newHandle].ref_count = 1;
	hdbp->handles[newHandle].handle = CL_HDL_MAKE_ADDR(ASP_NODEADDR, gEOIocPort, hdbp->id, newHandle + 1);;

    clDbgResourceNotify(clDbgHandleResource, clDbgAllocate, hdbp, newHandle+1, ("Specific handle [%p:%#llx] allocated", (ClPtrT) hdbp, newHandle+1));

	ec = pthread_mutex_unlock (&hdbp->mutex);
    if (ec != 0)
    {
        return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR); /* This can be devastating */
    }
	return rc;
}
ClRcT
clHandleCreateSpecifiedHandle (
    ClHandleDatabaseHandleT databaseHandle,
	ClInt32T instance_size,
    ClHandleT handle)
{
	void           *instance = NULL;
    ClRcT          rc        = CL_OK;
    ClHdlDatabaseT *hdbp     = (ClHdlDatabaseT*) databaseHandle;
    ClRcT          ec        = CL_OK;
    ClHandleT      orgHandle = handle;

    hdlDbValidityChk(hdbp);
    /* We allow handles to be created that are pointing to some other handle DB and node hdlValidityChk(handle,hdbp); */
    handle = CL_HDL_IDX(handle); /* once we've verified it, we only care about the index */

    if (CL_HANDLE_INVALID_VALUE == handle)
    {
        clLogError("HDL", CL_LOG_CONTEXT_UNSPECIFIED, 
                   "Passed handle [%p:%#llX] is an invalid handle",
                   (ClPtrT) hdbp, handle);
        clDbgPause();
        return CL_HANDLE_RC(CL_ERR_INVALID_HANDLE); /* 0 no longer allowed */
    }
    /*
     * Decrementing handle to ensure the non-zero handle interface.
     */
    handle--;

    ec = pthread_mutex_lock (&hdbp->mutex);
    if (ec != 0)
    {
        return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR);
    }
    if(handle >= hdbp->n_handles)
    {
        /*
         * Allocating space in Excess of to accomodate the value specified by the user.
         * NOTE: User should ensure that a sane value is supplied for handle.
         */
        ClHandleT excess_handles = handle - hdbp->n_handles + 1;

		ClHdlEntryT *new_handles = (ClHdlEntryT *)realloc (hdbp->handles,
			sizeof (ClHdlEntryT) * (hdbp->n_handles+excess_handles));
		if (new_handles == 0) 
        {
			ec = pthread_mutex_unlock (&hdbp->mutex);
            if (ec != 0)
            {
                return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR); /* This can be very bad */
            }
			return CL_HANDLE_RC(CL_ERR_NO_MEMORY);
		}

        /* 
         * Initialize the excess space. HANDLE_STATE_EMPTY will remain 0 
         * is assumed else need to set the excess entries explicitly in a 
         * loop. 
         */
        memset(&new_handles[hdbp->n_handles], 0, sizeof (ClHdlEntryT) * excess_handles);

        /* Update the values if success */
		hdbp->n_handles += excess_handles;
		hdbp->handles = new_handles;
	}

    if (hdbp->handles[handle].state != HANDLE_STATE_EMPTY) {
        /*
         * The specified handle already in use so return the specific error.
         */
        rc = CL_HANDLE_RC(CL_ERR_ALREADY_EXIST);
        goto error_exit;
    }

	instance = clHeapCalloc (1, instance_size);
	if (instance == 0) {
		rc = CL_HANDLE_RC(CL_ERR_NO_MEMORY);
        goto error_exit;
	}
	memset (instance, 0, instance_size);
	hdbp->handles[handle].state = HANDLE_STATE_USED;
	hdbp->handles[handle].instance = instance;
	hdbp->handles[handle].ref_count = 1;
	hdbp->handles[handle].handle = orgHandle;

    clDbgResourceNotify(clDbgHandleResource, clDbgAllocate, hdbp, handle+1, ("Specific handle [%p:%#llX] allocated", (ClPtrT) hdbp, handle+1));
    hdbp->n_handles_used++;
error_exit:

	ec = pthread_mutex_unlock (&hdbp->mutex);
    if (ec != 0)
    {
        return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR); /* This can be devastating */
    }
	return rc;
}