Skip to content

Commit

Permalink
gh-87: Address review issues in gh-734.
Browse files Browse the repository at this point in the history
1. Use acquire-release pair to synchronize the service user with the service stopper.
2. Reduce CPU usage when waiting for service user.
3. Fix gcc -Wstringop-truncation warning.
  • Loading branch information
PengZheng committed Mar 4, 2024
1 parent e665364 commit 3c3227a
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions libs/framework/src/bundle_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ static void celix_bundleContext_waitForUnusedServiceTracker(celix_bundle_context
// note that the use count cannot be increased anymore, because the tracker is removed from the map
struct timespec start = celix_gettime(CLOCK_MONOTONIC);
int logCount = 0;
while (__atomic_load_n(&trkEntry->useCount, __ATOMIC_RELAXED) > 0) {
while (__atomic_load_n(&trkEntry->useCount, __ATOMIC_ACQUIRE) > 0) {
if (celix_elapsedtime(CLOCK_MONOTONIC, start) > TRACKER_WARN_THRESHOLD_SEC) {
fw_log(ctx->framework->logger,
CELIX_LOG_LEVEL_WARNING,
Expand All @@ -858,7 +858,7 @@ static void celix_bundleContext_waitForUnusedServiceTracker(celix_bundle_context
start = celix_gettime(CLOCK_MONOTONIC);
logCount++;
}
usleep(1);
usleep(1000);
}
}

Expand Down Expand Up @@ -1082,7 +1082,7 @@ bool celix_bundleContext_useServiceWithId(
const char *serviceName,
void *callbackHandle,
void (*use)(void *handle, void *svc)) {
char filter[32]; //20 is max long length
char filter[1+/*(*/sizeof(CELIX_FRAMEWORK_SERVICE_ID)-1+1/*=*/+20/*INT64_MIN*/+1/*)*/+1/*'\0'*/];
(void)snprintf(filter, sizeof(filter), "(%s=%li)", CELIX_FRAMEWORK_SERVICE_ID, serviceId);

celix_service_use_options_t opts = CELIX_EMPTY_SERVICE_USE_OPTIONS;
Expand Down Expand Up @@ -1458,7 +1458,7 @@ static size_t celix_bundleContext_useTrackedServiceWithOptionsInternal(celix_bun
trkEntry->tracker, NULL, opts->callbackHandle, opts->use, opts->useWithProperties, opts->useWithOwner);
}

(void)__atomic_fetch_sub(&trkEntry->useCount, 1, __ATOMIC_RELAXED);
(void)__atomic_fetch_sub(&trkEntry->useCount, 1, __ATOMIC_RELEASE);
return callCount;
}

Expand Down

0 comments on commit 3c3227a

Please sign in to comment.