2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <json/json.h>
30 #include "ttrss_wsasync.h"
32 struct async_request {
34 struct json_object *request;
35 void (*callback)(struct json_object *, void *);
38 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
40 static struct async_request **requests;
42 static pthread_t *thread;
44 static void async_request_free(struct async_request *rq)
48 json_object_put(rq->request);
55 struct json_object *rp;
56 struct async_request **tmp, **cur, *rq;
61 pthread_mutex_lock(&lock);
68 pthread_mutex_unlock(&lock);
74 rp = ws_execute(rq->request);
77 rq->callback(rp, rq->data);
79 async_request_free(rq);
89 log_debug("loop() done");
96 static struct async_request *
97 add_async_request(struct json_object *rq,
98 void (*callback)(struct json_object *, void *data),
101 struct async_request *async_rq;
102 struct async_request **tmp;
104 pthread_mutex_lock(&lock);
106 thread = malloc(sizeof(pthread_t));
107 pthread_create(thread, NULL, loop, NULL);
109 pthread_mutex_unlock(&lock);
111 async_rq = malloc(sizeof(struct async_request));
112 async_rq->request = rq;
113 async_rq->data = data;
114 async_rq->callback = callback;
116 pthread_mutex_lock(&lock);
117 tmp = (struct async_request **)list_add((void **)requests, async_rq);
118 list_free((void **)requests);
120 pthread_mutex_unlock(&lock);
125 void ws_async_set_article_unread(int id, int unread)
127 struct json_object *rq;
129 log_debug("ws_async_set_article_unread(%d,%d)", id, unread);
131 rq = ws_request_new_set_article_unread(id, unread);
133 add_async_request(rq, NULL, NULL);
135 log_debug("ws_async_set_article_unread(%d,%d) done", id, unread);