# HG changeset patch # User Paul Boddie # Date 1705859434 -3600 # Node ID 4c5fc873da6e164f062766217f2066461c025eca # Parent b6288af98f9345966fb5950e10a8a0ec31bf8fe5 Employ a singular per-task wrapper for the per-task notifier object. diff -r b6288af98f93 -r 4c5fc873da6e libfsclient/lib/src/notify.cc --- a/libfsclient/lib/src/notify.cc Sun Jan 21 18:44:44 2024 +0100 +++ b/libfsclient/lib/src/notify.cc Sun Jan 21 18:50:34 2024 +0100 @@ -1,7 +1,7 @@ /* * Notification-related functions. * - * Copyright (C) 2023 Paul Boddie + * Copyright (C) 2023, 2024 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -19,6 +19,7 @@ * Boston, MA 02110-1301, USA */ +#include #include #include "notify.h" @@ -32,6 +33,16 @@ ObjectNotifier *obj; }; +/* Lock protecting per-task notifier access. */ + +static std::mutex _task_lock; + +/* Per-task storage for the wrapped notifier. */ + +static struct notifier *_notifier = NULL; + + + /* Return the notifiable details from a compatible object. */ notifiable_t *notify_notifiable(notifiable_base_t *notifiable) @@ -57,8 +68,13 @@ void notify_close(notifier_t *notifier) { + std::lock_guard guard(_task_lock); + delete notifier->obj; delete notifier; + + if (notifier == _notifier) + _notifier = NULL; } /* Obtain a local notifier object. */ @@ -75,10 +91,13 @@ notifier_t *notify_get_task() { - notifier_t *notifier = new notifier_t; + std::lock_guard guard(_task_lock); - notifier->obj = notifier_get_task_notifier(); - return notifier; + if (_notifier == NULL) + _notifier = new notifier_t; + + _notifier->obj = notifier_get_task_notifier(); + return _notifier; } /* Subscribe to notification events. */