+/*
+ * Copyright (C) 2010-2013 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <unistd.h>
+
+#include <pthread.h>
+
+#include <json/json.h>
+
+#include "list.h"
+#include "log.h"
+#include "ttrss_ws.h"
+#include "ttrss_wsasync.h"
+
+static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+
+static struct json_object **requests;
+
+static pthread_t *thread;
+
+void *loop()
+{
+ struct json_object **tmp, **cur, *rp, *rq;
+
+ while (1) {
+ log_debug("loop()");
+
+ pthread_mutex_lock(&lock);
+ if (requests) {
+ tmp = requests;
+ requests = NULL;
+ } else {
+ tmp = NULL;
+ }
+ pthread_mutex_unlock(&lock);
+
+ if (tmp) {
+ cur = tmp;
+ while (*cur) {
+ rq = *cur;
+ rp = ws_execute(rq);
+ json_object_put(rq);
+ if (rp)
+ json_object_put(rp);
+ cur++;
+ }
+ free(tmp);
+ }
+
+ log_debug("loop() done");
+ sleep(1);
+ }
+
+ pthread_exit(NULL);
+}
+
+void ws_async_set_article_unread(int id, int unread)
+{
+ 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);
+
+ pthread_mutex_lock(&lock);
+ if (!thread) {
+ thread = malloc(sizeof(pthread_t));
+ pthread_create(thread, NULL, loop, NULL);
+ }
+
+ tmp = (struct json_object **)list_add((void **)requests, rq);
+ list_free((void **)requests);
+ requests = tmp;
+ pthread_mutex_unlock(&lock);
+
+ log_debug("ws_async_set_article_unread(%d,%d) done", id, unread);
+}