Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved
Copyright (c) 2017, Joyent, Inc. All Rights Reserved.
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with
the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
#include <sys/dditypes.h> #include <sys/sunddi.h> ddi_periodic_t ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval, int level);
The callback function to be invoked periodically in the specified interval.
The argument passed to the callback function.
The periodic interval time in nanoseconds.
The callback function is invoked at this priority level. If the value of level is zero, the callback function is invoked in kernel context. If the value is greater than zero, but less than or equal to ten, the callback function is invoked in interrupt context at the specified interrupt level, which may be used for real time applications. This value must be in range of 0-10, which can be either an integer literal, a pre-defined macro (DDI_IPL_0, ... , DDI_IPL_10), or the DDI_INTR_PRI macro with the interrupt priority.
As with timeout(9F), the exact time interval over which the function takes effect cannot be guaranteed, but the value given is a close approximation. If the callback function has not finished execution when the next interval expires, the system will skip running the callback for that interval.
In the following example, the device driver registers a periodic callback function invoked in kernel context.
static void my_periodic_func(void *arg) { /* * This handler is invoked periodically. */ struct my_state *statep = (struct my_state *)arg; mutex_enter(&statep->lock); if (load_unbalanced(statep)) { balance_tasks(statep); } mutex_exit(&statep->lock); } static void start_periodic_timer(struct my_state *statep) { hrtime_t interval = CHECK_INTERVAL; mutex_init(&statep->lock, NULL, MUTEX_DRIVER, DDI_IPL_0); /* * Register my_callback which is invoked periodically * in CHECK_INTERVAL in kernel context. */ statep->periodic_id = ddi_periodic_add(my_periodic_func, statep, interval, DDI_IPL_0);
In the following example, the device driver registers a callback function invoked in interrupt context at level 7.
/* * This handler is invoked periodically in interrupt context. */ static void my_periodic_int7_func(void *arg) { struct my_state *statep = (struct my_state *)arg; mutex_enter(&statep->lock); monitor_device(statep); mutex_exit(&statep->lock); } static void start_monitor_device(struct my_state *statep) { hrtime_t interval = MONITOR_INTERVAL; mutex_init(&statep->lock, NULL, MUTEX_DRIVER, DDI_IPL_7); /* * Register the callback function invoked periodically * at interrupt level 7. */ statep->periodic_id = ddi_periodic_add(my_periodic_int7_func, statep, interval, DDI_IPL_7); }