1.1 --- a/libfsclient/lib/src/notify.cc Sun Jan 21 18:44:44 2024 +0100
1.2 +++ b/libfsclient/lib/src/notify.cc Sun Jan 21 18:50:34 2024 +0100
1.3 @@ -1,7 +1,7 @@
1.4 /*
1.5 * Notification-related functions.
1.6 *
1.7 - * Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -19,6 +19,7 @@
1.13 * Boston, MA 02110-1301, USA
1.14 */
1.15
1.16 +#include <mutex>
1.17 #include <notifier/notifier.h>
1.18
1.19 #include "notify.h"
1.20 @@ -32,6 +33,16 @@
1.21 ObjectNotifier *obj;
1.22 };
1.23
1.24 +/* Lock protecting per-task notifier access. */
1.25 +
1.26 +static std::mutex _task_lock;
1.27 +
1.28 +/* Per-task storage for the wrapped notifier. */
1.29 +
1.30 +static struct notifier *_notifier = NULL;
1.31 +
1.32 +
1.33 +
1.34 /* Return the notifiable details from a compatible object. */
1.35
1.36 notifiable_t *notify_notifiable(notifiable_base_t *notifiable)
1.37 @@ -57,8 +68,13 @@
1.38
1.39 void notify_close(notifier_t *notifier)
1.40 {
1.41 + std::lock_guard<std::mutex> guard(_task_lock);
1.42 +
1.43 delete notifier->obj;
1.44 delete notifier;
1.45 +
1.46 + if (notifier == _notifier)
1.47 + _notifier = NULL;
1.48 }
1.49
1.50 /* Obtain a local notifier object. */
1.51 @@ -75,10 +91,13 @@
1.52
1.53 notifier_t *notify_get_task()
1.54 {
1.55 - notifier_t *notifier = new notifier_t;
1.56 + std::lock_guard<std::mutex> guard(_task_lock);
1.57
1.58 - notifier->obj = notifier_get_task_notifier();
1.59 - return notifier;
1.60 + if (_notifier == NULL)
1.61 + _notifier = new notifier_t;
1.62 +
1.63 + _notifier->obj = notifier_get_task_notifier();
1.64 + return _notifier;
1.65 }
1.66
1.67 /* Subscribe to notification events. */