예제 #1
0
파일: os_cond.c 프로젝트: osrf/opensplice
/** \brief Initialize the condition variable taking the condition
 *         attributes into account
 *
 * \b os_condInit calls \b pthread_cond_init to intialize the posix condition
 * variable.
 *
 * In case the scope attribute is \b OS_SCOPE_SHARED, the posix
 * condition variable "pshared" attribute is set to \b PTHREAD_PROCESS_SHARED
 * otherwise it is set to \b PTHREAD_PROCESS_PRIVATE.
 *
 * When in single process mode, a request for a SHARED variable will
 * implictly create a PRIVATE equivalent.  This is an optimisation
 * because there is no need for "shared" multi process variables in
 * single process mode.
 */
os_result
os_condInit (
    os_cond *cond,
    os_mutex *dummymtx,
    const os_condAttr *condAttr)
{
    os_result result;

    assert (cond != NULL);

    if(!condAttr) {
        os_condAttr defAttr;

        os_condAttrInit(&defAttr);
        cond->scope = defAttr.scopeAttr;
    } else {
        cond->scope = condAttr->scopeAttr;
    }

    cond->state = 0;

    if (cond->scope == OS_SCOPE_SHARED) {
        return getSem(cond);
    } else {
        cond->qId = (long)CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
        if ((HANDLE)cond->qId == NULL) {
            return os_resultFail;
        } else {
            return os_resultSuccess;
        }
    }
}
예제 #2
0
파일: c_sync.c 프로젝트: xrl/opensplice
c_syncResult
c_condInit (
    c_cond *cnd,
    c_mutex *mtx,
    const c_condAttr attr)
{
    os_result result;

    os_condAttr condAttr;

    os_condAttrInit (&condAttr);
    if (attr == PRIVATE_COND) {
	condAttr.scopeAttr = OS_SCOPE_PRIVATE;
    }
#ifdef NDEBUG
    result = os_condInit(cnd, mtx, &condAttr);
#else
    mtx->owner = OS_THREAD_ID_NONE;
     result = os_condInit(cnd, &mtx->mtx, &condAttr);
#endif
    if(result != os_resultSuccess){
        OS_REPORT_1(OS_ERROR, "c_condInit", 0, "os_condInit failed; os_result = %d.", result);
        assert(result == os_resultSuccess);
    }
    return result;
}
예제 #3
0
_Object
_ObjectAlloc (
    _ObjectKind     kind,
    long            size,
    gapi_boolean    (*deallocator)(void *))
{
    gapi_handle handle = NULL;
    _Object     object = NULL;

    if ( deallocator != NULL ) {
        handle = (gapi_handle) gapi__malloc(gapi_handleFree, 0, (C_SIZEOF(gapi_handle)));
    } else {
        handle = (gapi_handle) gapi__malloc(NULL, 0, (C_SIZEOF(gapi_handle)));
    }
    if ( handle != NULL ) {
        os_result osResult;
        os_mutexAttr osMutexAttr;
        os_condAttr  osCondAttr;

        handle->magic    = MAGIC;
        handle->kind     = kind;
        handle->registry = NULL;
        handle->userData = NULL;
        handle->busy     = FALSE;
        handle->deleteActionInfo.action   = NULL;
        handle->deleteActionInfo.argument = NULL;

        handle->beingDeleted = FALSE;

        osResult = os_mutexAttrInit (&osMutexAttr);
        osMutexAttr.scopeAttr = OS_SCOPE_PRIVATE;
        osResult = os_mutexInit (&handle->mutex, &osMutexAttr);

        osResult = os_condAttrInit (&osCondAttr);
        osCondAttr.scopeAttr = OS_SCOPE_PRIVATE;
        osResult = os_condInit (&handle->cv, &handle->mutex, &osCondAttr);

#ifdef _RWLOCK_
        osResult = os_mutexInit (&handle->read, &osMutexAttr);
        handle->count = 0;
#endif

        object = (_Object) os_malloc(size);
        if ( object != NULL ) {
            memset(object, 0, size);

            handle->deallocator  = deallocator;

            os_mutexLock(&handle->mutex);

            handle->object   = object;
            object->handle   = (gapi_object)handle;
        } else {
            gapi__free(handle);
        }
    }
    return object;

}
예제 #4
0
cms_soapThread
cms_soapThreadNew(
    const c_char* name,
    cms_client client)
{
    cms_soapThread thread;
    os_mutexAttr attr;
    os_condAttr condAttr;
    os_result osr;

    thread = NULL;
    osr = os_resultInvalid;

    if(client != NULL){
        thread = cms_soapThread(os_malloc(C_SIZEOF(cms_soapThread)));
        if (thread != NULL) {
            cms_object(thread)->kind = CMS_SOAPTHREAD;
            cms_threadInit(cms_thread(thread), name, &client->service->configuration->clientScheduling);
            cms_thread(thread)->uri =  os_strdup(cms_thread(client)->uri);
            thread->client = client;
            thread->soap = NULL;

            osr = os_mutexAttrInit(&attr);
            if(osr == os_resultSuccess){
                attr.scopeAttr = OS_SCOPE_PRIVATE;
                osr = os_mutexInit(&thread->soapMutex, &attr);
                if(osr == os_resultSuccess){
                    osr = os_condAttrInit(&condAttr);
                    if(osr == os_resultSuccess){
                        condAttr.scopeAttr = OS_SCOPE_PRIVATE;
                        osr = os_condInit(&thread->condition, &thread->soapMutex, &condAttr );
                    }
                }
            }
        }
    }

    if (osr != os_resultSuccess) {
        cms_soapThreadFree(thread);
        return NULL;
    }

    return thread;
}
예제 #5
0
static void
nw_serviceMain(
    const char *serviceName,
    const char *URI)
{
    u_serviceManager serviceManager;
    os_time sleepTime;
    os_result waitResult;
    nw_termination terminate;
    os_mutexAttr termMtxAttr;
    os_condAttr termCvAttr;
	c_bool fatal = FALSE;
    v_duration leasePeriod;
    terminate.terminate = FALSE;

    os_mutexAttrInit( & termMtxAttr );
    os_condAttrInit( & termCvAttr );
    termMtxAttr.scopeAttr = OS_SCOPE_PRIVATE;
    termCvAttr.scopeAttr = OS_SCOPE_PRIVATE;
    os_mutexInit( &terminate.mtx, &termMtxAttr );
    os_condInit( &terminate.cv, &terminate.mtx, &termCvAttr );

     /* Create networking service with kernel */
   service = u_serviceNew(URI, NW_ATTACH_TIMEOUT, serviceName, NULL,
                          U_SERVICE_NETWORKING, NULL);
    /* Initialize configuration */
   nw_configurationInitialize(service, serviceName, URI);

    /* Ask service manager for splicedaemon state */
    serviceManager = u_serviceManagerNew(u_participant(service));

    /* Create the controller which starts the updating */
    /* and calls the listener on a fatal error */
    controller = nw_controllerNew(service,controller_onfatal,&fatal);

    if (controller) {
		os_procAtExit(on_exit_handler);
        /* Start the actual engine */
        NW_REPORT_INFO(1, "Networking started");
        NW_TRACE(Mainloop, 1, "Networking started");

        nw_controllerStart(controller);
        /* Change state for spliced */
        u_serviceChangeState(service, STATE_INITIALISING);
        u_serviceChangeState(service, STATE_OPERATIONAL);
        /* Get sleeptime from configuration */
        nw_retrieveLeaseSettings(&leasePeriod, &sleepTime);
        /*sleepTime.tv_sec = 1; */

        /* Loop until termination is requested */
        u_serviceWatchSpliceDaemon(service, nw_splicedaemonListener,
                                   &terminate);
	os_mutexLock( &terminate.mtx );
        while ((!(int)terminate.terminate) && (!(int)fatal) && (!(int)f_exit)) {
            /* Assert my liveliness and the Splicedaemon's liveliness*/
            u_serviceRenewLease(service, leasePeriod);
            /* Check if anybody is still remotely interested */
            nw_controllerUpdateHeartbeats(controller);
            /* Wait before renewing again */
	        waitResult = os_condTimedWait( &terminate.cv, &terminate.mtx, &sleepTime );
	        if (waitResult == os_resultFail)
            {
                OS_REPORT(OS_CRITICAL, "nw_serviceMain", 0,
                          "os_condTimedWait failed - thread will terminate");
                fatal = TRUE;
            }
/* QAC EXPECT 2467; Control variable, terminate, not modified inside loop. That is correct, it is modified by another thread */
        }
	os_mutexUnlock( &terminate.mtx );
		/* keep process here waiting for the exit processing */
		while ((int)f_exit){os_nanoSleep(sleepTime);}

		if (!(int)fatal ) {
			leasePeriod.seconds = 20;
			leasePeriod.nanoseconds = 0;
			u_serviceRenewLease(service, leasePeriod);
	        u_serviceChangeState(service, STATE_TERMINATING);

	        nw_controllerStop(controller);
	        nw_controllerFree(controller);
			controller = NULL;
	        NW_REPORT_INFO(1, "Networking stopped");
	        NW_TRACE(Mainloop, 1, "Networking stopped");
		}
    }
	if (!(int)fatal ) {
	    nw_configurationFinalize();

	    /* Clean up */
	    u_serviceChangeState(service, STATE_TERMINATED);
		u_serviceManagerFree(serviceManager);

		u_serviceFree(service);
	}
}
예제 #6
0
/**************************************************************
 * Protected functions
 **************************************************************/
