Various services improved (#110)
This commit is contained in:
committed by
GitHub
parent
36bb25deba
commit
d52fe52d96
@@ -1,42 +1,11 @@
|
||||
#include "Pubsub.h"
|
||||
#include "Check.h"
|
||||
#include "Mutex.h"
|
||||
#include <list>
|
||||
|
||||
namespace tt {
|
||||
|
||||
struct PubSubSubscription {
|
||||
uint64_t id;
|
||||
PubSubCallback callback;
|
||||
void* callback_context;
|
||||
};
|
||||
|
||||
typedef std::list<PubSubSubscription> Subscriptions;
|
||||
|
||||
struct PubSub {
|
||||
uint64_t last_id = 0;
|
||||
Subscriptions items;
|
||||
Mutex* mutex;
|
||||
};
|
||||
|
||||
PubSub* tt_pubsub_alloc() {
|
||||
auto* pubsub = new PubSub();
|
||||
|
||||
pubsub->mutex = tt_mutex_alloc(MutexTypeNormal);
|
||||
tt_assert(pubsub->mutex);
|
||||
|
||||
return pubsub;
|
||||
}
|
||||
|
||||
void tt_pubsub_free(PubSub* pubsub) {
|
||||
tt_assert(pubsub);
|
||||
tt_check(pubsub->items.empty());
|
||||
tt_mutex_free(pubsub->mutex);
|
||||
delete pubsub;
|
||||
}
|
||||
|
||||
PubSubSubscription* tt_pubsub_subscribe(PubSub* pubsub, PubSubCallback callback, void* callback_context) {
|
||||
tt_check(tt_mutex_acquire(pubsub->mutex, TtWaitForever) == TtStatusOk);
|
||||
PubSubSubscription* tt_pubsub_subscribe(std::shared_ptr<PubSub> pubsub, PubSubCallback callback, void* callback_context) {
|
||||
tt_check(pubsub->mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
PubSubSubscription subscription = {
|
||||
.id = (++pubsub->last_id),
|
||||
.callback = callback,
|
||||
@@ -46,16 +15,16 @@ PubSubSubscription* tt_pubsub_subscribe(PubSub* pubsub, PubSubCallback callback,
|
||||
subscription
|
||||
);
|
||||
|
||||
tt_check(tt_mutex_release(pubsub->mutex) == TtStatusOk);
|
||||
tt_check(pubsub->mutex.release() == TtStatusOk);
|
||||
|
||||
return (PubSubSubscription*)pubsub->last_id;
|
||||
}
|
||||
|
||||
void tt_pubsub_unsubscribe(PubSub* pubsub, PubSubSubscription* pubsub_subscription) {
|
||||
void tt_pubsub_unsubscribe(std::shared_ptr<PubSub> pubsub, PubSubSubscription* pubsub_subscription) {
|
||||
tt_assert(pubsub);
|
||||
tt_assert(pubsub_subscription);
|
||||
|
||||
tt_check(tt_mutex_acquire(pubsub->mutex, TtWaitForever) == TtStatusOk);
|
||||
tt_check(pubsub->mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
bool result = false;
|
||||
auto id = (uint64_t)pubsub_subscription;
|
||||
for (auto it = pubsub->items.begin(); it != pubsub->items.end(); it++) {
|
||||
@@ -66,19 +35,19 @@ void tt_pubsub_unsubscribe(PubSub* pubsub, PubSubSubscription* pubsub_subscripti
|
||||
}
|
||||
}
|
||||
|
||||
tt_check(tt_mutex_release(pubsub->mutex) == TtStatusOk);
|
||||
tt_check(pubsub->mutex.release() == TtStatusOk);
|
||||
tt_check(result);
|
||||
}
|
||||
|
||||
void tt_pubsub_publish(PubSub* pubsub, void* message) {
|
||||
tt_check(tt_mutex_acquire(pubsub->mutex, TtWaitForever) == TtStatusOk);
|
||||
void tt_pubsub_publish(std::shared_ptr<PubSub> pubsub, void* message) {
|
||||
tt_check(pubsub->mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
|
||||
// Iterate over subscribers
|
||||
for (auto& it : pubsub->items) {
|
||||
it.callback(message, it.callback_context);
|
||||
}
|
||||
|
||||
tt_check(tt_mutex_release(pubsub->mutex) == TtStatusOk);
|
||||
tt_check(pubsub->mutex.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user