void d_waitsetDeinit( d_object object) { d_waitset waitset; d_waitsetEntity we; d_waitsetHelper helper; assert(d_objectIsValid(object, D_WAITSET) == TRUE); if(object){ waitset = d_waitset(object); waitset->terminate = TRUE; if(waitset->runToCompletion == TRUE){ if(os_threadIdToInteger(waitset->thread)) { u_waitsetNotify(waitset->uwaitset, NULL); os_threadWaitExit(waitset->thread, NULL); } } else { if(waitset->threads){ helper = d_waitsetHelper(c_iterTakeFirst(waitset->threads)); while(helper){ helper->terminate = TRUE; u_waitsetNotify(helper->userWaitset, NULL); os_threadWaitExit(helper->tid, NULL); u_waitsetDetach(helper->userWaitset, u_entity(helper->entity->dispatcher)); u_waitsetFree(helper->userWaitset); os_free(helper); helper = d_waitsetHelper(c_iterTakeFirst(waitset->threads)); } c_iterFree(waitset->threads); waitset->threads = NULL; } } d_lockLock(d_lock(waitset)); if(waitset->entities) { we = d_waitsetEntity(c_iterTakeFirst(waitset->entities)); while(we) { if(waitset->runToCompletion == TRUE){ u_waitsetDetach(waitset->uwaitset, u_entity(we->dispatcher)); } d_waitsetEntityFree(we); we = d_waitsetEntity(c_iterTakeFirst(waitset->entities)); } c_iterFree(waitset->entities); } if(waitset->runToCompletion == TRUE){ if(waitset->uwaitset) { u_waitsetFree(waitset->uwaitset); } } d_lockUnlock(d_lock(waitset)); } }
c_bool d_waitsetDetach( d_waitset waitset, d_waitsetEntity we) { u_result ur; c_bool result = FALSE; int i; d_waitsetHelper helper; helper = NULL; assert(d_objectIsValid(d_object(waitset), D_WAITSET) == TRUE); assert(d_objectIsValid(d_object(we), D_WAITSET_ENTITY) == TRUE); if(waitset && we){ d_lockLock(d_lock(waitset)); if(c_iterContains(waitset->entities, we) == TRUE) { if(waitset->runToCompletion == TRUE){ ur = u_waitsetDetach(waitset->uwaitset, u_entity(we->dispatcher)); } else { for(i=0; i<c_iterLength(waitset->threads) && !helper; i++){ helper = d_waitsetHelper(c_iterObject(waitset->threads, i)); if(helper->entity != we){ helper = NULL; } } assert(helper); c_iterTake(waitset->threads, helper); helper->terminate = TRUE; u_waitsetNotify(helper->userWaitset, NULL); os_threadWaitExit(helper->tid, NULL); ur = u_waitsetDetach(helper->userWaitset, u_entity(we->dispatcher)); u_waitsetFree(helper->userWaitset); os_free(helper); } if(ur == U_RESULT_OK) { c_iterTake(waitset->entities, we); we->waitset = NULL; result = TRUE; } } d_lockUnlock(d_lock(waitset)); } return result; }
gapi_returnCode_t _ConditionRemoveWaitset( _Condition _this, gapi_waitSet waitset, u_waitset uWaitset) { gapi_returnCode_t result = GAPI_RETCODE_OK; u_result uResult; c_iterTake(_this->waitsets, waitset); if ((_this->uEntity != NULL) && (uWaitset != NULL)) { uResult = u_waitsetDetach(uWaitset, _this->uEntity); if (uResult != U_RESULT_OK) { result = GAPI_RETCODE_ERROR; } } return result; }
static u_result attachAndMonitor( const u_participant participant, const struct builtin_datareader_set *drset) { u_waitset waitset; u_dataReader dataReader; c_iter readers; u_result result; c_long i, length; result = U_RESULT_INTERNAL_ERROR; readers = NULL; length = 0; /*Create waitset.*/ waitset = u_waitsetNew(participant); if(waitset){ /*Set event mask of the waitset.*/ result = u_waitsetSetEventMask(waitset, V_EVENT_DATA_AVAILABLE | V_EVENT_NEW_GROUP | V_EVENT_SERVICESTATE_CHANGED); if(result == U_RESULT_OK){ result = u_dispatcherSetEventMask( (u_dispatcher)participant, V_EVENT_NEW_GROUP | V_EVENT_SERVICESTATE_CHANGED); if(result == U_RESULT_OK){ v_serviceFillNewGroups(service); result = u_waitsetAttach( waitset, (u_entity)participant, (u_entity)participant); if(result != U_RESULT_OK){ in_printf(IN_LEVEL_SEVERE, "Could not attach datareader to waitset.\n"); } } else { in_printf(IN_LEVEL_SEVERE, "Could not set event mask of participant."); } if(result == U_RESULT_OK){ readers = c_iterNew(drset->participant_dr); readers = c_iterInsert(readers, drset->publication_dr); readers = c_iterInsert(readers, drset->subscription_dr); result = U_RESULT_OK; length = c_iterLength(readers); for(i=0; i<length && (result == U_RESULT_OK); i++){ dataReader = (u_dataReader)(c_iterObject(readers, i)); /*Set event mask of the datareader to trigger on available data.*/ result = u_dispatcherSetEventMask( (u_dispatcher)dataReader, V_EVENT_DATA_AVAILABLE); if(result == U_RESULT_OK){ /*Attach reader to the waitset.*/ result = u_waitsetAttach( waitset, (u_entity)dataReader, (u_entity)dataReader); if(result != U_RESULT_OK){ in_printf(IN_LEVEL_SEVERE, "Could not attach datareader to waitset.\n"); } } else { in_printf(IN_LEVEL_SEVERE, "Could not set event mask of datareader.\n"); } } } } else { in_printf(IN_LEVEL_SEVERE, "Could not set event mask of waitset.\n"); } if(result == U_RESULT_OK){ /*Start monitoring the creation/deletion of entities.*/ result = startMonitoring(participant, waitset, drset); } u_waitsetDetach(waitset, u_entity(participant)); if(readers){ /*Detach all datareaders from the waitset.*/ for(i=0; i<length; i++){ u_waitsetDetach(waitset, (u_entity)(c_iterObject(readers, i))); } c_iterFree(readers); } /*Delete the waitset.*/ result = u_waitsetFree(waitset); if(result != U_RESULT_OK){ in_printf(IN_LEVEL_SEVERE, "Deletion of waitset failed.\n"); } } else { in_printf(IN_LEVEL_SEVERE, "Could not create waitset.\n"); } return result; }
c_bool d_waitsetAttach( d_waitset waitset, d_waitsetEntity we) { c_bool result = FALSE; u_result ur; os_result osr; d_waitsetHelper helper; c_ulong mask; d_admin admin; d_durability durability; assert(d_objectIsValid(d_object(waitset), D_WAITSET) == TRUE); assert(d_objectIsValid(d_object(we), D_WAITSET_ENTITY) == TRUE); if(waitset && we){ d_lockLock(d_lock(waitset)); if(!we->waitset) { if(c_iterContains(waitset->entities, we) == FALSE) { waitset->entities = c_iterInsert(waitset->entities, we); if(waitset->runToCompletion == TRUE){ ur = u_waitsetAttach(waitset->uwaitset, u_entity(we->dispatcher), (c_voidp)we->dispatcher); if(ur == U_RESULT_OK) { we->waitset = waitset; result = TRUE; } } else { admin = d_subscriberGetAdmin(waitset->subscriber); durability = d_adminGetDurability(admin); helper = os_malloc(C_SIZEOF(d_waitsetHelper)); helper->waitset = waitset; helper->entity = we; helper->terminate = FALSE; helper->tid = OS_THREAD_ID_NONE; helper->userWaitset = u_waitsetNew(u_participant(d_durabilityGetService(durability))); mask = V_EVENT_DATA_AVAILABLE; mask |= V_EVENT_NEW_GROUP; mask |= V_EVENT_HISTORY_DELETE; mask |= V_EVENT_HISTORY_REQUEST; mask |= V_EVENT_PERSISTENT_SNAPSHOT; mask |= V_EVENT_TRIGGER; u_waitsetSetEventMask(helper->userWaitset, mask); ur = u_waitsetAttach(helper->userWaitset, u_entity(we->dispatcher), (c_voidp)we->dispatcher); if(ur != U_RESULT_OK) { assert(FALSE); } else { result = TRUE; } if(result){ waitset->threads = c_iterInsert(waitset->threads, helper); osr = os_threadCreate(&(helper->tid), we->name, &(we->attr), d_waitsetEventHandler, helper); if(osr != os_resultSuccess){ c_iterTake(waitset->threads, helper); u_waitsetDetach(helper->userWaitset, u_entity(we->dispatcher)); u_waitsetFree(helper->userWaitset); os_free(helper); result = FALSE; } } else { u_waitsetFree(helper->userWaitset); os_free(helper); } } } } d_lockUnlock(d_lock(waitset)); } return result; }