+/*
+ * Purge all sessions which have the 'kill_me' flag set.
+ * This function has code to prevent it from running more than once every
+ * few seconds, because running it after every single unbind would waste a lot
+ * of CPU time and keep the context list locked too much.
+ */
+void dead_session_purge(void) {
+ static time_t last_purge = 0;
+ struct CitContext *ptr, *rem;
+
+ if ( (time(NULL) - last_purge) < 5 ) return; /* Too soon, go away */
+
+ do {
+ rem = NULL;
+ begin_critical_section(S_SESSION_TABLE);
+ for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
+ if ( (ptr->state == CON_IDLE) && (ptr->kill_me) ) {
+ rem = ptr;
+ }
+ }
+ end_critical_section(S_SESSION_TABLE);
+
+ /* RemoveContext() enters its own S_SESSION_TABLE critical
+ * section, so we have to do it like this.
+ */
+ if (rem != NULL) RemoveContext(rem);
+
+ } while (rem != NULL);
+}
+