PengZheng commented on code in PR #583:
URL: https://github.com/apache/celix/pull/583#discussion_r1247865846


##########
libs/framework/src/framework.c:
##########
@@ -2440,6 +2599,127 @@ void 
celix_framework_waitUntilNoPendingRegistration(celix_framework_t* fw)
     celixThreadMutex_unlock(&fw->dispatcher.mutex);
 }
 
+long celix_framework_scheduleEvent(celix_framework_t* fw,
+                                    long bndId,
+                                    const char* eventName,
+                                    double initialDelayInSeconds,
+                                    double intervalInSeconds,
+                                    void* callbackData,
+                                    void (*callback)(void*),
+                                    void* removeCallbackData,
+                                    void (*removeCallback)(void*)) {
+    if (callback == NULL) {
+        fw_log(fw->logger,
+               CELIX_LOG_LEVEL_ERROR,
+               "Cannot add scheduled event for bundle id %li. Invalid NULL 
event callback.",
+               bndId);
+        return -1;
+    }
+
+    celix_framework_bundle_entry_t* bndEntry = 
celix_framework_bundleEntry_getBundleEntryAndIncreaseUseCount(fw, bndId);
+    if (bndEntry == NULL) {
+        fw_log(fw->logger, CELIX_LOG_LEVEL_ERROR, "Cannot add scheduled event 
for non existing bundle id %li.", bndId);
+        return -1;
+    }
+    celix_scheduled_event_t* newEvent = celix_scheduledEvent_create(fw,
+                                                                 
bndEntry->bndId,
+                                                                 
celix_framework_nextScheduledEventId(fw),
+                                                                 eventName,
+                                                                 
initialDelayInSeconds,
+                                                                 
intervalInSeconds,
+                                                                 callbackData,
+                                                                 callback,
+                                                                 
removeCallbackData,
+                                                                 
removeCallback);
+    CELIX_SCHEDULED_EVENT_RETAIN_GUARD(event, newEvent);
+    celix_framework_bundleEntry_decreaseUseCount(bndEntry);
+
+    if (event == NULL) {
+        return -1L; //error logged by celix_scheduledEvent_create
+    }
+
+    fw_log(fw->logger,
+           CELIX_LOG_LEVEL_DEBUG,
+           "Added scheduled event '%s' (id=%li) for bundle '%s' (id=%li).",
+           celix_scheduledEvent_getName(event),
+           celix_scheduledEvent_getId(event),
+           celix_bundle_getSymbolicName(bndEntry->bnd),
+           bndId);
+
+    celixThreadMutex_lock(&fw->dispatcher.mutex);
+    celix_longHashMap_put(fw->dispatcher.scheduledEvents, 
celix_scheduledEvent_getId(event), event);
+    celixThreadCondition_broadcast(&fw->dispatcher.cond); //notify dispatcher 
thread for newly added scheduled event
+    celixThreadMutex_unlock(&fw->dispatcher.mutex);
+
+    return celix_scheduledEvent_getId(event);
+}
+
+celix_status_t celix_framework_wakeupScheduledEvent(celix_framework_t* fw, 
long scheduledEventId) {
+    celixThreadMutex_lock(&fw->dispatcher.mutex);
+    celix_scheduled_event_t* event = 
celix_longHashMap_get(fw->dispatcher.scheduledEvents, scheduledEventId);
+    if (event != NULL) {
+        celix_scheduledEvent_markForWakeup(event);
+        celixThreadCondition_broadcast(&fw->dispatcher.cond); //notify 
dispatcher thread for configured wakeup
+    }
+    celixThreadMutex_unlock(&fw->dispatcher.mutex);
+
+    if (event == NULL) {
+        fw_log(fw->logger,
+               CELIX_LOG_LEVEL_WARNING,
+               "celix_framework_wakeupScheduledEvent called with unknown 
scheduled event id %li.",
+               scheduledEventId);
+        return CELIX_ILLEGAL_ARGUMENT;
+    }
+
+    return CELIX_SUCCESS;
+}
+
+celix_status_t
+celix_framework_waitForScheduledEvent(celix_framework_t* fw, long 
scheduledEventId, double waitTimeInSeconds) {
+    celixThreadMutex_lock(&fw->dispatcher.mutex);
+    CELIX_SCHEDULED_EVENT_RETAIN_GUARD(event, 
celix_longHashMap_get(fw->dispatcher.scheduledEvents, scheduledEventId));
+    celixThreadMutex_unlock(&fw->dispatcher.mutex);
+
+    if (event == NULL) {

Review Comment:
   Quick fix: 
https://github.com/apache/celix/commit/6e3aa032b6017cd3e911ff06e3c85be454ccc2d1



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to