PengZheng commented on code in PR #583: URL: https://github.com/apache/celix/pull/583#discussion_r1247863206
########## libs/framework/src/framework.c: ########## @@ -1408,32 +1426,153 @@ static inline bool fw_removeTopEventFromQueue(celix_framework_t* fw) { static inline void fw_handleEvents(celix_framework_t* framework) { celixThreadMutex_lock(&framework->dispatcher.mutex); int size = framework->dispatcher.eventQueueSize + celix_arrayList_size(framework->dispatcher.dynamicEventQueue); - if (size == 0 && framework->dispatcher.active) { - celixThreadCondition_timedwaitRelative(&framework->dispatcher.cond, &framework->dispatcher.mutex, 1, 0); - } - size = framework->dispatcher.eventQueueSize + celix_arrayList_size(framework->dispatcher.dynamicEventQueue); celixThreadMutex_unlock(&framework->dispatcher.mutex); while (size > 0) { celix_framework_event_t* topEvent = fw_topEventFromQueue(framework); fw_handleEventRequest(framework, topEvent); - bool dynamiclyAllocatedEvent = fw_removeTopEventFromQueue(framework); + bool dynamicallyAllocatedEvent = fw_removeTopEventFromQueue(framework); if (topEvent->bndEntry != NULL) { celix_framework_bundleEntry_decreaseUseCount(topEvent->bndEntry); } free(topEvent->serviceName); - if (dynamiclyAllocatedEvent) { + if (dynamicallyAllocatedEvent) { free(topEvent); } celixThreadMutex_lock(&framework->dispatcher.mutex); size = framework->dispatcher.eventQueueSize + celix_arrayList_size(framework->dispatcher.dynamicEventQueue); - celixThreadCondition_broadcast(&framework->dispatcher.cond); celixThreadMutex_unlock(&framework->dispatcher.mutex); } } +/** + * @brief Process all scheduled events. + */ +static double celix_framework_processScheduledEvents(celix_framework_t* fw) { + struct timespec ts = celixThreadCondition_getTime(); + + double nextClosestScheduledEvent; + celix_scheduled_event_t* callEvent; + celix_scheduled_event_t* removeEvent; + do { + nextClosestScheduledEvent = -1; //negative means no event next event + callEvent = NULL; + removeEvent = NULL; + double nextEvent; + celixThreadMutex_lock(&fw->dispatcher.mutex); + CELIX_LONG_HASH_MAP_ITERATE(fw->dispatcher.scheduledEvents, entry) { + celix_scheduled_event_t* visit = entry.value.ptrValue; + if (celix_scheduledEvent_isMarkedForRemoval(visit)) { + removeEvent = visit; + celix_longHashMap_remove(fw->dispatcher.scheduledEvents, celix_scheduledEvent_getId(visit)); + break; + } + + bool call = celix_scheduledEvent_deadlineReached(visit, &ts, &nextEvent); + if (nextClosestScheduledEvent < 0 || nextEvent < nextClosestScheduledEvent) { + nextClosestScheduledEvent = nextEvent; + } + if (call) { + callEvent = visit; + if (celix_scheduledEvent_isSingleShot(visit)) { + removeEvent = visit; + celix_longHashMap_remove(fw->dispatcher.scheduledEvents, celix_scheduledEvent_getId(visit)); + } + break; + } + } + celixThreadMutex_unlock(&fw->dispatcher.mutex); + + if (callEvent != NULL) { + celix_scheduledEvent_process(callEvent, &ts); + } + if (removeEvent != NULL) { + const char* formatStr = celix_scheduledEvent_isSingleShot(removeEvent) ? + "Removing processed one-shot scheduled event '%s' (id=%li) for bundle if %li.": + "Removing processed scheduled event '%s' (id=%li) for bundle if %li."; + fw_log(fw->logger, + CELIX_LOG_LEVEL_DEBUG, + formatStr, Review Comment: Quick fix: https://github.com/apache/celix/pull/583/commits/1ac3e3e1bdd9c488e3a8ee715a1fc66785c5e41b -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org