process.c 2.5 KB

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