Skip to content

Commit

Permalink
gh-87: Refactor bnd ctx to use a rwlock instead of mutex.
Browse files Browse the repository at this point in the history
Also:
 - Remove celix_bundleContext_trackService and
   celix_bundleContext_trackServiceAsync functions.
 - Update doxygen for useTrackedService* calls.
 - Remove celix_bundle_destroyServiceTrackerList. function
   by return an array list with a configured remove callback.
 - Remove waitTimeoutInSeconds option for
   celix_tracked_service_use_options_t.
  • Loading branch information
pnoltes committed Feb 25, 2024
1 parent 5446bf0 commit eb6e699
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 237 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ limitations under the License.
- The signature of `celix_bundleContext_trackServices` has changed. The signature is now simpler to better support
the use-case of using a service tracker with the `celix_bundleContext_useTrackedService*` functions.
The `celix_bundleContext_trackServicesWithOptions` is still available for more advanced use-cases.
- Function `celix_bundle_destroyServiceTrackerList` is removed. The returned array list from
`celix_bundle_listServiceTrackers` is now configured to destroy the service trackers info entries.

## New Features

Expand Down
6 changes: 3 additions & 3 deletions bundles/logging/log_admin/gtest/src/LogAdminTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ TEST_F(LogBundleTestSuite, NrOfLogServices) {
EXPECT_EQ(1, control->nrOfLogServices(control->handle, nullptr)); //default the framework log services is available

//request "default" log service
long trkId1 = celix_bundleContext_trackService(ctx.get(), CELIX_LOG_SERVICE_NAME);
long trkId1 = celix_bundleContext_trackServices(ctx.get(), CELIX_LOG_SERVICE_NAME);
EXPECT_EQ(2, control->nrOfLogServices(control->handle, nullptr));

//request "default" log service -> already created
long trkId2 = celix_bundleContext_trackService(ctx.get(), CELIX_LOG_SERVICE_NAME);
long trkId2 = celix_bundleContext_trackServices(ctx.get(), CELIX_LOG_SERVICE_NAME);
EXPECT_EQ(2, control->nrOfLogServices(control->handle, nullptr));

//request a 'logger1' log service
Expand Down Expand Up @@ -225,7 +225,7 @@ TEST_F(LogBundleTestSuite, SinkLogControl) {

TEST_F(LogBundleTestSuite, LogServiceControl) {
//request "default" log service
long trkId1 = celix_bundleContext_trackService(ctx.get(), CELIX_LOG_SERVICE_NAME);
long trkId1 = celix_bundleContext_trackServices(ctx.get(), CELIX_LOG_SERVICE_NAME);
celix_framework_waitForEmptyEventQueue(fw.get());
EXPECT_EQ(2, control->nrOfLogServices(control->handle, nullptr));

Expand Down
2 changes: 1 addition & 1 deletion bundles/shell/shell/src/query_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static void queryCommand_callback(void *handle, const celix_bundle_t *bnd) {
}
}
}
celix_bundle_destroyServiceTrackerList(trackers);
celix_arrayList_destroy(trackers);
}

