Skip to content

Commit 749a368

Browse files
committed
move common code to fr_thread_start / fr_thread_exit
1 parent 029a10d commit 749a368

File tree

4 files changed

+59
-45
lines changed

4 files changed

+59
-45
lines changed

src/lib/io/coord.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,13 @@ static void *fr_coordinate_thread(void *arg)
444444
{
445445
fr_schedule_coord_t *sc = talloc_get_type_abort(arg, fr_schedule_coord_t);
446446
fr_coord_reg_t *coord_reg = sc->coord_reg;
447+
fr_thread_status_t status = FR_THREAD_FAIL;
447448
char coordinate_name[64];
448449

449450
snprintf(coordinate_name, sizeof(coordinate_name), "Coordinate %s", coord_reg->name);
450451

451452
if (fr_thread_setup(&sc->thread, coordinate_name) < 0) goto fail;
452453

453-
INFO("%s - Starting", coordinate_name);
454-
455454
sc->coord = fr_coord_create(sc->thread.ctx, sc->thread.el, coord_reg, false, sc->max_workers);
456455
if (!sc->coord) {
457456
PERROR("%s - Failed creating coordinator thread", coordinate_name);
@@ -463,16 +462,19 @@ static void *fr_coordinate_thread(void *arg)
463462
*/
464463
if (fr_thread_instantiate(sc->thread.ctx, sc->thread.el) < 0) goto fail;
465464

466-
sem_post(sc->sem);
465+
/*
466+
* Tell the originator that the thread has started.
467+
*/
468+
fr_thread_start(&sc->thread, sc->sem);
467469

468470
fr_coordinate(sc->coord);
469471

470-
fail:
471-
INFO("%s - Exiting", coordinate_name);
472+
status = FR_THREAD_EXITED;
472473

474+
fail:
473475
fr_thread_detach();
474476

475-
talloc_free(sc->thread.ctx);
477+
fr_thread_exit(&sc->thread, status, sc->sem);
476478

477479
return NULL;
478480
}
@@ -487,6 +489,8 @@ static void *fr_coordinate_thread(void *arg)
487489
*/
488490
int fr_coord_start(uint32_t num_workers, fr_sem_t *sem)
489491
{
492+
int num = 0;
493+
490494
if (!coord_regs) return 0;
491495

492496
MEM(coord_threads = talloc(NULL, fr_dlist_head_t));
@@ -498,6 +502,7 @@ int fr_coord_start(uint32_t num_workers, fr_sem_t *sem)
498502

499503
MEM(sc = talloc_zero(coord_threads, fr_schedule_coord_t));
500504

505+
sc->thread.id = num++;
501506
sc->coord_reg = coord_reg;
502507
sc->max_workers = num_workers;
503508
sc->sem = sem;

src/lib/io/schedule.c

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ static void *fr_schedule_worker_thread(void *arg)
130130

131131
if (fr_thread_setup(&sw->thread, worker_name) < 0) goto fail;
132132

133-
INFO("%s - Starting", worker_name);
134-
135133
sw->worker = fr_worker_alloc(sw->thread.ctx, sw->thread.el, worker_name, sc->log, sc->lvl, &sc->config->worker);
136134
if (!sw->worker) {
137135
PERROR("%s - Failed creating worker", worker_name);
@@ -156,8 +154,6 @@ static void *fr_schedule_worker_thread(void *arg)
156154
}
157155
}
158156

159-
sw->thread.status = FR_THREAD_RUNNING;
160-
161157
/*
162158
* Add this worker to all network threads.
163159
*/
@@ -170,12 +166,10 @@ static void *fr_schedule_worker_thread(void *arg)
170166
}
171167
}
172168

173-
DEBUG3("%s - Started", worker_name);
174-
175169
/*
176170
* Tell the originator that the thread has started.
177171
*/
178-
sem_post(sc->worker_sem);
172+
fr_thread_start(&sw->thread, sc->worker_sem);
179173

