diff --git a/src/c/autoevent.c b/src/c/autoevent.c index cd8f4783..c39c4caa 100644 --- a/src/c/autoevent.c +++ b/src/c/autoevent.c @@ -19,6 +19,9 @@ #include +static int init_flg_mstr = -1; +static int init_flg_slv = -1; + typedef struct edgex_autoimpl { devsdk_service_t *svc; @@ -45,6 +48,7 @@ static void edgex_autoimpl_release (edgex_autoimpl *ai) static void *ae_runner (void *p) { + sem_wait (&slv_mutex); edgex_autoimpl *ai = (edgex_autoimpl *)p; atomic_fetch_add (&ai->refs, 1); @@ -55,6 +59,7 @@ static void *ae_runner (void *p) { edgex_device_release (ai->svc, dev); edgex_autoimpl_release (ai); + sem_post (&slv_mutex); return NULL; } edgex_device_alloc_crlid (NULL); @@ -138,6 +143,7 @@ static void *ae_runner (void *p) } } edgex_autoimpl_release (ai); + sem_post (&slv_mutex); return NULL; } @@ -154,6 +160,17 @@ static void *starter (void *p) void edgex_device_autoevent_start (devsdk_service_t *svc, edgex_device *dev) { + if (init_flg_mstr != 0) + { + init_flg_mstr = sem_init (&mstr_mutex, 0, 1); + } + + if (init_flg_slv != 0) + { + init_flg_slv = sem_init (&slv_mutex, 0, 1) + } + + sem_wait (&mstr_mutex); for (edgex_device_autoevents *ae = dev->autos; ae; ae = ae->next) { if (ae->impl == NULL) @@ -202,6 +219,7 @@ void edgex_device_autoevent_start (devsdk_service_t *svc, edgex_device *dev) iot_schedule_add (ae->impl->svc->scheduler, ae->impl->handle); } } + sem_post (&mstr_mutex); } static void stopper (edgex_autoimpl *ai) @@ -230,3 +248,44 @@ void edgex_device_autoevent_stop (edgex_device *dev) } } } + +void edgex_device_autoevent_stop_to_update (edgex_device *dev) +{ + if (init_flg_mstr != 0) + { + init_flg_mstr = sem_init (&mstr_mutex, 0, 1); + } + + if (init_flg_slv != 0) + { + init_flg_slv = sem_init (&slv_mutex, 0, 1); + } + + sem_wait (&mstr_mutex); + for (edgex_device_autoevents *ae = dev->autos; ae; ae = ae->next) + { + if (ae->impl) + { + sem_wait (&slv_mutex); + stopper (ae->impl); + ae->impl = NULL; + sem_post (&slv_mutex); + } + } + sem_post (&mstr_mutex); +} + +void clear_ae_signals () +{ + if (init_flg_mstr == 0) + { + sem_destroy (&mstr_mutex); + init_flg_mstr = -1; + } + + if (init_flg_slv == 0) + { + sem_destroy (&slv_mutex); + init_flg_slv = -1; + } +} diff --git a/src/c/autoevent.h b/src/c/autoevent.h index b6c3be08..4ca779cf 100644 --- a/src/c/autoevent.h +++ b/src/c/autoevent.h @@ -5,14 +5,23 @@ * SPDX-License-Identifier: Apache-2.0 * */ +#include #ifndef _EDGEX_DEVICE_AUTOEVENT_H #define _EDGEX_DEVICE_AUTOEVENT_H 1 #include "service.h" +sem_t mstr_mutex; + +sem_t slv_mutex; + void edgex_device_autoevent_start (devsdk_service_t *svc, edgex_device *dev); void edgex_device_autoevent_stop (edgex_device *dev); +void edgex_device_autoevent_stop_to_update (edgex_device *dev); + +void clear_ae_signals (); + #endif diff --git a/src/c/devmap.c b/src/c/devmap.c index b8677e31..9759a3c1 100644 --- a/src/c/devmap.c +++ b/src/c/devmap.c @@ -65,6 +65,7 @@ void edgex_devmap_clear (edgex_devmap_t *map) edgex_map_remove (&map->devices, key); key = next; } + clear_ae_signals (); pthread_rwlock_unlock (&map->lock); } @@ -348,7 +349,7 @@ void edgex_devmap_update_profile (devsdk_service_t *svc, edgex_deviceprofile *dp edgex_device **dev = edgex_map_get (&svc->devices->devices, key); if ((*dev)->profile == old) { - edgex_device_autoevent_stop (*dev); + edgex_device_autoevent_stop_to_update (*dev); (*dev)->profile = dp; edgex_device_autoevent_start (svc, *dev); }