Skip to content

Commit

Permalink
wip: channel: Delete later event
Browse files Browse the repository at this point in the history
  • Loading branch information
shramov committed Dec 27, 2024
1 parent 1f1e107 commit 05a0927
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/channel/impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ int tll_channel_internal_child_del(tll_channel_internal_t *ptr, const tll_channe
tll_channel_callback_del((tll_channel_t *) c, _state_callback, ptr, TLL_MESSAGE_MASK_STATE);
return 0;
}

int tll_channel_internal_delete_later(tll_channel_internal_t *ptr)
{
tll_msg_t msg = {.type = TLL_MESSAGE_CHANNEL, .msgid = TLL_MESSAGE_CHANNEL_DEFERRED_DELETE};
tll_logger_printf(ptr->logger, TLL_LOGGER_INFO, "Ask loop to delete this channel");
tll_channel_callback(ptr, &msg);
return 0;
}
1 change: 1 addition & 0 deletions src/ld.script
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ TLL_0.3.0 {

TLL_0.4.0 {
global:
tll_channel_internal_delete_later;
tll_config_value_dup;
tll_logger_stat;
} TLL_0.3.0;
1 change: 1 addition & 0 deletions src/tll/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef enum {
TLL_MESSAGE_CHANNEL_ADD = 1, ///< Add new sub-channel
TLL_MESSAGE_CHANNEL_DELETE = 2, ///< Delete sub-channel
TLL_MESSAGE_CHANNEL_UPDATE_FD = 3, ///< Update fd, data is old fd
TLL_MESSAGE_CHANNEL_DEFERRED_DELETE = 4, ///< Delete this channel when process is finished
} tll_msg_channel_t;

typedef union {
Expand Down
6 changes: 6 additions & 0 deletions src/tll/channel/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,12 @@ class Base
return old;
}

/// Signal loop that this channel should be deleted when callback is finished
void delete_later()
{
tll_channel_internal_delete_later(&internal);
}

template <typename R, typename... Args>
[[nodiscard]]
R state_fail(R err, tll::logger::format_string<Args...> format, Args && ... args)
Expand Down
3 changes: 3 additions & 0 deletions src/tll/channel/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ static inline int tll_channel_callback(const tll_channel_internal_t * in, const
return 0;
}

/// Ask event loop to delete this channel when callback is finished
int tll_channel_internal_delete_later(tll_channel_internal_t *ptr);

#ifdef __cplusplus
} // extern "C"
#endif//__cplusplus
Expand Down
25 changes: 22 additions & 3 deletions src/tll/processor/loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,10 @@ struct tll_processor_loop_t
tll::Logger _log;

std::list<tll::Channel *> list; // All registered channels
tll::processor::List<tll::Channel> list_process; // List of channels to process
tll::processor::List<tll::Channel> list_pending; // List of channels with pending data
tll::processor::List<tll::Channel> list_nofd; // List of channels to process that don't have file descriptors
tll::processor::List<tll::Channel> list_process; //< List of channels to process
tll::processor::List<tll::Channel> list_pending; //< List of channels with pending data
tll::processor::List<tll::Channel> list_nofd; //< List of channels to process that don't have file descriptors
tll::processor::List<tll::Channel> list_delete; //< Deferred delete requests

int stop = 0; ///< Stop flag that can be toggled to stop loop iteration
bool time_cache_enable = false; ///< Enable feeding time cache
Expand Down Expand Up @@ -504,6 +505,17 @@ struct tll_processor_loop_t
return r == 0 ? EAGAIN : 0;
}

int process_delete()
{
for (unsigned i = 0; i < list_delete.size; i++) {
auto c = list_delete[i];
if (c == nullptr) continue;
_log.info("Delete channel {}", c->name());
delete c;
}
return 0;
}

int add(tll::Channel *c)
{
_log.debug("Add channel {} with fd {}", c->name(), c->fd());
Expand Down Expand Up @@ -594,6 +606,7 @@ struct tll_processor_loop_t
}

const_cast<tll::Channel *>(c)->callback_del(this, TLL_MESSAGE_MASK_CHANNEL | TLL_MESSAGE_MASK_STATE);
list_delete.del(c);
return 0;
}

Expand Down Expand Up @@ -664,6 +677,12 @@ struct tll_processor_loop_t
return update(c, c->dcaps(), *(unsigned *) msg->data);
else if (msg->msgid == TLL_MESSAGE_CHANNEL_UPDATE_FD)
return update_fd(const_cast<tll::Channel *>(c), *(int *) msg->data);
else if (msg->msgid == TLL_MESSAGE_CHANNEL_DEFERRED_DELETE) {
list_delete.add(const_cast<tll::Channel *>(c));
if (_poll_enable)
_poll.pending_enable();
return 0;
}
return 0;
}
};
Expand Down

0 comments on commit 05a0927

Please sign in to comment.