From: Jean-Philippe Orsini Date: Thu, 2 May 2013 20:18:44 +0000 (+0000) Subject: (no commit message) X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=commitdiff_plain;h=9f2b2bde43dc8f831417213463709640935eeeb9;ds=sidebyside --- diff --git a/src/glade/prss.glade b/src/glade/prss.glade index 1c7a070..aff2879 100644 --- a/src/glade/prss.glade +++ b/src/glade/prss.glade @@ -416,6 +416,7 @@ feed_store False False + True True True @@ -430,6 +431,9 @@ 1 Feed True + True + True + 0 diff --git a/src/ttrss_ws.c b/src/ttrss_ws.c index ac6ee06..2c0da73 100644 --- a/src/ttrss_ws.c +++ b/src/ttrss_ws.c @@ -135,7 +135,7 @@ execute(struct http_session *sess, struct json_object *rq, char **err) if (str) { log_debug("execute() err=%s", str); content = NULL; - + if (err) *err = strdup(str); } else { diff --git a/src/ttrss_wsasync.c b/src/ttrss_wsasync.c index cb30f84..f3c3d84 100644 --- a/src/ttrss_wsasync.c +++ b/src/ttrss_wsasync.c @@ -29,15 +29,31 @@ #include "ttrss_ws.h" #include "ttrss_wsasync.h" +struct async_request { + void *data; + struct json_object *request; + void (*callback)(struct json_object *, void *); +}; + static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; -static struct json_object **requests; +static struct async_request **requests; static pthread_t *thread; -void *loop() +static void async_request_free(struct async_request *rq) { - struct json_object **tmp, **cur, *rp, *rq; + if (rq) { + if (rq->request) + json_object_put(rq->request); + free(rq); + } +} + +static void *loop() +{ + struct json_object *rp; + struct async_request **tmp, **cur, *rq; while (1) { log_debug("loop()"); @@ -55,10 +71,15 @@ void *loop() cur = tmp; while (*cur) { rq = *cur; - rp = ws_execute(rq); - json_object_put(rq); + rp = ws_execute(rq->request); + + if (rq->callback) + rq->callback(rp, rq->data); + + async_request_free(rq); if (rp) json_object_put(rp); + cur++; usleep(100000); } @@ -72,24 +93,44 @@ void *loop() pthread_exit(NULL); } -void ws_async_set_article_unread(int id, int unread) +static struct async_request * +add_async_request(struct json_object *rq, + void (*callback)(struct json_object *, void *data), + void *data) { - struct json_object *rq, **tmp; - - log_debug("ws_async_set_article_unread(%d,%d)", id, unread); - - rq = ws_request_new_set_article_unread(id, unread); + struct async_request *async_rq; + struct async_request **tmp; pthread_mutex_lock(&lock); if (!thread) { thread = malloc(sizeof(pthread_t)); pthread_create(thread, NULL, loop, NULL); } + pthread_mutex_unlock(&lock); + + async_rq = malloc(sizeof(struct async_request)); + async_rq->request = rq; + async_rq->data = data; + async_rq->callback = callback; - tmp = (struct json_object **)list_add((void **)requests, rq); + pthread_mutex_lock(&lock); + tmp = (struct async_request **)list_add((void **)requests, async_rq); list_free((void **)requests); requests = tmp; pthread_mutex_unlock(&lock); + return async_rq; +} + +void ws_async_set_article_unread(int id, int unread) +{ + struct json_object *rq; + + log_debug("ws_async_set_article_unread(%d,%d)", id, unread); + + rq = ws_request_new_set_article_unread(id, unread); + + add_async_request(rq, NULL, NULL); + log_debug("ws_async_set_article_unread(%d,%d) done", id, unread); }