(no commit message)
[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 <stdlib.h>
21 #include <unistd.h>
22
23 #include <pthread.h>
24
25 #include <json/json.h>
26
27 #include "list.h"
28 #include "log.h"
29 #include "ttrss_ws.h"
30 #include "ttrss_wsasync.h"
31
32 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
33
34 static struct json_object **requests;
35
36 static pthread_t *thread;
37
38 void *loop()
39 {
40         struct json_object **tmp, **cur, *rp, *rq;
41
42         while (1) {
43                 log_debug("loop()");
44
45                 pthread_mutex_lock(&lock);
46                 if (requests) {
47                         tmp = requests;
48                         requests = NULL;
49                 } else {
50                         tmp = NULL;
51                 }
52                 pthread_mutex_unlock(&lock);
53
54                 if (tmp) {
55                         cur = tmp;
56                         while (*cur) {
57                                 rq = *cur;
58                                 rp = ws_execute(rq);
59                                 json_object_put(rq);
60                                 if (rp)
61                                         json_object_put(rp);
62                                 cur++;
63                         }
64                         free(tmp);
65                 }
66
67                 log_debug("loop() done");
68                 sleep(1);
69         }
70
71         pthread_exit(NULL);
72 }
73
74 void ws_async_set_article_unread(int id, int unread)
75 {
76         struct json_object *rq, **tmp;
77
78         log_debug("ws_async_set_article_unread(%d,%d)", id, unread);
79
80         rq = ws_request_new_set_article_unread(id, unread);
81
82         pthread_mutex_lock(&lock);
83         if (!thread) {
84                 thread = malloc(sizeof(pthread_t));
85                 pthread_create(thread, NULL, loop, NULL);
86         }
87
88         tmp = (struct json_object **)list_add((void **)requests, rq);
89         list_free((void **)requests);
90         requests = tmp;
91         pthread_mutex_unlock(&lock);
92
93         log_debug("ws_async_set_article_unread(%d,%d) done", id, unread);
94 }