/
bus.c
152 lines (120 loc) · 3.77 KB
/
bus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @file bus.c
* @version 7.0.0
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
int bus_module_init()
{
return (0);
}
int bus_init(struct bus_t *self_p)
{
ASSERTN(self_p != NULL, EINVAL);
binary_tree_init(&self_p->listeners);
rwlock_init(&self_p->rwlock);
return (0);
}
int bus_listener_init(struct bus_listener_t *self_p,
int id,
chan_t *chan_p)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(chan_p != NULL, EINVAL);
self_p->base.key = id;
self_p->id = id;
self_p->chan_p = chan_p;
self_p->next_p = NULL;
return (0);
}
int bus_attach(struct bus_t *self_p,
struct bus_listener_t *listener_p)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(listener_p != NULL, EINVAL);
struct bus_listener_t *head_p;
rwlock_writer_take(&self_p->rwlock);
/* Try to insert the node into the tree. It fails if there already
* is a node with the same key (id).*/
if (binary_tree_insert(&self_p->listeners, &listener_p->base) != 0) {
head_p = (struct bus_listener_t *)binary_tree_search(
&self_p->listeners, listener_p->id);
listener_p->next_p = head_p->next_p;
head_p->next_p = listener_p;
}
rwlock_writer_give(&self_p->rwlock);
return (0);
}
int bus_detatch(struct bus_t *self_p,
struct bus_listener_t *listener_p)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(listener_p != NULL, EINVAL);
int res = 0;
struct bus_listener_t *head_p, *curr_p, *prev_p;
rwlock_writer_take(&self_p->rwlock);
head_p = (struct bus_listener_t *)binary_tree_search(
&self_p->listeners, listener_p->id);
if (head_p == NULL) {
res = -1;
} else if (head_p == listener_p) {
res = binary_tree_delete(&self_p->listeners, listener_p->id);
if (listener_p->next_p != NULL) {
binary_tree_insert(&self_p->listeners,
&listener_p->next_p->base);
}
} else {
curr_p = head_p->next_p;
prev_p = head_p;
res = -1;
while (curr_p != NULL) {
if (curr_p == listener_p) {
prev_p->next_p = listener_p->next_p;
res = 0;
break;
}
prev_p = curr_p;
curr_p = curr_p->next_p;
}
}
rwlock_writer_give(&self_p->rwlock);
return (res);
}
int bus_write(struct bus_t *self_p,
int id,
const void *buf_p,
size_t size)
{
ASSERTN(self_p != NULL, EINVAL);
ASSERTN(buf_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
int number_of_receivers;
struct bus_listener_t *curr_p;
rwlock_reader_take(&self_p->rwlock);
curr_p = (struct bus_listener_t *)binary_tree_search(
&self_p->listeners, id);
number_of_receivers = 0;
while (curr_p != NULL) {
((struct chan_t *)curr_p->chan_p)->write(curr_p->chan_p,
buf_p,
size);
number_of_receivers++;
curr_p = curr_p->next_p;
}
rwlock_reader_give(&self_p->rwlock);
return (number_of_receivers);
}