+}
+
+/* Internal function: do any Glk-thread cleanup for shutting down the Glk library. */
+void
+shutdown_glk_post(void)
+{
+ ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
+
+ /* Free all opaque objects; can't iterate normally, because the objects are
+ being removed from the global iteration lists */
+ if(glk_data->root_window)
+ glk_window_close(glk_data->root_window->data, NULL);
+ g_assert(glk_data->root_window == NULL);
+ strid_t str;
+ while( (str = glk_stream_iterate(NULL, NULL)) )
+ glk_stream_close(str, NULL);
+ frefid_t fref;
+ while( (fref = glk_fileref_iterate(NULL, NULL)) )
+ glk_fileref_destroy(fref);
+ schanid_t sch;
+ while( (sch = glk_schannel_iterate(NULL, NULL)) )
+ glk_schannel_destroy(sch);
+
+ /* Empty the event queue */
+ g_mutex_lock(glk_data->event_lock);
+ g_queue_foreach(glk_data->event_queue, (GFunc)g_free, NULL);
+
+ /* COMPAT: g_queue_clear could be used here, but only appeared in 2.14 */
+ // g_queue_clear(glk_data->event_queue);
+ g_list_free(glk_data->event_queue->head);
+ glk_data->event_queue->head = glk_data->event_queue->tail = NULL;
+ glk_data->event_queue->length = 0;
+
+ g_mutex_unlock(glk_data->event_lock);
+
+ /* Reset the abort signaling mechanism */
+ g_mutex_lock(glk_data->abort_lock);
+ glk_data->abort_signalled = FALSE;
+ g_mutex_unlock(glk_data->abort_lock);
+
+ /* Reset arrangement mechanism */
+ g_mutex_lock(glk_data->arrange_lock);
+ glk_data->needs_rearrange = FALSE;
+ glk_data->ignore_next_arrange_event = FALSE;
+ g_mutex_unlock(glk_data->arrange_lock);
+
+ /* Unref input queues (they are not destroyed because the main thread stil holds a ref */
+ g_async_queue_unref(glk_data->char_input_queue);
+ g_async_queue_unref(glk_data->line_input_queue);