1 /* krxtimod.c: RXRPC timeout daemon
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/completion.h>
16 #include <linux/freezer.h>
17 #include <rxrpc/rxrpc.h>
18 #include <rxrpc/krxtimod.h>
19 #include <asm/errno.h>
22 static DECLARE_COMPLETION(krxtimod_alive);
23 static DECLARE_COMPLETION(krxtimod_dead);
24 static DECLARE_WAIT_QUEUE_HEAD(krxtimod_sleepq);
25 static int krxtimod_die;
27 static LIST_HEAD(krxtimod_list);
28 static DEFINE_SPINLOCK(krxtimod_lock);
30 static int krxtimod(void *arg);
32 /*****************************************************************************/
34 * start the timeout daemon
36 int rxrpc_krxtimod_start(void)
40 ret = kernel_thread(krxtimod, NULL, 0);
44 wait_for_completion(&krxtimod_alive);
47 } /* end rxrpc_krxtimod_start() */
49 /*****************************************************************************/
51 * stop the timeout daemon
53 void rxrpc_krxtimod_kill(void)
55 /* get rid of my daemon */
57 wake_up(&krxtimod_sleepq);
58 wait_for_completion(&krxtimod_dead);
60 } /* end rxrpc_krxtimod_kill() */
62 /*****************************************************************************/
64 * timeout processing daemon
66 static int krxtimod(void *arg)
68 DECLARE_WAITQUEUE(myself, current);
72 printk("Started krxtimod %d\n", current->pid);
74 daemonize("krxtimod");
76 complete(&krxtimod_alive);
78 /* loop around looking for things to attend to */
80 set_current_state(TASK_INTERRUPTIBLE);
81 add_wait_queue(&krxtimod_sleepq, &myself);
87 /* deal with the server being asked to die */
89 remove_wait_queue(&krxtimod_sleepq, &myself);
91 complete_and_exit(&krxtimod_dead, 0);
96 /* discard pending signals */
97 rxrpc_discard_my_signals();
99 /* work out the time to elapse before the next event */
100 spin_lock(&krxtimod_lock);
101 if (list_empty(&krxtimod_list)) {
102 timeout = MAX_SCHEDULE_TIMEOUT;
105 timer = list_entry(krxtimod_list.next,
106 rxrpc_timer_t, link);
107 timeout = timer->timo_jif;
110 if (time_before_eq((unsigned long) timeout, jif))
114 timeout = (long) timeout - (long) jiffies;
117 spin_unlock(&krxtimod_lock);
119 schedule_timeout(timeout);
121 set_current_state(TASK_INTERRUPTIBLE);
124 /* the thing on the front of the queue needs processing
125 * - we come here with the lock held and timer pointing to the expired
129 remove_wait_queue(&krxtimod_sleepq, &myself);
130 set_current_state(TASK_RUNNING);
132 _debug("@@@ Begin Timeout of %p", timer);
134 /* dequeue the timer */
135 list_del_init(&timer->link);
136 spin_unlock(&krxtimod_lock);
138 /* call the timeout function */
139 timer->ops->timed_out(timer);
141 _debug("@@@ End Timeout");
144 } /* end krxtimod() */
146 /*****************************************************************************/
150 void rxrpc_krxtimod_add_timer(rxrpc_timer_t *timer, unsigned long timeout)
152 struct list_head *_p;
153 rxrpc_timer_t *ptimer;
155 _enter("%p,%lu", timer, timeout);
157 spin_lock(&krxtimod_lock);
159 list_del(&timer->link);
161 /* the timer was deferred or reset - put it back in the queue at the
163 timer->timo_jif = jiffies + timeout;
165 list_for_each(_p, &krxtimod_list) {
166 ptimer = list_entry(_p, rxrpc_timer_t, link);
167 if (time_before(timer->timo_jif, ptimer->timo_jif))
171 list_add_tail(&timer->link, _p); /* insert before stopping point */
173 spin_unlock(&krxtimod_lock);
175 wake_up(&krxtimod_sleepq);
178 } /* end rxrpc_krxtimod_add_timer() */
180 /*****************************************************************************/
183 * - returns 0 if the timer was deleted or -ENOENT if it wasn't queued
185 int rxrpc_krxtimod_del_timer(rxrpc_timer_t *timer)
191 spin_lock(&krxtimod_lock);
193 if (list_empty(&timer->link))
196 list_del_init(&timer->link);
198 spin_unlock(&krxtimod_lock);
200 wake_up(&krxtimod_sleepq);
202 _leave(" = %d", ret);
204 } /* end rxrpc_krxtimod_del_timer() */