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>
19 #include "kafstimod.h"
20 #include <asm/errno.h>
23 static DECLARE_COMPLETION(kafstimod_alive);
24 static DECLARE_COMPLETION(kafstimod_dead);
25 static DECLARE_WAIT_QUEUE_HEAD(kafstimod_sleepq);
26 static int kafstimod_die;
28 static LIST_HEAD(kafstimod_list);
29 static DEFINE_SPINLOCK(kafstimod_lock);
31 static int kafstimod(void *arg);
34 * start the timeout daemon
36 int afs_kafstimod_start(void)
40 ret = kernel_thread(kafstimod, NULL, 0);
44 wait_for_completion(&kafstimod_alive);
50 * stop the timeout daemon
52 void afs_kafstimod_stop(void)
54 /* get rid of my daemon */
56 wake_up(&kafstimod_sleepq);
57 wait_for_completion(&kafstimod_dead);
61 * timeout processing daemon
63 static int kafstimod(void *arg)
65 struct afs_timer *timer;
67 DECLARE_WAITQUEUE(myself, current);
69 printk("kAFS: Started kafstimod %d\n", current->pid);
71 daemonize("kafstimod");
73 complete(&kafstimod_alive);
75 /* loop around looking for things to attend to */
77 set_current_state(TASK_INTERRUPTIBLE);
78 add_wait_queue(&kafstimod_sleepq, &myself);
84 /* deal with the server being asked to die */
86 remove_wait_queue(&kafstimod_sleepq, &myself);
88 complete_and_exit(&kafstimod_dead, 0);
93 /* discard pending signals */
94 afs_discard_my_signals();
96 /* work out the time to elapse before the next event */
97 spin_lock(&kafstimod_lock);
98 if (list_empty(&kafstimod_list)) {
99 timeout = MAX_SCHEDULE_TIMEOUT;
101 timer = list_entry(kafstimod_list.next,
102 struct afs_timer, link);
103 timeout = timer->timo_jif;
106 if (time_before_eq((unsigned long) timeout, jif))
108 timeout = (long) timeout - (long) jiffies;
110 spin_unlock(&kafstimod_lock);
112 schedule_timeout(timeout);
114 set_current_state(TASK_INTERRUPTIBLE);
117 /* the thing on the front of the queue needs processing
118 * - we come here with the lock held and timer pointing to the expired
122 remove_wait_queue(&kafstimod_sleepq, &myself);
123 set_current_state(TASK_RUNNING);
125 _debug("@@@ Begin Timeout of %p", timer);
127 /* dequeue the timer */
128 list_del_init(&timer->link);
129 spin_unlock(&kafstimod_lock);
131 /* call the timeout function */
132 timer->ops->timed_out(timer);
134 _debug("@@@ End Timeout");
141 void afs_kafstimod_add_timer(struct afs_timer *timer, unsigned long timeout)
143 struct afs_timer *ptimer;
144 struct list_head *_p;
146 _enter("%p,%lu", timer, timeout);
148 spin_lock(&kafstimod_lock);
150 list_del(&timer->link);
152 /* the timer was deferred or reset - put it back in the queue at the
154 timer->timo_jif = jiffies + timeout;
156 list_for_each(_p, &kafstimod_list) {
157 ptimer = list_entry(_p, struct afs_timer, link);
158 if (time_before(timer->timo_jif, ptimer->timo_jif))
162 list_add_tail(&timer->link, _p); /* insert before stopping point */
164 spin_unlock(&kafstimod_lock);
166 wake_up(&kafstimod_sleepq);
173 * - returns 0 if the timer was deleted or -ENOENT if it wasn't queued
175 int afs_kafstimod_del_timer(struct afs_timer *timer)
181 spin_lock(&kafstimod_lock);
183 if (list_empty(&timer->link))
186 list_del_init(&timer->link);
188 spin_unlock(&kafstimod_lock);
190 wake_up(&kafstimod_sleepq);
192 _leave(" = %d", ret);