diff --git a/inc/lektor/queue.h b/inc/lektor/queue.h index 65eb7b08888ba92206f016e6750ade894b99ad82..88a7fe7d9218e60f562af72a2aac07b73f20fff2 100644 --- a/inc/lektor/queue.h +++ b/inc/lektor/queue.h @@ -14,6 +14,7 @@ #include <stddef.h> #include <string.h> #include <stdio.h> +#include <limits.h> #include <lektor/common.h> enum { @@ -120,7 +121,7 @@ lkt_queue_send(struct queue *queue, enum lkt_event_type _type, void *_attr) goto end; queue->contents = new; - queue->size *= 2; + queue->size *= 2; } lkt_event evt = { @@ -131,6 +132,11 @@ lkt_queue_send(struct queue *queue, enum lkt_event_type _type, void *_attr) if (! (_type & queue->available)) LOG_WARN("QUEUE", "The event %d is not available, push it anyway", _type); + else if (_attr) { + LOG_DEBUG("QUEUE", "Pushed event %d with attr (int: %d, ptr %0*p)", + evt.type, evt.attr, sizeof(size_t) * CHAR_BIT / 4, evt.attr); + } else + LOG_DEBUG("QUEUE", "Pushed event %d with NULL attr", evt.type); end: pthread_mutex_unlock(&queue->lock); } @@ -142,17 +148,24 @@ lkt_queue_handle(struct queue *queue) lkt_event ret = {0}; if (!queue || !queue->last) goto end; - if (! (queue->contents[0].type & queue->available) && - queue->contents[0].type) { + if (! (queue->contents[0].type & queue->available) && queue->contents[0].type) { LOG_WARN("QUEUE", "Event %d is not available", queue->contents[0].type); goto end; } ret = queue->contents[0]; - memmove((void *) queue->contents, (void *) (queue->contents + 1), - (--(queue->last)) * sizeof(void *)); + for (size_t i = 1; i < queue->last; ++i) + queue->contents[i - 1] = queue->contents[i]; + queue->last -= 1; end: pthread_mutex_unlock(&queue->lock); + if (ret.attr) { + LOG_DEBUG("QUEUE", "Handle event %d with attr (int: %d, ptr: %0*p)", + ret.type, ret.attr, sizeof(size_t) * CHAR_BIT / 4, ret.attr); + } else if (ret.type) { + /* Don't do anyhting with the 'null' event */ + LOG_DEBUG("QUEUE", "Handle event %d with NULL attr", ret.type); + } return ret; } diff --git a/src/module/module_repo.c b/src/module/module_repo.c index 0fef215106c23e2a0829ed2cec83233cd3a79bc8..81bf16929c0ce1d48a5370914883166dd3052404 100644 --- a/src/module/module_repo.c +++ b/src/module/module_repo.c @@ -625,6 +625,7 @@ __worker_import_favorites_internal(const char UNUSED *k, const char *val, int UN /* TODO: Add a way to use the workers to do this for each fav list */ LOG_INFO("REPO", "Processing favorite list: %s", fav); __handle_fav_list(repo, fav, LKT_LINE_MAX); + lkt_queue_send(repo->queue, lkt_event_db_update_tick, NULL); } static void * @@ -642,8 +643,10 @@ __worker_import_favorites(void *__repo) LOG_ERROR("REPO", "Failed to get json, possibly no internet connexion or repo is down"); pthread_exit(NULL); } - LOG_INFO("REPO", "Finished to dl favorite lists"); size_t len = json_parse_get_count(json, 2); + LOG_INFO("REPO", "Finished to dl favorite lists, got %ld lists", len); + lkt_queue_send(repo->queue, lkt_event_db_updating, LKT_DB_UPDATING_PROGRESS); + lkt_queue_send(repo->queue, lkt_event_db_update_total, (void *) (size_t) len); json_parse(json, 2, __worker_import_favorites_internal, (void *) repo); free(json); LOG_INFO("REPO", "Finished to deal with %ld favorite lists", len); @@ -653,6 +656,7 @@ __worker_import_favorites(void *__repo) GOTO_IF(pthread_mutex_unlock(&(repo->mtx)), "Failed to unlock", end_no_lock); end_no_lock: + lkt_queue_send(repo->queue, lkt_event_db_updating, LKT_DB_UPDATING_FINISHED); pthread_exit(NULL); } diff --git a/src/net/listen.c b/src/net/listen.c index 11a8561a1e5e4f97d8a4c77ceaeaf94d3b1494db..6a6ab647f9c616e287f69e9dfe0ebedbcb04fbf0 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -822,15 +822,15 @@ lkt_client_auth(struct lkt_state *srv, size_t c, bool set) static inline void handle_queue_events(struct lkt_state *srv) { -#define __CASE(type, func) \ - case lkt_event_ ## type: \ - if (evt.attr != NULL) { \ - LOG_DEBUG("EVENT", "Got event " #type " with attr (int: %d, ptr %0*p)", \ - evt.attr, sizeof(size_t) * CHAR_BIT / 4, evt.attr); \ - } else { \ - LOG_DEBUG("EVENT", "Got event " #type " with NULL attr"); \ - } \ - func; \ +#define __CASE(type, func) \ + case lkt_event_ ## type: \ + if (evt.attr != NULL) { \ + LOG_DEBUG("EVENT", "Got event " #type " (%d) with attr (int: %d, ptr %0*p)", \ + lkt_event_##type, evt.attr, sizeof(size_t) * CHAR_BIT / 4, evt.attr); \ + } else { \ + LOG_DEBUG("EVENT", "Got event " #type " (%d) with NULL attr", lkt_event_##type);\ + } \ + func; \ break; #define __ATTR_IS_STATE(state, func) { \ if ((__LKT_PLAY_ ## state) == (size_t) evt.attr) { func; }}