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