Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* |
| 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 Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 6 | * 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 Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 9 | * (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 Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 15 | * |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 16 | * 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 Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | #include <thread.h> |
| 21 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 22 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | static void *tall_ctx_thr; |
| 31 | |
| 32 | static int thread_finish(struct thread_notifier *not) |
| 33 | { |
| 34 | pthread_mutex_destroy(¬->guard); |
| 35 | close(not->fd[0]); |
| 36 | close(not->fd[1]); |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | void thread_init(void) |
| 42 | { |
| 43 | tall_ctx_thr = talloc_named_const(NULL, 1, "threads"); |
| 44 | } |
| 45 | |
| 46 | struct 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(¬->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(¬->__head1); |
| 66 | INIT_LLIST_HEAD(¬->__head2); |
| 67 | not->main_head = ¬->__head1; |
| 68 | not->thread_head = ¬->__head2; |
| 69 | talloc_set_destructor(not, thread_finish); |
| 70 | return not; |
| 71 | } |
| 72 | |
| 73 | void thread_safe_add(struct thread_notifier *not, struct llist_head *_new) |
| 74 | { |
| 75 | char c; |
| 76 | pthread_mutex_lock(¬->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(¬->guard); |
| 84 | } |
| 85 | |
| 86 | void thread_swap(struct thread_notifier *not) |
| 87 | { |
| 88 | pthread_mutex_lock(¬->guard); |
| 89 | |
| 90 | if (not->main_head == ¬->__head1) { |
| 91 | not->main_head = ¬->__head2; |
| 92 | not->thread_head = ¬->__head1; |
| 93 | } else { |
| 94 | not->main_head = ¬->__head1; |
| 95 | not->thread_head = ¬->__head2; |
| 96 | } |
| 97 | |
| 98 | pthread_mutex_unlock(¬->guard); |
| 99 | } |