if (printBundleCalled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ TEST_F(CelixBundleContextBundlesTestSuite, BundleInfoTests) {
auto *services = celix_bundle_listRegisteredServices(bnd);
data->requestedCount = celix_arrayList_size(trackers);
data->provideCount = celix_arrayList_size(services);
celix_bundle_destroyServiceTrackerList(trackers);
celix_arrayList_destroy(trackers);
celix_bundle_destroyRegisteredServicesList(services);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ TEST_F(CelixBundleContextServicesTestSuite, TrackServiceTrackerTest) {
EXPECT_TRUE(trackerId >= 0);
EXPECT_EQ(0, count);

long tracker2 = celix_bundleContext_trackService(ctx, "example");
long tracker2 = celix_bundleContext_trackServices(ctx, "example");
EXPECT_TRUE(tracker2 >= 0);
EXPECT_EQ(1, count);

Expand Down
12 changes: 3 additions & 9 deletions libs/framework/include/celix_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,17 @@ typedef struct celix_bundle_service_tracker_list_entry {
size_t nrOfTrackedServices;
} celix_bundle_service_tracker_list_entry_t;


/**
* Returns a array list of service tracker info entries for this bundle.
*
* @warning It requires a valid bundle context. Calling it for an inactive bundle will lead to crash.
*
* @param ctx The bundle context
* @param bndId The bundle id for which the services should be listed
* @return A celix array list with celix_bundle_service_tracker_list_entry_t*. Caller is owner of the celix array.
* @return A celix array list with celix_bundle_service_tracker_list_entry_t*. Caller is owner of the celix
* array. The returned list should be freed using celix_arrayList_destroy.
*/
CELIX_FRAMEWORK_EXPORT celix_array_list_t* celix_bundle_listServiceTrackers(const celix_bundle_t *bnd);

/**
* Utils function to free memory for the return of a celix_bundle_listServiceTrackers call.
*/
CELIX_FRAMEWORK_EXPORT void celix_bundle_destroyServiceTrackerList(celix_array_list_t* list);

CELIX_FRAMEWORK_EXPORT celix_array_list_t* celix_bundle_listServiceTrackers(const celix_bundle_t* bnd);

#ifdef __cplusplus
}
Expand Down
297 changes: 148 additions & 149 deletions libs/framework/include/celix_bundle_context.h

Large diffs are not rendered by default.

31 changes: 13 additions & 18 deletions libs/framework/src/bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,18 @@ void celix_bundle_destroyRegisteredServicesList(celix_array_list_t* list) {
}
}

static void celix_bundle_destroyServiceTrackerListCallback(void *data) {
celix_bundle_service_tracker_list_entry_t *entry = data;
free(entry->filter);
free(entry->serviceName);
free(entry);
}

celix_array_list_t* celix_bundle_listServiceTrackers(const celix_bundle_t *bnd) {
celix_array_list_t* result = celix_arrayList_create();
celixThreadMutex_lock(&bnd->context->mutex);
celix_array_list_create_options_t opts = CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS;
opts.simpleRemovedCallback = celix_bundle_destroyServiceTrackerListCallback;
celix_array_list_t* result = celix_arrayList_createWithOptions(&opts);
celixThreadRwlock_readLock(&bnd->context->lock);
CELIX_LONG_HASH_MAP_ITERATE(bnd->context->serviceTrackers, iter) {
celix_bundle_context_service_tracker_entry_t *trkEntry = iter.value.ptrValue;
if (trkEntry->tracker != NULL) {
Expand All @@ -597,28 +606,14 @@ celix_array_list_t* celix_bundle_listServiceTrackers(const celix_bundle_t *bnd)
} else {
framework_logIfError(bnd->framework->logger, CELIX_BUNDLE_EXCEPTION, NULL,
"Failed to get service name from tracker. filter is %s", entry->filter);
free(entry->filter);
free(entry);
celix_bundle_destroyServiceTrackerListCallback(entry);
}
}
}
celixThreadMutex_unlock(&bnd->context->mutex);
celixThreadRwlock_unlock(&bnd->context->lock);
return result;
}


void celix_bundle_destroyServiceTrackerList(celix_array_list_t* list) {
if (list != NULL) {
for (int i = 0; i < celix_arrayList_size(list); ++i) {
celix_bundle_service_tracker_list_entry_t *entry = celix_arrayList_get(list, i);
free(entry->filter);
free(entry->serviceName);
free(entry);
}
celix_arrayList_destroy(list);
}
}

bundle_archive_t* celix_bundle_getArchive(const celix_bundle_t *bundle) {
bundle_archive_t* archive = NULL;
bundle_getArchive(bundle, &archive);
Expand Down
Loading

0 comments on commit eb6e699

Please sign in to comment.