blob: 3f48dd0a40abae125b5bd436e1630ba8cad4a3ec [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/*
2 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010 by On-Waves
4 * All Rights Reserved
5 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +01006 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080015 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080018 *
19 */
20#include <thread.h>
21
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080022#include <osmocore/talloc.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080023
24#include <sys/types.h>
25#include <sys/socket.h>
26
27#include <string.h>
28#include <unistd.h>
29
30static void *tall_ctx_thr;
31
32static int thread_finish(struct thread_notifier *not)
33{
34 pthread_mutex_destroy(&not->guard);
35 close(not->fd[0]);
36 close(not->fd[1]);
37
38 return 0;
39}
40
41void thread_init(void)
42{
43 tall_ctx_thr = talloc_named_const(NULL, 1, "threads");
44}
45
46struct thread_notifier *thread_notifier_alloc()
47{
48 struct thread_notifier *not = talloc_zero(tall_ctx_thr, struct thread_notifier);
49 if (!not)
50 return NULL;
51
52 if (socketpair(AF_UNIX, SOCK_STREAM, 0, not->fd) == -1) {
53 talloc_free(not);
54 return NULL;
55 }
56
57 if (pthread_mutex_init(&not->guard, NULL) != 0) {
58 close(not->fd[0]);
59 close(not->fd[1]);
60 talloc_free(not);
61 return NULL;
62 }
63
64 not->bfd.fd = not->fd[1];
65 INIT_LLIST_HEAD(&not->__head1);
66 INIT_LLIST_HEAD(&not->__head2);
67 not->main_head = &not->__head1;
68 not->thread_head = &not->__head2;
69 talloc_set_destructor(not, thread_finish);
70 return not;
71}
72
73void thread_safe_add(struct thread_notifier *not, struct llist_head *_new)
74{
75 char c;
76 pthread_mutex_lock(&not->guard);
77
78 llist_add_tail(_new, not->thread_head);
79 if (!not->no_write && write(not->fd[0], &c, sizeof(c)) != 1) {
80 fprintf(stderr, "BAD NEWS. Socket write failed.\n");
81 }
82
83 pthread_mutex_unlock(&not->guard);
84}
85
86void thread_swap(struct thread_notifier *not)
87{
88 pthread_mutex_lock(&not->guard);
89
90 if (not->main_head == &not->__head1) {
91 not->main_head = &not->__head2;
92 not->thread_head = &not->__head1;
93 } else {
94 not->main_head = &not->__head1;
95 not->thread_head = &not->__head2;
96 }
97
98 pthread_mutex_unlock(&not->guard);
99}