process.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. printk( "Stopping tasks: " );
  53. start_time = jiffies;
  54. do {
  55. todo = 0;
  56. read_lock(&tasklist_lock);
  57. do_each_thread(g, p) {
  58. unsigned long flags;
  59. if (!freezeable(p))
  60. continue;
  61. if ((frozen(p)) ||
  62. (p->state == TASK_TRACED) ||
  63. (p->state == TASK_STOPPED))
  64. continue;
  65. freeze(p);
  66. spin_lock_irqsave(&p->sighand->siglock, flags);
  67. signal_wake_up(p, 0);
  68. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  69. todo++;
  70. } while_each_thread(g, p);
  71. read_unlock(&tasklist_lock);
  72. yield(); /* Yield is okay here */
  73. if (time_after(jiffies, start_time + TIMEOUT)) {
  74. printk( "\n" );
  75. printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo );
  76. return todo;
  77. }
  78. } while(todo);
  79. printk( "|\n" );
  80. BUG_ON(in_atomic());
  81. return 0;
  82. }
  83. void thaw_processes(void)
  84. {
  85. struct task_struct *g, *p;
  86. printk( "Restarting tasks..." );
  87. read_lock(&tasklist_lock);
  88. do_each_thread(g, p) {
  89. if (!freezeable(p))
  90. continue;
  91. if (!thaw_process(p))
  92. printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
  93. } while_each_thread(g, p);
  94. read_unlock(&tasklist_lock);
  95. schedule();
  96. printk( " done\n" );
  97. }
  98. EXPORT_SYMBOL(refrigerator);