UpdateQueue: use std::string and std::queue

This commit is contained in:
Max Kellermann 2013-10-17 21:13:40 +02:00
parent 196ec25682
commit 7ef40de98b
3 changed files with 35 additions and 40 deletions

View File

@ -139,8 +139,6 @@ update_enqueue(const char *path, bool _discard)
*/ */
static void update_finished_event(void) static void update_finished_event(void)
{ {
char *path;
assert(progress == UPDATE_PROGRESS_DONE); assert(progress == UPDATE_PROGRESS_DONE);
update_thread.Join(); update_thread.Join();
@ -151,11 +149,11 @@ static void update_finished_event(void)
/* send "idle" events */ /* send "idle" events */
instance->DatabaseModified(); instance->DatabaseModified();
path = update_queue_shift(&discard); auto i = update_queue_shift();
if (path != NULL) { if (i.IsDefined()) {
/* schedule the next path */ /* schedule the next path */
spawn_update_task(path); discard = i.discard;
g_free(path); spawn_update_task(i.path_utf8.c_str());
} else { } else {
progress = UPDATE_PROGRESS_IDLE; progress = UPDATE_PROGRESS_IDLE;

View File

@ -19,49 +19,31 @@
#include "config.h" #include "config.h"
#include "UpdateQueue.hxx" #include "UpdateQueue.hxx"
#include "util/Macros.hxx"
#include <glib.h> #include <queue>
#include <list>
#include <assert.h> static constexpr unsigned MAX_UPDATE_QUEUE_SIZE = 32;
#include <string.h>
/* make this dynamic?, or maybe this is big enough... */ static std::queue<UpdateQueueItem, std::list<UpdateQueueItem>> update_queue;
static struct {
char *path;
bool discard;
} update_queue[32];
static size_t update_queue_length;
unsigned unsigned
update_queue_push(const char *path, bool discard, unsigned base) update_queue_push(const char *path, bool discard, unsigned base)
{ {
assert(update_queue_length <= ARRAY_SIZE(update_queue)); if (update_queue.size() >= MAX_UPDATE_QUEUE_SIZE)
if (update_queue_length == ARRAY_SIZE(update_queue))
return 0; return 0;
update_queue[update_queue_length].path = g_strdup(path); update_queue.emplace(path, discard);
update_queue[update_queue_length].discard = discard; return base + update_queue.size();
++update_queue_length;
return base + update_queue_length;
} }
char * UpdateQueueItem
update_queue_shift(bool *discard_r) update_queue_shift()
{ {
char *path; if (update_queue.empty())
return UpdateQueueItem();
if (update_queue_length == 0) auto i = std::move(update_queue.front());
return NULL; update_queue.pop();
return i;
path = update_queue[0].path;
*discard_r = update_queue[0].discard;
memmove(&update_queue[0], &update_queue[1],
--update_queue_length * sizeof(update_queue[0]));
return path;
} }

View File

@ -22,10 +22,25 @@
#include "check.h" #include "check.h"
#include <string>
struct UpdateQueueItem {
std::string path_utf8;
bool discard;
UpdateQueueItem() = default;
UpdateQueueItem(const char *_path, bool _discard)
:path_utf8(_path), discard(_discard) {}
bool IsDefined() const {
return !path_utf8.empty();
}
};
unsigned unsigned
update_queue_push(const char *path, bool discard, unsigned base); update_queue_push(const char *path, bool discard, unsigned base);
char * UpdateQueueItem
update_queue_shift(bool *discard_r); update_queue_shift();
#endif #endif