process.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(unsigned long flag)
  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. current->flags &= ~PF_FREEZE;
  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. current->flags |= PF_FROZEN;
  42. while (current->flags & PF_FROZEN)
  43. schedule();
  44. pr_debug("%s left refrigerator\n", current->comm);
  45. current->state = save;
  46. }
  47. /* 0 = success, else # of processes that we failed to stop */
  48. int freeze_processes(void)
  49. {
  50. int todo;
  51. unsigned long start_time;
  52. struct task_struct *g, *p;
  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. unsigned long flags;
  60. if (!freezeable(p))
  61. continue;
  62. if ((p->flags & PF_FROZEN) ||
  63. (p->state == TASK_TRACED) ||
  64. (p->state == TASK_STOPPED))
  65. continue;
  66. /* FIXME: smp problem here: we may not access other process' flags
  67. without locking */
  68. p->flags |= PF_FREEZE;
  69. spin_lock_irqsave(&p->sighand->siglock, flags);
  70. signal_wake_up(p, 0);
  71. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  72. todo++;
  73. } while_each_thread(g, p);
  74. read_unlock(&tasklist_lock);
  75. yield(); /* Yield is okay here */
  76. if (time_after(jiffies, start_time + TIMEOUT)) {
  77. printk( "\n" );
  78. printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo );
  79. return todo;
  80. }
  81. } while(todo);
  82. printk( "|\n" );
  83. BUG_ON(in_atomic());
  84. return 0;
  85. }
  86. void thaw_processes(void)
  87. {
  88. struct task_struct *g, *p;
  89. printk( "Restarting tasks..." );
  90. read_lock(&tasklist_lock);
  91. do_each_thread(g, p) {
  92. if (!freezeable(p))
  93. continue;
  94. if (p->flags & PF_FROZEN) {
  95. p->flags &= ~PF_FROZEN;
  96. wake_up_process(p);
  97. } else
  98. printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
  99. } while_each_thread(g, p);
  100. read_unlock(&tasklist_lock);
  101. schedule();
  102. printk( " done\n" );
  103. }
  104. EXPORT_SYMBOL(refrigerator);