]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/lablgtksourceview/test/test.txt
ocaml 3.09 transition
[helm.git] / helm / DEVEL / lablgtksourceview / test / test.txt
1 /* CPU control.
2  * (C) 2001, 2002, 2003, 2004 Rusty Russell
3  *
4  * This code is licenced under the GPL.
5  */
6 #include <linux/proc_fs.h>
7 #include <linux/smp.h>
8 #include <linux/init.h>
9 #include <linux/notifier.h>
10 #include <linux/sched.h>
11 #include <linux/unistd.h>
12 #include <linux/cpu.h>
13 #include <linux/module.h>
14 #include <linux/kmod.h>         /* for hotplug_path */
15 #include <linux/kthread.h>
16 #include <linux/stop_machine.h>
17 #include <asm/semaphore.h>
18
19 /* This protects CPUs going up and down... */
20 DECLARE_MUTEX(cpucontrol);
21
22 static struct notifier_block *cpu_chain;
23
24 /* Need to know about CPUs going up/down? */
25 int register_cpu_notifier(struct notifier_block *nb)
26 {
27         int ret;
28
29         if ((ret = down_interruptible(&cpucontrol)) != 0)
30                 return ret;
31         ret = notifier_chain_register(&cpu_chain, nb);
32         up(&cpucontrol);
33         return ret;
34 }
35 EXPORT_SYMBOL(register_cpu_notifier);
36
37 void unregister_cpu_notifier(struct notifier_block *nb)
38 {
39         down(&cpucontrol);
40         notifier_chain_unregister(&cpu_chain, nb);
41         up(&cpucontrol);
42 }
43 EXPORT_SYMBOL(unregister_cpu_notifier);
44
45 #ifdef CONFIG_HOTPLUG_CPU
46 static inline void check_for_tasks(int cpu)
47 {
48         struct task_struct *p;
49
50         write_lock_irq(&tasklist_lock);
51         for_each_process(p) {
52                 if (task_cpu(p) == cpu && (p->utime != 0 || p->stime != 0))
53                         printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
54                                 (state = %ld, flags = %lx) \n",
55                                  p->comm, p->pid, cpu, p->state, p->flags);
56         }
57         write_unlock_irq(&tasklist_lock);
58 }
59
60 /* Notify userspace when a cpu event occurs, by running '/sbin/hotplug
61  * cpu' with certain environment variables set.  */
62 static int cpu_run_sbin_hotplug(unsigned int cpu, const char *action)
63 {
64         char *argv[3], *envp[5], cpu_str[12], action_str[32];
65         int i;
66
67         sprintf(cpu_str, "CPU=%d", cpu);
68         sprintf(action_str, "ACTION=%s", action);
69         /* FIXME: Add DEVPATH. --RR */
70
71         i = 0;
72         argv[i++] = hotplug_path;
73         argv[i++] = "cpu";
74         argv[i] = NULL;
75
76         i = 0;
77         /* minimal command environment */
78         envp[i++] = "HOME=/";
79         envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
80         envp[i++] = cpu_str;
81         envp[i++] = action_str;
82         envp[i] = NULL;
83
84         return call_usermodehelper(argv[0], argv, envp, 0);
85 }
86
87 /* Take this CPU down. */
88 static int take_cpu_down(void *unused)
89 {
90         int err;
91
92         /* Take offline: makes arch_cpu_down somewhat easier. */
93         cpu_clear(smp_processor_id(), cpu_online_map);
94
95         /* Ensure this CPU doesn't handle any more interrupts. */
96         err = __cpu_disable();
97         if (err < 0)
98                 cpu_set(smp_processor_id(), cpu_online_map);
99         else
100                 /* Force idle task to run as soon as we yield: it should
101                    immediately notice cpu is offline and die quickly. */
102                 sched_idle_next();
103
104         return err;
105 }
106
107 int cpu_down(unsigned int cpu)
108 {
109         int err;
110         struct task_struct *p;
111         cpumask_t old_allowed, tmp;
112
113         if ((err = lock_cpu_hotplug_interruptible()) != 0)
114                 return err;
115
116         if (num_online_cpus() == 1) {
117                 err = -EBUSY;
118                 goto out;
119         }
120
121         if (!cpu_online(cpu)) {
122                 err = -EINVAL;
123                 goto out;
124         }
125
126         /* Ensure that we are not runnable on dying cpu */
127         old_allowed = current->cpus_allowed;
128         tmp = CPU_MASK_ALL;
129         cpu_clear(cpu, tmp);
130         set_cpus_allowed(current, tmp);
131
132         p = __stop_machine_run(take_cpu_down, NULL, cpu);
133         if (IS_ERR(p)) {
134                 err = PTR_ERR(p);
135                 goto out_allowed;
136         }
137
138         if (cpu_online(cpu))
139                 goto out_thread;
140
141         /* Wait for it to sleep (leaving idle task). */
142         while (!idle_cpu(cpu))
143                 yield();
144
145         /* This actually kills the CPU. */
146         __cpu_die(cpu);
147
148         /* Move it here so it can run. */
149         kthread_bind(p, smp_processor_id());
150
151         /* CPU is completely dead: tell everyone.  Too late to complain. */
152         if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
153             == NOTIFY_BAD)
154                 BUG();
155
156         check_for_tasks(cpu);
157
158         cpu_run_sbin_hotplug(cpu, "offline");
159
160 out_thread:
161         err = kthread_stop(p);
162 out_allowed:
163         set_cpus_allowed(current, old_allowed);
164 out:
165         unlock_cpu_hotplug();
166         return err;
167 }
168 #else
169 static inline int cpu_run_sbin_hotplug(unsigned int cpu, const char *action)
170 {
171         return 0;
172 }
173 #endif /*CONFIG_HOTPLUG_CPU*/
174
175 int __devinit cpu_up(unsigned int cpu)
176 {
177         int ret;
178         void *hcpu = (void *)(long)cpu;
179
180         if ((ret = down_interruptible(&cpucontrol)) != 0)
181                 return ret;
182
183         if (cpu_online(cpu) || !cpu_present(cpu)) {
184                 ret = -EINVAL;
185                 goto out;
186         }
187         ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
188         if (ret == NOTIFY_BAD) {
189                 printk("%s: attempt to bring up CPU %u failed\n",
190                                 __FUNCTION__, cpu);
191                 ret = -EINVAL;
192                 goto out_notify;
193         }
194
195         /* Arch-specific enabling code. */
196         ret = __cpu_up(cpu);
197         if (ret != 0)
198                 goto out_notify;
199         if (!cpu_online(cpu))
200                 BUG();
201
202         /* Now call notifier in preparation. */
203         notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
204
205 out_notify:
206         if (ret != 0)
207                 notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
208 out:
209         up(&cpucontrol);
210         return ret;
211 }