180174
/*
181175
* Do all of the work.
@@ -185,30 +179,14 @@ static void *fr_schedule_worker_thread(void *arg)
185179
status = FR_THREAD_EXITED;
186180

187181
fail:
188-
sw->thread.status = status;
189-
190182
if (sw->worker) {
191183
fr_worker_destroy(sw->worker);
192184
sw->worker = NULL;
193185
}
194186

195-
INFO("%s - Exiting", worker_name);
196-
197187
if (sc->worker_thread_detach) sc->worker_thread_detach(NULL); /* Fixme once we figure out what uctx should be */
198188

199-
/*
200-
* Not looping at this point, but may catch timer/fd
201-
* insertions being done after the thread should have
202-
* exited.
203-
*/
204-
if (sw->thread.el) fr_event_loop_exit(sw->thread.el, 1);
205-
206-
/*
207-
* Tell the scheduler we're done.
208-
*/
209-
sem_post(sc->worker_sem);
210-
211-
talloc_free(sw->thread.ctx);
189+
fr_thread_exit(&sw->thread, status, sc->worker_sem);
212190

213191
return NULL;
214192
}
@@ -247,21 +225,18 @@ static void *fr_schedule_network_thread(void *arg)
247225
goto fail;
248226
}
249227

250-
sn->thread.status = FR_THREAD_RUNNING;
251-
252228
/*
253229
* Tell the originator that the thread has started.
254230
*/
255-
sem_post(sc->network_sem);
256-
257-
DEBUG3("%s - Started", network_name);
231+
fr_thread_start(&sn->thread, sc->network_sem);
258232

259233
/*
260234
* Print out statistics for this network IO handler.
261235
*/
262236
if (fr_time_delta_ispos(sc->config->stats_interval)) {
263237
(void) fr_timer_in(sn, sn->thread.el->tl, &sn->ev, sn->sc->config->stats_interval, false, stats_timer, sn);
264238
}
239+
265240
/*
266241
* Call the main event processing loop of the network
267242
* thread Will not return until the worker is about
@@ -272,16 +247,7 @@ static void *fr_schedule_network_thread(void *arg)
272247
status = FR_THREAD_EXITED;
273248

274249
fail:
275-
sn->thread.status = status;
276-
277-
INFO("%s - Exiting", network_name);
278-
279-
/*
280-
* Tell the scheduler we're done.
281-
*/
282-
sem_post(sc->network_sem);
283-
284-
talloc_free(sn->thread.ctx);
250+
fr_thread_exit(&sn->thread, status, sc->network_sem);
285251

286252
return NULL;
287253
}

src/lib/io/thread.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,42 @@ void fr_thread_detach(void)
193193
virtual_servers_thread_detach();
194194
modules_rlm_thread_detach();
195195
}
196+
197+
198+
/** Signal the parent that we're done.
199+
*
200+
* @param[out] thread which is starting
201+
* @param[in] sem semaphore to signal.
202+
*/
203+
void fr_thread_start(fr_thread_t *thread, fr_sem_t *sem)
204+
{
205+
thread->status = FR_THREAD_RUNNING;
206+
207+
INFO("%s - Starting", thread->name);
208+
209+
sem_post(sem);
210+
}
211+
212+
/** Signal the parent that we're done.
213+
*
214+
* @param[out] thread which is exiting
215+
* @param[in] status to write
216+
* @param[in] sem semaphore to signal.
217+
*/
218+
void fr_thread_exit(fr_thread_t *thread, fr_thread_status_t status, fr_sem_t *sem)
219+
{
220+
INFO("%s - Exiting", thread->name);
221+
222+
thread->status = status;
223+
224+
/*
225+
* Not looping at this point, but may catch timer/fd
226+
* insertions being done after the thread should have
227+
* exited.
228+
*/
229+
if (thread->el) fr_event_loop_exit(thread->el, 1);
230+
231+
TALLOC_FREE(thread->ctx);
232+
233+
sem_post(sem);
234+
}

src/lib/io/thread.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ int fr_thread_setup(fr_thread_t *out, char const *name) CC_HINT(nonnull);
6969
int fr_thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el) CC_HINT(nonnull);
7070

7171
void fr_thread_detach(void);
72+
73+
void fr_thread_start(fr_thread_t *thread, fr_sem_t *sem) CC_HINT(nonnull);
74+
75+
void fr_thread_exit(fr_thread_t *thread, fr_thread_status_t status, fr_sem_t *sem) CC_HINT(nonnull);

0 commit comments

Comments
 (0)