Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux/fpc-iii.git] / net / rxrpc / krxtimod.c
blob3e7466900bd4e1260728ed63759d60794a88c69e
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 <rxrpc/rxrpc.h>
17 #include <rxrpc/krxtimod.h>
18 #include <asm/errno.h>
19 #include "internal.h"
21 static DECLARE_COMPLETION(krxtimod_alive);
22 static DECLARE_COMPLETION(krxtimod_dead);
23 static DECLARE_WAIT_QUEUE_HEAD(krxtimod_sleepq);
24 static int krxtimod_die;
26 static LIST_HEAD(krxtimod_list);
27 static DEFINE_SPINLOCK(krxtimod_lock);
29 static int krxtimod(void *arg);
31 /*****************************************************************************/
33 * start the timeout daemon
35 int rxrpc_krxtimod_start(void)
37 int ret;
39 ret = kernel_thread(krxtimod, NULL, 0);
40 if (ret < 0)
41 return ret;
43 wait_for_completion(&krxtimod_alive);
45 return ret;
46 } /* end rxrpc_krxtimod_start() */
48 /*****************************************************************************/
50 * stop the timeout daemon
52 void rxrpc_krxtimod_kill(void)
54 /* get rid of my daemon */
55 krxtimod_die = 1;
56 wake_up(&krxtimod_sleepq);
57 wait_for_completion(&krxtimod_dead);
59 } /* end rxrpc_krxtimod_kill() */
61 /*****************************************************************************/
63 * timeout processing daemon
65 static int krxtimod(void *arg)
67 DECLARE_WAITQUEUE(myself, current);
69 rxrpc_timer_t *timer;
71 printk("Started krxtimod %d\n", current->pid);
73 daemonize("krxtimod");
75 complete(&krxtimod_alive);
77 /* loop around looking for things to attend to */
78 loop:
79 set_current_state(TASK_INTERRUPTIBLE);
80 add_wait_queue(&krxtimod_sleepq, &myself);
82 for (;;) {
83 unsigned long jif;
84 long timeout;
86 /* deal with the server being asked to die */
87 if (krxtimod_die) {
88 remove_wait_queue(&krxtimod_sleepq, &myself);
89 _leave("");
90 complete_and_exit(&krxtimod_dead, 0);
93 try_to_freeze();
95 /* discard pending signals */
96 rxrpc_discard_my_signals();
98 /* work out the time to elapse before the next event */
99 spin_lock(&krxtimod_lock);
100 if (list_empty(&krxtimod_list)) {
101 timeout = MAX_SCHEDULE_TIMEOUT;
103 else {
104 timer = list_entry(krxtimod_list.next,
105 rxrpc_timer_t, link);
106 timeout = timer->timo_jif;
107 jif = jiffies;
109 if (time_before_eq((unsigned long) timeout, jif))
110 goto immediate;
112 else {
113 timeout = (long) timeout - (long) jiffies;
116 spin_unlock(&krxtimod_lock);
118 schedule_timeout(timeout);
120 set_current_state(TASK_INTERRUPTIBLE);
123 /* the thing on the front of the queue needs processing
124 * - we come here with the lock held and timer pointing to the expired
125 * entry
127 immediate:
128 remove_wait_queue(&krxtimod_sleepq, &myself);
129 set_current_state(TASK_RUNNING);
131 _debug("@@@ Begin Timeout of %p", timer);
133 /* dequeue the timer */
134 list_del_init(&timer->link);
135 spin_unlock(&krxtimod_lock);
137 /* call the timeout function */
138 timer->ops->timed_out(timer);
140 _debug("@@@ End Timeout");
141 goto loop;
143 } /* end krxtimod() */
145 /*****************************************************************************/
147 * (re-)queue a timer
149 void rxrpc_krxtimod_add_timer(rxrpc_timer_t *timer, unsigned long timeout)
151 struct list_head *_p;
152 rxrpc_timer_t *ptimer;
154 _enter("%p,%lu", timer, timeout);
156 spin_lock(&krxtimod_lock);
158 list_del(&timer->link);
160 /* the timer was deferred or reset - put it back in the queue at the
161 * right place */
162 timer->timo_jif = jiffies + timeout;
164 list_for_each(_p, &krxtimod_list) {
165 ptimer = list_entry(_p, rxrpc_timer_t, link);
166 if (time_before(timer->timo_jif, ptimer->timo_jif))
167 break;
170 list_add_tail(&timer->link, _p); /* insert before stopping point */
172 spin_unlock(&krxtimod_lock);
174 wake_up(&krxtimod_sleepq);
176 _leave("");
177 } /* end rxrpc_krxtimod_add_timer() */
179 /*****************************************************************************/
181 * dequeue a timer
182 * - returns 0 if the timer was deleted or -ENOENT if it wasn't queued
184 int rxrpc_krxtimod_del_timer(rxrpc_timer_t *timer)
186 int ret = 0;
188 _enter("%p", timer);
190 spin_lock(&krxtimod_lock);
192 if (list_empty(&timer->link))
193 ret = -ENOENT;
194 else
195 list_del_init(&timer->link);
197 spin_unlock(&krxtimod_lock);
199 wake_up(&krxtimod_sleepq);
201 _leave(" = %d", ret);
202 return ret;
203 } /* end rxrpc_krxtimod_del_timer() */