u_waitset u_waitsetNew(void) { u_waitset _this = NULL; u_result result; result = u_userInitialise(); if (result == U_RESULT_OK) { _this = u_objectAlloc(sizeof(*_this), U_WAITSET, u__waitsetDeinitW, u__waitsetFreeW); _this->entries = NULL; _this->eventMask = V_EVENTMASK_ALL; _this->alive = TRUE; _this->waitBusy = FALSE; _this->detachCnt = 0; _this->multi_mode = OS_TRUE; _this->eventsEnabled = OS_TRUE; _this->notifyDetached = OS_FALSE; pa_st32(&_this->useCount, 0); os_mutexInit(&_this->mutex, NULL); os_condInit(&_this->cv, &_this->mutex, NULL); os_condInit(&_this->waitCv, &_this->mutex, NULL); } else { OS_REPORT(OS_ERROR, "u_waitsetNew", result, "Initialization failed. "); } return _this; }
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; }
struct nn_servicelease *nn_servicelease_new (void (*renew_cb) (void *arg), void *renew_arg) { struct nn_servicelease *sl; sl = os_malloc (sizeof (*sl)); nn_retrieve_lease_settings (&sl->sleepTime); sl->keepgoing = -1; sl->renew_cb = renew_cb ? renew_cb : dummy_renew_cb; sl->renew_arg = renew_arg; sl->ts = NULL; if ((sl->av_ary = os_malloc (thread_states.nthreads * sizeof (*sl->av_ary))) == NULL) goto fail_vtimes; /* service lease update thread initializes av_ary */ if (os_mutexInit (&sl->lock, NULL) != os_resultSuccess) goto fail_lock; if (os_condInit (&sl->cond, &sl->lock, NULL) != os_resultSuccess) goto fail_cond; return sl; fail_cond: os_mutexDestroy (&sl->lock); fail_lock: os_free (sl->av_ary); fail_vtimes: os_free (sl); return NULL; }
_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; }
cms_client cms_clientNew( unsigned long ip, cms_service service) { cms_client client; os_result osr; client = os_malloc(sizeof *client); if(client != NULL){ if (cms_threadInit(cms_thread(client), "cms_client", &service->configuration->clientScheduling)) { cms_object(client)->kind = CMS_CLIENT; cms_thread(client)->did = service->did; cms_thread(client)->uri = os_strdup(service->uri); client->ip = ip; client->initCount = 0; client->service = service; client->internalFree = FALSE; osr = os_mutexInit(&client->soapMutex, NULL); client->soapEnvs = c_iterNew(NULL); if(osr == os_resultSuccess){ osr = os_mutexInit(&client->conditionMutex, NULL); if(osr == os_resultSuccess){ osr = os_condInit(&client->condition, &client->conditionMutex, NULL ); if(osr == os_resultSuccess){ osr = os_mutexInit(&client->threadMutex, NULL); 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); client = NULL; } } if(client == NULL){ if(service->configuration->verbosity >= 1){ OS_REPORT(OS_ERROR, CMS_CONTEXT, 0, "cms_clientNew: client could not be initialized."); } } return client; }
s_shmMonitor s_shmMonitorNew( spliced splicedaemon) { s_shmMonitor _this; os_result result; s_configuration config; assert(splicedaemon); config = splicedGetConfiguration(splicedaemon); assert(config); _this = os_malloc(sizeof *_this); _this->spliceDaemon = splicedaemon; _this->terminate = OS_FALSE; _this->thr = NULL; _this->shmState = SHM_STATE_CLEAN; result = os_mutexInit(&_this->mutex, NULL); if(result != os_resultSuccess){ OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0, "Failed to init shm monitor mutex"); goto err_shmMonitor_mtx; } result = os_condInit(&_this->cleanCondition, &_this->mutex, NULL); if(result != os_resultSuccess){ OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0, "Failed to init shm monitor cleanCondition"); goto err_shmMonitor_clean_cnd; } ut_threadCreate(splicedGetThreads(splicedaemon), &(_this->thr), "shmMonitor", &config->leaseRenewAttribute, shmMonitorMain, _this); if (_this->thr == NULL) { OS_REPORT(OS_ERROR, OSRPT_CNTXT_SPLICED, 0, "Failed to start shared memory monitor"); goto err_shmMonitor_thr; } return _this; /* Error handling */ err_shmMonitor_thr: os_condDestroy(&_this->cleanCondition); err_shmMonitor_clean_cnd: os_mutexDestroy(&_this->mutex); err_shmMonitor_mtx: os_free(_this); return NULL; }
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; }
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); } }
/************************************************************** * 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; }
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; }