xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/isc/managers.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1 /*	$NetBSD: managers.c,v 1.1 2024/02/18 20:57:49 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #include <isc/managers.h>
17 #include <isc/util.h>
18 
19 #include "netmgr_p.h"
20 #include "task_p.h"
21 
22 isc_result_t
isc_managers_create(isc_mem_t * mctx,size_t workers,size_t quantum,isc_nm_t ** netmgrp,isc_taskmgr_t ** taskmgrp)23 isc_managers_create(isc_mem_t *mctx, size_t workers, size_t quantum,
24 		    isc_nm_t **netmgrp, isc_taskmgr_t **taskmgrp) {
25 	isc_result_t result;
26 	isc_taskmgr_t *taskmgr = NULL;
27 	isc_nm_t *netmgr = NULL;
28 
29 	REQUIRE(netmgrp != NULL && *netmgrp == NULL);
30 	isc__netmgr_create(mctx, workers, &netmgr);
31 	*netmgrp = netmgr;
32 	INSIST(netmgr != NULL);
33 
34 	REQUIRE(taskmgrp == NULL || *taskmgrp == NULL);
35 	if (taskmgrp != NULL) {
36 		INSIST(netmgr != NULL);
37 		result = isc__taskmgr_create(mctx, quantum, netmgr, &taskmgr);
38 		if (result != ISC_R_SUCCESS) {
39 			UNEXPECTED_ERROR(__FILE__, __LINE__,
40 					 "isc_managers_create() failed: %s",
41 					 isc_result_totext(result));
42 			goto fail;
43 		}
44 		*taskmgrp = taskmgr;
45 	}
46 
47 	return (ISC_R_SUCCESS);
48 fail:
49 	isc_managers_destroy(netmgrp, taskmgrp);
50 
51 	return (result);
52 }
53 
54 void
isc_managers_destroy(isc_nm_t ** netmgrp,isc_taskmgr_t ** taskmgrp)55 isc_managers_destroy(isc_nm_t **netmgrp, isc_taskmgr_t **taskmgrp) {
56 	/*
57 	 * If we have a taskmgr to clean up, then we must also have a netmgr.
58 	 */
59 	REQUIRE(taskmgrp != NULL || netmgrp == NULL);
60 
61 	/*
62 	 * The sequence of operations here is important:
63 	 *
64 	 * 1. Initiate shutdown of the taskmgr, sending shutdown events to
65 	 * all tasks that are not already shutting down.
66 	 */
67 	if (taskmgrp != NULL) {
68 		INSIST(*taskmgrp != NULL);
69 		isc__taskmgr_shutdown(*taskmgrp);
70 	}
71 
72 	/*
73 	 * 2. Initiate shutdown of the network manager, freeing clients
74 	 * and other resources and preventing new connections, but do
75 	 * not stop processing of existing events.
76 	 */
77 	if (netmgrp != NULL) {
78 		INSIST(*netmgrp != NULL);
79 		isc__netmgr_shutdown(*netmgrp);
80 	}
81 
82 	/*
83 	 * 3. Finish destruction of the task manager when all tasks
84 	 * have completed.
85 	 */
86 	if (taskmgrp != NULL) {
87 		isc__taskmgr_destroy(taskmgrp);
88 	}
89 
90 	/*
91 	 * 4. Finish destruction of the netmgr, and wait until all
92 	 * references have been released.
93 	 */
94 	if (netmgrp != NULL) {
95 		isc__netmgr_destroy(netmgrp);
96 	}
97 }
98