asynchronous ws call for flagging article as read
[prss.git] / src / ttrss_wsasync.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19
20 #include <unistd.h>
21
22 #include <pthread.h>
23
24 #include <json/json.h>
25
26 #include "list.h"
27 #include "log.h"
28 #include "ttrss_ws.h"
29 #include "ttrss_wsasync.h"
30
31 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
32
33 static struct json_object **requests;
34
35 static pthread_t *thread;
36
37 void *loop()
38 {
39         struct json_object **tmp, **cur, *rp, *rq;
40
41         while (1) {
42                 log_debug("loop()");
43
44                 pthread_mutex_lock(&lock);
45                 if (requests) {
46                         tmp = requests;
47                         requests = NULL;
48                 } else {
49                         tmp = NULL;
50                 }
51                 pthread_mutex_unlock(&lock);
52
53                 if (tmp) {
54                         cur = tmp;
55                         while (*cur) {
56                                 rq = *cur;
57                                 rp = ws_execute(rq);
58                                 json_object_put(rq);
59                                 if (rp)
60                                         json_object_put(rp);
61                                 cur++;
62                         }
63                         free(tmp);
64                 }
65
66                 log_debug("loop() done");
67                 sleep(1);
68         }
69
70         pthread_exit(NULL);
71 }
72
73 void ws_async_set_article_unread(int id, int unread)
74 {
75         struct json_object *rq, **tmp;
76
77         log_debug("ws_async_set_article_unread(%d,%d)", id, unread);
78
79         rq = ws_request_new_set_article_unread(id, unread);
80
81         pthread_mutex_lock(&lock);
82         if (!thread) {
83                 thread = malloc(sizeof(pthread_t));
84                 pthread_create(thread, NULL, loop, NULL);
85         }
86
87         tmp = (struct json_object **)list_add((void **)requests, rq);
88         list_free((void **)requests);
89         requests = tmp;
90         pthread_mutex_unlock(&lock);
91
92         log_debug("ws_async_set_article_unread(%d,%d) done", id, unread);
93 }