s_kernelManager
s_kernelManagerNew(
    spliced daemon)
{
    s_kernelManager km;
    s_configuration config;
    os_mutexAttr mtxAttr;
    os_condAttr cvAttr;
    int status;
    os_result osr;

    status = 0;
    km = os_malloc((os_uint32)C_SIZEOF(s_kernelManager));
    if (km) {
        km->spliced = splicedGetService(daemon);
        km->active = 0;
        osr = os_mutexAttrInit(&mtxAttr);
        if (osr == os_resultSuccess) {
            mtxAttr.scopeAttr = OS_SCOPE_PRIVATE;
            osr = os_mutexInit(&km->mtx, &mtxAttr);
        } else {
            status++;
        }
        if (osr == os_resultSuccess) {
            osr = os_condAttrInit(&cvAttr);
            if (osr == os_resultSuccess) {
                cvAttr.scopeAttr = OS_SCOPE_PRIVATE;
                osr = os_condInit(&km->cv, &km->mtx, &cvAttr);
            } else {
                os_mutexDestroy(&km->mtx); /* don't care if this succeeds, already in error situation */
                status++;
            }
            if (osr == os_resultSuccess) {
                config = splicedGetConfiguration(daemon);
                osr = os_threadCreate(&km->id, 
                            S_THREAD_KERNELMANAGER, &config->kernelManagerScheduling, 
                            kernelManager, km);
                if (osr != os_resultSuccess) {
                    /* don't care if the following statements succeeds, already in error situation */
                    os_mutexDestroy(&km->mtx);
                    os_condDestroy(&km->cv);
                    status++;
                }
            }
            if (osr == os_resultSuccess) {
                config = splicedGetConfiguration(daemon);
                osr = os_threadCreate(&km->resendManager,
                            S_THREAD_RESENDMANAGER, &config->resendManagerScheduling,
                            resendManager, km);
                if (osr != os_resultSuccess) {
                    /* don't care if the following statements succeeds, already in error situation */
                    os_mutexDestroy(&km->mtx);
                    os_condDestroy(&km->cv);
                    status++;
                }
            }
            if (osr == os_resultSuccess ) {
                config = splicedGetConfiguration(daemon);
                if (config->enableCandMCommandThread ) {
                   osr = os_threadCreate(&km->cAndMCommandManager,
                                         S_THREAD_C_AND_M_COMMANDMANAGER,
                                         &config->cAndMCommandScheduling,
                                         cAndMCommandManager, km);
                   if (osr != os_resultSuccess) {
                      /* don't care if the following statements succeeds, already in error situation */
                      os_mutexDestroy(&km->mtx);
                      os_condDestroy(&km->cv);
                      status++;
                   }
                }
            }
        } else {
            status++;
        }
    }
    
    if (status && km) {
        os_free(km);
        km = NULL;
    }
    
    return km;
}
예제 #7
0
cms_client
cms_clientNew(
    unsigned long ip,
    cms_service service,
    const c_char* uri)
{
    cms_client client;
    os_result osr;
    os_mutexAttr attr;
    os_condAttr condAttr;

    client = cms_client(os_malloc(C_SIZEOF(cms_client)));
    cms_threadInit(cms_thread(client), "cms_client", &service->configuration->clientScheduling);
    if(client != NULL){
        cms_object(client)->kind = CMS_CLIENT;
        cms_thread(client)->uri = os_strdup(uri);
        client->ip = ip;
        client->initCount = 0;
        client->service = service;
        client->internalFree = FALSE;
        osr = os_mutexAttrInit(&attr);

        if(osr == os_resultSuccess){
            attr.scopeAttr = OS_SCOPE_PRIVATE;
            osr = os_mutexInit(&client->soapMutex, &attr);
            client->soapEnvs = c_iterNew(NULL);

            if(osr == os_resultSuccess){
                osr = os_condAttrInit(&condAttr);

                if(osr == os_resultSuccess){
                    osr = os_mutexInit(&client->conditionMutex, &attr);

                    if(osr == os_resultSuccess){
                        condAttr.scopeAttr = OS_SCOPE_PRIVATE;
                        osr = os_condInit(&client->condition, &client->conditionMutex, &condAttr );
                        if(osr == os_resultSuccess){
                            osr = os_mutexInit(&client->threadMutex, &attr);

                            if(osr == os_resultSuccess){
                                client->threads = c_iterNew(NULL);
                            } else {
                                cms_clientFree(client);
                            }
                        }
                    } else {
                        cms_clientFree(client);
                    }
                } else {
                    cms_clientFree(client);
                }
            } else {
                cms_clientFree(client);
            }
        } else {
            cms_clientFree(client);
        }
    }

    if(client == NULL){
        if(service->configuration->verbosity >= 1){
            OS_REPORT(OS_ERROR, CMS_CONTEXT, 0,
            "cms_clientNew: client could not be initialized.");
        }
    }
    return client;
}