Skip to content

Commit b07b65d

Browse files
committed
IPC: change service instance from handle to pointer.
Mainly to be consistent with the other objects. Also: - splint warnings(-weak) are now zero. - Added a reference counter to replace the handle. Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
1 parent 6714a17 commit b07b65d

6 files changed

Lines changed: 72 additions & 71 deletions

File tree

configure.ac

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ case "$host_os" in
241241
*linux*)
242242
AC_DEFINE_UNQUOTED([QB_LINUX], [1],
243243
[Compiling for Linux platform])
244-
LINT_FLAGS+=" -expect 21"
245244
;;
246245
darwin*)
247246
AC_DEFINE_UNQUOTED([QB_DARWIN], [1],
@@ -269,7 +268,6 @@ case "$host_os" in
269268
[Compiling for FreeBSD >= 8 platform])
270269
;;
271270
esac
272-
LINT_FLAGS="$LINT_FLAGS -expect 22"
273271
;;
274272
*solaris*)
275273
AC_DEFINE_UNQUOTED([QB_SOLARIS], [1],

include/qb/qbipcs.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ enum qb_ipcs_rate_limit {
4444
struct qb_ipcs_connection;
4545
typedef struct qb_ipcs_connection qb_ipcs_connection_t;
4646

47-
typedef qb_handle_t qb_ipcs_service_pt;
47+
struct qb_ipcs_service;
48+
typedef struct qb_ipcs_service qb_ipcs_service_t;
4849

4950
struct qb_ipcs_stats {
5051
uint32_t active_connections;
@@ -125,37 +126,58 @@ struct qb_ipcs_service_handlers {
125126

126127
/**
127128
* Create a new IPC server.
129+
*
130+
* @param name for clients to connect to.
131+
* @param service_id an integer to associate with the service
132+
* @param type transport type.
133+
* @param handlers callbacks.
134+
* @return the new service instance.
128135
*/
129-
qb_ipcs_service_pt qb_ipcs_create(const char *name,
136+
qb_ipcs_service_t* qb_ipcs_create(const char *name,
130137
int32_t service_id,
131138
enum qb_ipc_type type,
132139
struct qb_ipcs_service_handlers *handlers);
133140

141+
142+
/**
143+
* Increase the reference counter on the service object.
144+
*
145+
* @param s service instance
146+
*/
147+
void qb_ipcs_ref(qb_ipcs_service_t *s);
148+
149+
/**
150+
* Decrease the reference counter on the service object.
151+
*
152+
* @param s service instance
153+
*/
154+
void qb_ipcs_unref(qb_ipcs_service_t *s);
155+
134156
/**
135157
* Set your poll callbacks.
136158
* @param s service instance
137159
*/
138-
void qb_ipcs_poll_handlers_set(qb_ipcs_service_pt s,
160+
void qb_ipcs_poll_handlers_set(qb_ipcs_service_t* s,
139161
struct qb_ipcs_poll_handlers *handlers);
140162

141163
/**
142164
* run the new IPC server.
143165
* @param s service instance
144166
* @return 0 == ok; -errno to indicate a failure
145167
*/
146-
int32_t qb_ipcs_run(qb_ipcs_service_pt s);
168+
int32_t qb_ipcs_run(qb_ipcs_service_t* s);
147169

148170
/**
149171
* Destroy the IPC server.
150172
* @param s service instance
151173
*/
152-
void qb_ipcs_destroy(qb_ipcs_service_pt s);
174+
void qb_ipcs_destroy(qb_ipcs_service_t* s);
153175

154176
/**
155177
*
156178
* @param s service instance
157179
*/
158-
void qb_ipcs_request_rate_limit(qb_ipcs_service_pt pt, enum qb_ipcs_rate_limit rl);
180+
void qb_ipcs_request_rate_limit(qb_ipcs_service_t* pt, enum qb_ipcs_rate_limit rl);
159181

160182
/**
161183
* send a response to a incomming request.
@@ -232,7 +254,7 @@ int32_t qb_ipcs_connection_stats_get(qb_ipcs_connection_t *c,
232254
* @param pt service instance
233255
* @return 0 == ok; -errno to indicate a failure
234256
*/
235-
int32_t qb_ipcs_stats_get(qb_ipcs_service_pt pt,
257+
int32_t qb_ipcs_stats_get(qb_ipcs_service_t* pt,
236258
struct qb_ipcs_stats* stats,
237259
int32_t clear_after_read);
238260

@@ -244,7 +266,7 @@ int32_t qb_ipcs_stats_get(qb_ipcs_service_pt pt,
244266
* @param pt service instance
245267
* @return first connection
246268
*/
247-
qb_ipcs_connection_t * qb_ipcs_connection_first_get(qb_ipcs_service_pt pt);
269+
qb_ipcs_connection_t * qb_ipcs_connection_first_get(qb_ipcs_service_t* pt);
248270

249271
/**
250272
* Get the next connection.
@@ -255,7 +277,7 @@ qb_ipcs_connection_t * qb_ipcs_connection_first_get(qb_ipcs_service_pt pt);
255277
* @param current current connection
256278
* @return next connection
257279
*/
258-
qb_ipcs_connection_t * qb_ipcs_connection_next_get(qb_ipcs_service_pt pt,
280+
qb_ipcs_connection_t * qb_ipcs_connection_next_get(qb_ipcs_service_t* pt,
259281
qb_ipcs_connection_t *current);
260282

261283
/* *INDENT-OFF* */

lib/ipc_int.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ struct qb_ipcs_service {
142142
enum qb_ipc_type type;
143143
char name[NAME_MAX];
144144
int32_t service_id;
145+
int32_t ref_count;
145146
pid_t pid;
146147
int32_t needs_sock_for_poll;
147148
int32_t server_sock;
@@ -152,6 +153,7 @@ struct qb_ipcs_service {
152153
enum qb_loop_priority poll_priority;
153154

154155
struct qb_list_head connections;
156+
struct qb_list_head list;
155157
struct qb_ipcs_stats stats;
156158
};
157159

lib/ipcs.c

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,25 @@
2626
#include <qb/qbatomic.h>
2727
#include <qb/qbipcs.h>
2828

29-
static void qb_ipcs_destroy_internal(void *data);
3029
static void qb_ipcs_flowcontrol_set(struct qb_ipcs_connection *c,
3130
int32_t fc_enable);
3231

33-
QB_HDB_DECLARE(qb_ipc_services, qb_ipcs_destroy_internal);
32+
static QB_LIST_DECLARE(qb_ipc_services);
3433

35-
qb_ipcs_service_pt qb_ipcs_create(const char *name,
34+
qb_ipcs_service_t* qb_ipcs_create(const char *name,
3635
int32_t service_id,
3736
enum qb_ipc_type type,
3837
struct qb_ipcs_service_handlers *handlers)
3938
{
4039
struct qb_ipcs_service *s;
41-
qb_ipcs_service_pt h;
4240

43-
qb_hdb_handle_create(&qb_ipc_services,
44-
sizeof(struct qb_ipcs_service), &h);
45-
qb_hdb_handle_get(&qb_ipc_services, h, (void **)&s);
41+
s = calloc(1, sizeof(struct qb_ipcs_service));
4642

4743
s->pid = getpid();
4844
s->type = type;
4945
s->needs_sock_for_poll = QB_FALSE;
5046
s->poll_priority = QB_LOOP_MED;
47+
s->ref_count = 1;
5148

5249
s->service_id = service_id;
5350
strncpy(s->name, name, NAME_MAX);
@@ -58,33 +55,24 @@ qb_ipcs_service_pt qb_ipcs_create(const char *name,
5855
s->serv_fns.connection_destroyed = handlers->connection_destroyed;
5956

6057
qb_list_init(&s->connections);
58+
qb_list_init(&s->list);
59+
qb_list_add(&s->list, &qb_ipc_services);
6160

62-
qb_hdb_handle_put(&qb_ipc_services, h);
63-
64-
return h;
61+
return s;
6562
}
6663

67-
void qb_ipcs_poll_handlers_set(qb_ipcs_service_pt pt,
64+
void qb_ipcs_poll_handlers_set(struct qb_ipcs_service* s,
6865
struct qb_ipcs_poll_handlers *handlers)
6966
{
70-
struct qb_ipcs_service *s;
71-
72-
qb_hdb_handle_get(&qb_ipc_services, pt, (void **)&s);
73-
7467
s->poll_fns.job_add = handlers->job_add;
7568
s->poll_fns.dispatch_add = handlers->dispatch_add;
7669
s->poll_fns.dispatch_mod = handlers->dispatch_mod;
7770
s->poll_fns.dispatch_del = handlers->dispatch_del;
78-
79-
qb_hdb_handle_put(&qb_ipc_services, pt);
8071
}
8172

82-
int32_t qb_ipcs_run(qb_ipcs_service_pt pt)
73+
int32_t qb_ipcs_run(struct qb_ipcs_service* s)
8374
{
8475
int32_t res;
85-
struct qb_ipcs_service *s;
86-
87-
qb_hdb_handle_get(&qb_ipc_services, pt, (void **)&s);
8876

8977
switch (s->type) {
9078
case QB_IPC_SOCKET:
@@ -105,21 +93,19 @@ int32_t qb_ipcs_run(qb_ipcs_service_pt pt)
10593
}
10694
res = qb_ipcs_us_publish(s);
10795
if (res < 0) {
108-
qb_hdb_handle_put(&qb_ipc_services, pt);
96+
qb_ipcs_unref(s);
10997
return res;
11098
}
11199

112100
if (res < 0) {
113-
qb_ipcs_us_withdraw(s);
101+
(void)qb_ipcs_us_withdraw(s);
114102
}
115103

116-
qb_hdb_handle_put(&qb_ipc_services, pt);
117104
return res;
118105
}
119106

120-
void qb_ipcs_request_rate_limit(qb_ipcs_service_pt pt, enum qb_ipcs_rate_limit rl)
107+
void qb_ipcs_request_rate_limit(struct qb_ipcs_service* s, enum qb_ipcs_rate_limit rl)
121108
{
122-
struct qb_ipcs_service *s;
123109
struct qb_ipcs_connection *c;
124110
enum qb_loop_priority p;
125111

@@ -137,8 +123,6 @@ void qb_ipcs_request_rate_limit(qb_ipcs_service_pt pt, enum qb_ipcs_rate_limit r
137123
break;
138124
}
139125

140-
qb_hdb_handle_get(&qb_ipc_services, pt, (void**)&s);
141-
142126
qb_list_for_each_entry(c, &s->connections, list) {
143127
qb_ipcs_connection_ref_inc(c);
144128

@@ -166,33 +150,40 @@ void qb_ipcs_request_rate_limit(qb_ipcs_service_pt pt, enum qb_ipcs_rate_limit r
166150
qb_ipcs_connection_ref_dec(c);
167151
}
168152
s->poll_priority = p;
169-
qb_hdb_handle_put(&qb_ipc_services, pt);
170153
}
171154

172-
void qb_ipcs_destroy(qb_ipcs_service_pt pt)
155+
void qb_ipcs_ref(struct qb_ipcs_service *s)
173156
{
174-
qb_hdb_handle_put(&qb_ipc_services, pt);
175-
qb_hdb_handle_destroy(&qb_ipc_services, pt);
157+
qb_atomic_int_inc(&s->ref_count);
176158
}
177159

178-
static void qb_ipcs_destroy_internal(void *data)
160+
void qb_ipcs_unref(struct qb_ipcs_service *s)
179161
{
180-
struct qb_ipcs_service *s = (struct qb_ipcs_service *)data;
162+
int32_t free_it;
181163
struct qb_ipcs_connection *c = NULL;
182164
struct qb_list_head *iter;
183165
struct qb_list_head *iter_next;
184166

185-
qb_util_log(LOG_DEBUG, "%s\n", __func__);
186-
187-
qb_list_for_each_safe(iter, iter_next, &s->connections) {
188-
c = qb_list_entry(iter, struct qb_ipcs_connection, list);
189-
if (c == NULL) {
190-
continue;
167+
assert(s->ref_count > 0);
168+
free_it = qb_atomic_int_dec_and_test(&s->ref_count);
169+
if (free_it) {
170+
qb_util_log(LOG_DEBUG, "%s() - destorying\n", __func__);
171+
qb_list_for_each_safe(iter, iter_next, &s->connections) {
172+
c = qb_list_entry(iter, struct qb_ipcs_connection, list);
173+
if (c == NULL) {
174+
continue;
175+
}
176+
qb_ipcs_disconnect(c);
191177
}
192-
qb_ipcs_disconnect(c);
178+
free(s);
193179
}
194180
}
195181

182+
void qb_ipcs_destroy(struct qb_ipcs_service* s)
183+
{
184+
qb_ipcs_unref(s);
185+
}
186+
196187
/*
197188
* connection API
198189
*/
@@ -299,44 +290,36 @@ ssize_t qb_ipcs_event_sendv(struct qb_ipcs_connection *c, const struct iovec * i
299290
return res;
300291
}
301292

302-
qb_ipcs_connection_t * qb_ipcs_connection_first_get(qb_ipcs_service_pt pt)
293+
qb_ipcs_connection_t * qb_ipcs_connection_first_get(struct qb_ipcs_service* s)
303294
{
304-
struct qb_ipcs_service *s;
305295
struct qb_ipcs_connection *c;
306296
struct qb_list_head *iter;
307297

308-
qb_hdb_handle_get(&qb_ipc_services, pt, (void **)&s);
309-
310298
if (qb_list_empty(&s->connections)) {
311-
qb_hdb_handle_put(&qb_ipc_services, pt);
312299
return NULL;
313300
}
314301
iter = s->connections.next;
315302

316303
c = qb_list_entry(iter, struct qb_ipcs_connection, list);
317304
qb_ipcs_connection_ref_inc(c);
318-
qb_hdb_handle_put(&qb_ipc_services, pt);
305+
319306
return c;
320307
}
321308

322-
qb_ipcs_connection_t * qb_ipcs_connection_next_get(qb_ipcs_service_pt pt,
309+
qb_ipcs_connection_t * qb_ipcs_connection_next_get(struct qb_ipcs_service* s,
323310
struct qb_ipcs_connection *current)
324311
{
325-
struct qb_ipcs_service *s;
326312
struct qb_ipcs_connection *c;
327313
struct qb_list_head *iter;
328314

329-
qb_hdb_handle_get(&qb_ipc_services, pt, (void **)&s);
330-
331315
if (current->list.next == &s->connections) {
332-
qb_hdb_handle_put(&qb_ipc_services, pt);
333316
return NULL;
334317
}
335318
iter = current->list.next;
336319

337320
c = qb_list_entry(iter, struct qb_ipcs_connection, list);
338321
qb_ipcs_connection_ref_inc(c);
339-
qb_hdb_handle_put(&qb_ipc_services, pt);
322+
340323
return c;
341324
}
342325

@@ -588,18 +571,14 @@ int32_t qb_ipcs_connection_stats_get(qb_ipcs_connection_t *c,
588571
return 0;
589572
}
590573

591-
int32_t qb_ipcs_stats_get(qb_ipcs_service_pt pt,
574+
int32_t qb_ipcs_stats_get(struct qb_ipcs_service* s,
592575
struct qb_ipcs_stats* stats,
593576
int32_t clear_after_read)
594577
{
595-
struct qb_ipcs_service *s;
596-
597-
qb_hdb_handle_get(&qb_ipc_services, pt, (void **)&s);
598578
memcpy(stats, &s->stats, sizeof(struct qb_ipcs_stats));
599579
if (clear_after_read) {
600580
memset(&s->stats, 0, sizeof(struct qb_ipcs_stats));
601581
}
602-
qb_hdb_handle_put(&qb_ipc_services, pt);
603582
return 0;
604583
}
605584

tests/bms.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static qb_loop_t *bms_loop;
3939
static GMainLoop *glib_loop;
4040
static qb_array_t *gio_map;
4141
#endif
42-
static qb_ipcs_service_pt s1;
42+
static qb_ipcs_service_t* s1;
4343

4444
static int32_t s1_connection_accept_fn(qb_ipcs_connection_t *c, uid_t uid, gid_t gid)
4545
{

tests/check_ipc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ enum my_msg_ids {
6868
* 8) multiple services
6969
*/
7070
static qb_loop_t *my_loop;
71-
static qb_ipcs_service_pt s1;
71+
static qb_ipcs_service_t* s1;
7272
static int32_t turn_on_fc = QB_FALSE;
7373
static int32_t fc_enabled = 89;
7474

0 commit comments

Comments
 (0)