patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / kernel / power / process.c
1 /*
2  * drivers/power/process.c - Functions for starting/stopping processes on 
3  *                           suspend transitions.
4  *
5  * Originally from swsusp.
6  */
7
8
9 #undef DEBUG
10
11 #include <linux/smp_lock.h>
12 #include <linux/interrupt.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15
16 /* 
17  * Timeout for stopping processes
18  */
19 #define TIMEOUT (6 * HZ)
20
21
22 static inline int freezeable(struct task_struct * p)
23 {
24         if ((p == current) || 
25             (p->flags & PF_NOFREEZE) ||
26             (p->state == TASK_ZOMBIE) ||
27             (p->state == TASK_DEAD) ||
28             (p->state == TASK_STOPPED))
29                 return 0;
30         return 1;
31 }
32
33 /* Refrigerator is place where frozen processes are stored :-). */
34 void refrigerator(unsigned long flag)
35 {
36         /* Hmm, should we be allowed to suspend when there are realtime
37            processes around? */
38         long save;
39         save = current->state;
40         current->state = TASK_UNINTERRUPTIBLE;
41         pr_debug("%s entered refrigerator\n", current->comm);
42         printk("=");
43         current->flags &= ~PF_FREEZE;
44
45         spin_lock_irq(&current->sighand->siglock);
46         recalc_sigpending(); /* We sent fake signal, clean it up */
47         spin_unlock_irq(&current->sighand->siglock);
48
49         current->flags |= PF_FROZEN;
50         while (current->flags & PF_FROZEN)
51                 schedule();
52         pr_debug("%s left refrigerator\n", current->comm);
53         current->state = save;
54 }
55
56 /* 0 = success, else # of processes that we failed to stop */
57 int freeze_processes(void)
58 {
59        int todo;
60        unsigned long start_time;
61         struct task_struct *g, *p;
62         
63         printk( "Stopping tasks: " );
64         start_time = jiffies;
65         do {
66                 todo = 0;
67                 read_lock(&tasklist_lock);
68                 do_each_thread(g, p) {
69                         unsigned long flags;
70                         if (!freezeable(p))
71                                 continue;
72                         if ((p->flags & PF_FROZEN) ||
73                             (p->state == TASK_STOPPED))
74                                 continue;
75
76                         /* FIXME: smp problem here: we may not access other process' flags
77                            without locking */
78                         p->flags |= PF_FREEZE;
79                         spin_lock_irqsave(&p->sighand->siglock, flags);
80                         signal_wake_up(p, 0);
81                         spin_unlock_irqrestore(&p->sighand->siglock, flags);
82                         todo++;
83                 } while_each_thread(g, p);
84                 read_unlock(&tasklist_lock);
85                 yield();                        /* Yield is okay here */
86                 if (time_after(jiffies, start_time + TIMEOUT)) {
87                         printk( "\n" );
88                         printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo );
89                         return todo;
90                 }
91         } while(todo);
92         
93         printk( "|\n" );
94         BUG_ON(in_atomic());
95         return 0;
96 }
97
98 void thaw_processes(void)
99 {
100         struct task_struct *g, *p;
101
102         printk( "Restarting tasks..." );
103         read_lock(&tasklist_lock);
104         do_each_thread(g, p) {
105                 if (!freezeable(p))
106                         continue;
107                 if (p->flags & PF_FROZEN) {
108                         p->flags &= ~PF_FROZEN;
109                         wake_up_process(p);
110                 } else
111                         printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
112                 wake_up_process(p);
113         } while_each_thread(g, p);
114
115         read_unlock(&tasklist_lock);
116         schedule();
117         printk( " done\n" );
118 }
119
120 EXPORT_SYMBOL(refrigerator);