2626#include <qb/qbatomic.h>
2727#include <qb/qbipcs.h>
2828
29- static void qb_ipcs_destroy_internal (void * data );
3029static 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
0 commit comments