Skip to content

Commit 82fa9a7

Browse files
committed
tweaks, and use fr_dlist_foreach() in more places
1 parent 91b003c commit 82fa9a7

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/lib/io/coord.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ RCSID("$Id$")
3131
#include <freeradius-devel/server/module_rlm.h>
3232
#include <freeradius-devel/tls/base.h>
3333
#include <freeradius-devel/unlang/base.h>
34-
#include <freeradius-devel/util/dlist.h>
3534
#include <freeradius-devel/util/syserror.h>
3635

3736
#include <stdalign.h>
@@ -143,11 +142,11 @@ fr_coord_reg_t *fr_coord_register(TALLOC_CTX *ctx, fr_coord_reg_ctx_t *reg_ctx)
143142

144143
/* Allocate the list of registered coordinators if not already done */
145144
if (!coord_regs) {
146-
coord_regs = talloc_zero(NULL, fr_dlist_head_t);
145+
MEM(coord_regs = talloc_zero(NULL, fr_dlist_head_t));
147146
fr_dlist_init(coord_regs, fr_coord_reg_t, entry);
148147
}
149148

150-
coord_reg = talloc(ctx, fr_coord_reg_t);
149+
MEM(coord_reg = talloc(ctx, fr_coord_reg_t));
151150
*coord_reg = (fr_coord_reg_t) {
152151
.name = reg_ctx->name,
153152
.coord_cb = reg_ctx->coord_cb,
@@ -171,7 +170,6 @@ fr_coord_reg_t *fr_coord_register(TALLOC_CTX *ctx, fr_coord_reg_ctx_t *reg_ctx)
171170
*/
172171
void fr_coord_deregister(fr_coord_reg_t *coord_reg)
173172
{
174-
fr_schedule_coord_t *sc = NULL;
175173
int ret;
176174

177175
fr_dlist_remove(coord_regs, coord_reg);
@@ -181,19 +179,20 @@ void fr_coord_deregister(fr_coord_reg_t *coord_reg)
181179
*/
182180
if (!coord_threads) goto free;
183181

184-
while ((sc = fr_dlist_next(coord_threads, sc))) {
182+
fr_dlist_foreach(coord_threads, fr_schedule_coord_t, sc) {
185183
if (sc->coord_reg == coord_reg) {
186184
if ((ret = pthread_join(sc->pthread_id, NULL)) != 0) {
187185
ERROR("Failed joining coordinator %s: %s", coord_reg->name, fr_syserror(ret));
188186
} else {
189187
DEBUG2("Coordinator %s joined (cleaned up)", coord_reg->name);
190188
}
189+
191190
fr_dlist_remove(coord_threads, sc);
191+
talloc_free(sc);
192192
break;
193193
}
194194
}
195195

196-
talloc_free(sc);
197196
free:
198197
talloc_free(coord_reg);
199198

@@ -233,7 +232,7 @@ static void coord_data_recv(void *ctx, void const *data, size_t data_size, fr_ti
233232

234233
/** Callback for a worker receiving data from a coordinator
235234
*/
236-
static void coord_worker_data_recv(void *ctx, void const *data, size_t data_size, UNUSED fr_time_t now)
235+
static void coord_worker_data_recv(void *ctx, void const *data, size_t data_size, fr_time_t now)
237236
{
238237
fr_coord_worker_t *cw = talloc_get_type_abort(ctx, fr_coord_worker_t);
239238
fr_coord_msg_t cm;
@@ -321,7 +320,7 @@ static fr_coord_t *fr_coord_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_coor
321320
fr_coord_cb_reg_t *cb = coord_reg->coord_cb;
322321
fr_atomic_queue_t *aq;
323322

324-
coord = talloc(ctx, fr_coord_t);
323+
MEM(coord = talloc(ctx, fr_coord_t));
325324
*coord = (fr_coord_t) {
326325
.el = el,
327326
.coord_reg = coord_reg,
@@ -463,7 +462,6 @@ static void *fr_coordinate_thread(void *arg)
463462
INFO("%s - Starting", coordinate_name);
464463

465464
sc->ctx = ctx = talloc_init("%s", coordinate_name);
466-
467465
if (!ctx) {
468466
ERROR("%s - Failed allocating memory", coordinate_name);
469467
goto fail;
@@ -476,7 +474,6 @@ static void *fr_coordinate_thread(void *arg)
476474
}
477475

478476
sc->coord = fr_coord_create(ctx, sc->el, coord_reg, false, sc->max_workers);
479-
480477
if (!sc->coord) {
481478
PERROR("%s - Failed creating coordinator thread", coordinate_name);
482479
goto fail;
@@ -520,42 +517,43 @@ static void *fr_coordinate_thread(void *arg)
520517
*/
521518
int fr_coord_start(uint32_t num_workers, fr_sem_t *sem)
522519
{
523-
fr_coord_reg_t *coord_reg = NULL;
524-
fr_schedule_coord_t *sc;
525-
526520
if (!coord_regs) return 0;
527521

528522
MEM(coord_threads = talloc(NULL, fr_dlist_head_t));
529523
fr_dlist_init(coord_threads, fr_schedule_coord_t, entry);
530524
fr_rb_inline_talloc_init(&coords, fr_coord_t, node, coord_cmp, NULL);
531525

532-
while ((coord_reg = fr_dlist_next(coord_regs, coord_reg))) {
526+
fr_dlist_foreach(coord_regs, fr_coord_reg_t, coord_reg) {
527+
fr_schedule_coord_t *sc;
528+
533529
MEM(sc = talloc_zero(coord_threads, fr_schedule_coord_t));
534530

535531
sc->coord_reg = coord_reg;
536532
sc->max_workers = num_workers;
537533
sc->sem = sem;
534+
538535
if (fr_schedule_pthread_create(&sc->pthread_id, fr_coordinate_thread, sc) < 0) {
536+
talloc_free(sc);
539537
PERROR("Failed creating coordinator %s", coord_reg->name);
540538
return -1;
541539
};
540+
542541
fr_dlist_insert_tail(coord_threads, sc);
543542
}
544543

545544
/*
546545
* See if all the coordinators have started.
547546
*/
548-
sc = NULL;
549-
while ((sc = fr_dlist_next(coord_threads, sc))) {
547+
fr_dlist_foreach(coord_threads, fr_schedule_coord_t, sc) {
550548
DEBUG3("Waiting for semaphore from coordinator %s", sc->coord_reg->name);
551549
SEM_WAIT_INTR(sem);
552550
}
553551

554552
/*
555553
* Insert the coordinators in the tree
556554
*/
557-
sc = NULL;
558-
while ((sc = fr_dlist_next(coord_threads, sc))) {
555+
fr_dlist_foreach(coord_threads, fr_schedule_coord_t, sc) {
556+
fr_assert(sc->coord);
559557
fr_rb_insert(&coords, sc->coord);
560558
}
561559

@@ -571,7 +569,7 @@ void fr_coords_destroy(void)
571569

572570
if (fr_rb_num_elements(&coords) == 0) return;
573571

574-
while((coord = fr_rb_iter_init_inorder(&coords, &iter))) {
572+
while ((coord = fr_rb_iter_init_inorder(&coords, &iter))) {
575573
fr_rb_iter_delete_inorder(&coords, &iter);
576574
talloc_free(coord);
577575
}
@@ -581,13 +579,11 @@ void fr_coords_destroy(void)
581579
*/
582580
int fr_coords_create(TALLOC_CTX *ctx, fr_event_list_t *el)
583581
{
584-
fr_coord_reg_t *coord_reg = NULL;
585-
586582
if (!coord_regs) return 0;
587583

588584
fr_rb_inline_talloc_init(&coords, fr_coord_t, node, coord_cmp, NULL);
589585

590-
while ((coord_reg = fr_dlist_next(coord_regs, coord_reg))) {
586+
fr_dlist_foreach(coord_regs, fr_coord_reg_t, coord_reg) {
591587
char coordinate_name[64];
592588
fr_coord_t *coord;
593589

@@ -596,7 +592,6 @@ int fr_coords_create(TALLOC_CTX *ctx, fr_event_list_t *el)
596592
INFO("%s - Starting", coordinate_name);
597593

598594
coord = fr_coord_create(ctx, el, coord_reg, true, 1);
599-
600595
if (!coord) {
601596
PERROR("%s - Failed creating coordinator thread", coordinate_name);
602597
return -1;
@@ -779,7 +774,7 @@ int fr_worker_to_coord_send(fr_coord_worker_t *cw, uint32_t cb_id, fr_dbuff_t *d
779774
.worker = fr_schedule_worker_id()
780775
};
781776

782-
cd = (fr_coord_data_t *)fr_message_alloc(cw->worker_send_ms, (fr_message_t *)cd, fr_dbuff_used(dbuff));
777+
cd = (fr_coord_data_t *) fr_message_alloc(cw->worker_send_ms, (fr_message_t *)cd, fr_dbuff_used(dbuff));
783778
if (!cd) return -1;
784779

785780
memcpy(cd->m.data, fr_dbuff_buff(dbuff), fr_dbuff_used(dbuff));

0 commit comments

Comments
 (0)