summaryrefslogtreecommitdiffstats
path: root/lib/isc/managers.c
blob: 5e9a6c3731207006ef03092bf843d752d6634af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#include <isc/managers.h>
#include <isc/util.h>

#include "netmgr_p.h"
#include "task_p.h"
#include "timer_p.h"

isc_result_t
isc_managers_create(isc_mem_t *mctx, size_t workers, size_t quantum,
		    isc_nm_t **netmgrp, isc_taskmgr_t **taskmgrp,
		    isc_timermgr_t **timermgrp) {
	isc_result_t result;
	isc_nm_t *netmgr = NULL;
	isc_taskmgr_t *taskmgr = NULL;
	isc_timermgr_t *timermgr = NULL;

	REQUIRE(netmgrp != NULL && *netmgrp == NULL);
	isc__netmgr_create(mctx, workers, &netmgr);
	*netmgrp = netmgr;
	INSIST(netmgr != NULL);

	REQUIRE(taskmgrp == NULL || *taskmgrp == NULL);
	if (taskmgrp != NULL) {
		INSIST(netmgr != NULL);
		result = isc__taskmgr_create(mctx, quantum, netmgr, &taskmgr);
		if (result != ISC_R_SUCCESS) {
			UNEXPECTED_ERROR("isc_taskmgr_create() failed: %s",
					 isc_result_totext(result));
			goto fail;
		}
		*taskmgrp = taskmgr;
	}

	REQUIRE(timermgrp == NULL || *timermgrp == NULL);
	if (timermgrp != NULL) {
		result = isc__timermgr_create(mctx, &timermgr);
		if (result != ISC_R_SUCCESS) {
			UNEXPECTED_ERROR("isc_timermgr_create() failed: %s",
					 isc_result_totext(result));
			goto fail;
		}
		*timermgrp = timermgr;
	}

	return (ISC_R_SUCCESS);
fail:
	isc_managers_destroy(netmgrp, taskmgrp, timermgrp);

	return (result);
}

void
isc_managers_destroy(isc_nm_t **netmgrp, isc_taskmgr_t **taskmgrp,
		     isc_timermgr_t **timermgrp) {
	/*
	 * If we have a taskmgr to clean up, then we must also have a netmgr.
	 */
	REQUIRE(taskmgrp == NULL || netmgrp != NULL);

	/*
	 * The sequence of operations here is important:
	 *
	 * 1. Initiate shutdown of the taskmgr, sending shutdown events to
	 * all tasks that are not already shutting down.
	 */
	if (taskmgrp != NULL) {
		INSIST(*taskmgrp != NULL);
		isc__taskmgr_shutdown(*taskmgrp);
	}

	/*
	 * 2. Initiate shutdown of the network manager, freeing clients
	 * and other resources and preventing new connections, but do
	 * not stop processing of existing events.
	 */
	if (netmgrp != NULL) {
		INSIST(*netmgrp != NULL);
		isc__netmgr_shutdown(*netmgrp);
	}

	/*
	 * 3. Finish destruction of the task manager when all tasks
	 * have completed.
	 */
	if (taskmgrp != NULL) {
		isc__taskmgr_destroy(taskmgrp);
	}

	/*
	 * 4. Finish destruction of the netmgr, and wait until all
	 * references have been released.
	 */
	if (netmgrp != NULL) {
		isc__netmgr_destroy(netmgrp);
	}

	/*
	 * 5. Clean up the remaining managers.
	 */
	if (timermgrp != NULL) {
		INSIST(*timermgrp != NULL);
		isc__timermgr_destroy(timermgrp);
	}
}