Home | History | Annotate | Line # | Download | only in executor
      1 /*	$NetBSD: executor.c,v 1.3 2024/07/06 09:53:19 andvar Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: executor.c,v 1.3 2024/07/06 09:53:19 andvar Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/module.h>
     34 #include <sys/callout.h>
     35 #include <sys/once.h>
     36 
     37 /*
     38  * Check if dmesg prints
     39  * "executor <no-of-seconds-after-loading>" every second
     40  * and "executor once" exactly once in the beginning
     41  * to test this module
     42  */
     43 
     44 MODULE(MODULE_CLASS_MISC, executor, NULL);
     45 
     46 static void callout_example(void *);
     47 static int runonce_example(void);
     48 
     49 static int executor_count = 0; //To hold the count of seconds
     50 
     51 static callout_t sc;
     52 
     53 /* Creating a variable that marks whether the function has executed or not */
     54 static once_t ctl;
     55 static ONCE_DECL(ctl);
     56 
     57 /*
     58  * runonce_example : This function should execute only once
     59  * It should return 0 as RUN_ONCE calls the function until it returns 0.
     60  */
     61 
     62 static int
     63 runonce_example(void) {
     64 	printf("executor once\n");
     65 	return 0;
     66 }
     67 
     68 /*
     69  * callout_example : This function should get executed every second.
     70  * It calls runonce_example each time.
     71  * It prints the seconds elapsed after the module was loaded.
     72  * It reschedules the callout to the next second using callout_schedule
     73  */
     74 
     75 static void
     76 callout_example(void *arg) {
     77 	RUN_ONCE(&ctl, runonce_example);
     78 	executor_count++;
     79 	printf("Callout %d\n", executor_count);
     80 	callout_schedule(&sc, mstohz(1000));
     81 }
     82 
     83 /*
     84  * executor_modcmd : has two tasks
     85  * On loading the module it needs to initialize a callout handle and schedule
     86  * the first callout.
     87  * On unloading the module it needs to stop and destroy the callout handle
     88  */
     89 
     90 static int
     91 executor_modcmd(modcmd_t cmd, void *arg)
     92 {
     93 	switch(cmd) {
     94 	case MODULE_CMD_INIT:
     95 		printf("executor module inserted\n");
     96 		callout_init(&sc, CALLOUT_MPSAFE);
     97 		callout_reset(&sc, mstohz(1000), callout_example, NULL);
     98 		break;
     99 	case MODULE_CMD_FINI:
    100 		printf("executor module unloaded\n");
    101 		callout_halt(&sc, NULL);
    102 		callout_destroy(&sc);
    103 		break;
    104 	default:
    105 		return ENOTTY;
    106 	}
    107 	return 0;
    108 }
    109