Repository: celix Updated Branches: refs/heads/develop 5872268f2 -> a4a1f5000
CELIX-446: Removes the clang block example, does not work as expected Project: http://git-wip-us.apache.org/repos/asf/celix/repo Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/a4a1f500 Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/a4a1f500 Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/a4a1f500 Branch: refs/heads/develop Commit: a4a1f5000f0f66683923fdf9dec7ca1a5c97e09f Parents: 5872268 Author: Pepijn Noltes <[email protected]> Authored: Thu May 10 16:03:42 2018 +0200 Committer: Pepijn Noltes <[email protected]> Committed: Thu May 10 16:03:42 2018 +0200 ---------------------------------------------------------------------- .../services_example_c/CMakeLists.txt | 6 +- .../services_example_c/src/consumer_example.c | 60 ++++++++++---------- framework/include/bundle_context.h | 10 ++++ 3 files changed, 46 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/celix/blob/a4a1f500/examples/celix-examples/services_example_c/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/examples/celix-examples/services_example_c/CMakeLists.txt b/examples/celix-examples/services_example_c/CMakeLists.txt index 6f7913b..f56ddd8 100644 --- a/examples/celix-examples/services_example_c/CMakeLists.txt +++ b/examples/celix-examples/services_example_c/CMakeLists.txt @@ -31,10 +31,14 @@ add_celix_bundle(consumer_example ) target_link_libraries(consumer_example PRIVATE services_example_c_api) +if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") + target_compile_definitions(consumer_example PRIVATE USE_NESTED_FUNCTION_EXAMPLE) +endif() + add_celix_container(services_example_c BUNDLES Celix::shell Celix::shell_tui provider_example consumer_example -) \ No newline at end of file +) http://git-wip-us.apache.org/repos/asf/celix/blob/a4a1f500/examples/celix-examples/services_example_c/src/consumer_example.c ---------------------------------------------------------------------- diff --git a/examples/celix-examples/services_example_c/src/consumer_example.c b/examples/celix-examples/services_example_c/src/consumer_example.c index c472662..899cb57 100644 --- a/examples/celix-examples/services_example_c/src/consumer_example.c +++ b/examples/celix-examples/services_example_c/src/consumer_example.c @@ -27,12 +27,12 @@ #include "bundle_activator.h" typedef struct activator_data { - int trackCount; celix_bundle_context_t *ctx; pthread_t thread; pthread_mutex_t mutex; //protects running bool running; + int trackCount; } activator_data_t; static bool isRunning(activator_data_t *data) { @@ -62,7 +62,7 @@ static void useCalc(void *handle, void *svc) { } static void gccExample(activator_data_t *data) { -#ifdef __GNUC__ +#ifdef USE_NESTED_FUNCTION_EXAMPLE int result = 0; long rank = 0; @@ -88,41 +88,33 @@ static void gccExample(activator_data_t *data) { #endif } -static void clangExample(activator_data_t *data) { -#ifdef __clang__ - /*TODO -#include <Block.h> - __block result = 0; - __block rank = 0; - __block svcId = 0; - - void (^use)(void *handle, void *svc, const celix_properties_t *props) = ^(void *handle, void *svc, const celix_properties_t *props) { - example_calc_t *calc = svc; - rank = celix_properties_getAsLong(props, OSGI_FRAMEWORK_SERVICE_RANKING, -1L); - svcId = celix_properties_getAsLong(props, OSGI_FRAMEWORK_SERVICE_ID, -1L); - result = calc->calc(calc->handle, 1); - }; - - celix_service_use_options_t opts; - memset(&opts, 0, sizeof(opts)); +static void addSvc(activator_data_t *data, void *svc __attribute__((unused))) { + pthread_mutex_lock(&data->mutex); + data->trackCount += 1; + pthread_mutex_unlock(&data->mutex); +} - opts.serviceName = EXAMPLE_CALC_NAME; - opts.callbackHandle = NULL; //can be null for trampolines - opts.useWithProperties = use; - bool called = celix_bundleContext_useServiceWithOptions(data->ctx, &opts); +static void removeSvc(activator_data_t *data, void *svc __attribute__((unused))) { + pthread_mutex_lock(&data->mutex); + data->trackCount -= 1; + pthread_mutex_unlock(&data->mutex); +} - printf("Called func %s. Result is %i, rank is %li and svc id is %li\n", called ? "called" : "not called", result, rank, svcId); - */ -#endif +static void useHighest(activator_data_t *data __attribute__((unused)), example_calc_t *svc, const celix_properties_t *props) { + int result = svc->calc(svc->handle, 2); + long svcId = celix_properties_getAsLong(props, OSGI_FRAMEWORK_SERVICE_ID, -1L); + long rank = celix_properties_getAsLong(props, OSGI_FRAMEWORK_SERVICE_RANKING, -1L); + printf("Called highest ranking service. Result is %i, svc id is %li, svc ranking is %li\n", result, svcId, rank); } + void * run(void *handle) { activator_data_t *data = handle; printf("starting consumer thread\n"); + long trkId = celix_bundleContext_trackServices(data->ctx, EXAMPLE_CALC_NAME, data, (void*)addSvc, (void*)removeSvc); while (isRunning(data)) { - struct info info; info.result = 0; info.count = 0; @@ -131,13 +123,23 @@ void * run(void *handle) { gccExample(data); //gcc trampolines example (nested functions) - clangExample(data); //TODO use clang blocks + celix_service_use_options_t opts; + memset(&opts, 0, sizeof(opts)); + + opts.serviceName = EXAMPLE_CALC_NAME; + opts.callbackHandle = data; + opts.useWithProperties = (void*)useHighest; + celix_bundleContext_useServiceWithOptions(data->ctx, &opts); - //TODO tracker example + pthread_mutex_lock(&data->mutex); + int count = data->trackCount; + pthread_mutex_unlock(&data->mutex); + printf("Current tracking count is %i\n", count); sleep(5); } + celix_bundleContext_stopTracker(data->ctx, trkId); printf("exiting consumer thread\n"); pthread_exit(NULL); http://git-wip-us.apache.org/repos/asf/celix/blob/a4a1f500/framework/include/bundle_context.h ---------------------------------------------------------------------- diff --git a/framework/include/bundle_context.h b/framework/include/bundle_context.h index 5ab777a..ec974fa 100644 --- a/framework/include/bundle_context.h +++ b/framework/include/bundle_context.h @@ -230,6 +230,16 @@ long celix_bundleContext_registerServiceForLang(celix_bundle_context_t *ctx, con */ void celix_bundleContext_unregisterService(celix_bundle_context_t *ctx, long serviceId); + +//TODO +//typedef struct celix_service_find_filter_options { +// servicname, versionrange, filter, lang +//} celix_service_find_filter_options_t; +//celix_array_list_t* celix_bundleContext_findServices(celix_bundle_context_t *ctx, const char *serviceName); +//celix_array_list_t* celix_bundleContext_findServices(celix_bundle_context_t *ctx, const char *serviceName); +//alts WithOptions + + /** * track service for the provided serviceName and/or filter. * The highest ranking services will used for the callback.
