handler.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * net/tipc/handler.c: TIPC signal handling
  3. *
  4. * Copyright (c) 2003-2005, Ericsson Research Canada
  5. * Copyright (c) 2005, Wind River Systems
  6. * Copyright (c) 2005-2006, Ericsson AB
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "core.h"
  34. struct queue_item {
  35. struct list_head next_signal;
  36. void (*handler) (unsigned long);
  37. unsigned long data;
  38. };
  39. static kmem_cache_t *tipc_queue_item_cache;
  40. static struct list_head signal_queue_head;
  41. static spinlock_t qitem_lock = SPIN_LOCK_UNLOCKED;
  42. static int handler_enabled = 0;
  43. static void process_signal_queue(unsigned long dummy);
  44. static DECLARE_TASKLET_DISABLED(tipc_tasklet, process_signal_queue, 0);
  45. unsigned int k_signal(Handler routine, unsigned long argument)
  46. {
  47. struct queue_item *item;
  48. if (!handler_enabled) {
  49. err("Signal request ignored by handler\n");
  50. return -ENOPROTOOPT;
  51. }
  52. spin_lock_bh(&qitem_lock);
  53. item = kmem_cache_alloc(tipc_queue_item_cache, GFP_ATOMIC);
  54. if (!item) {
  55. err("Signal queue out of memory\n");
  56. spin_unlock_bh(&qitem_lock);
  57. return -ENOMEM;
  58. }
  59. item->handler = routine;
  60. item->data = argument;
  61. list_add_tail(&item->next_signal, &signal_queue_head);
  62. spin_unlock_bh(&qitem_lock);
  63. tasklet_schedule(&tipc_tasklet);
  64. return 0;
  65. }
  66. static void process_signal_queue(unsigned long dummy)
  67. {
  68. struct queue_item *__volatile__ item;
  69. struct list_head *l, *n;
  70. spin_lock_bh(&qitem_lock);
  71. list_for_each_safe(l, n, &signal_queue_head) {
  72. item = list_entry(l, struct queue_item, next_signal);
  73. list_del(&item->next_signal);
  74. spin_unlock_bh(&qitem_lock);
  75. item->handler(item->data);
  76. spin_lock_bh(&qitem_lock);
  77. kmem_cache_free(tipc_queue_item_cache, item);
  78. }
  79. spin_unlock_bh(&qitem_lock);
  80. }
  81. int handler_start(void)
  82. {
  83. tipc_queue_item_cache =
  84. kmem_cache_create("tipc_queue_items", sizeof(struct queue_item),
  85. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  86. if (!tipc_queue_item_cache)
  87. return -ENOMEM;
  88. INIT_LIST_HEAD(&signal_queue_head);
  89. tasklet_enable(&tipc_tasklet);
  90. handler_enabled = 1;
  91. return 0;
  92. }
  93. void handler_stop(void)
  94. {
  95. struct list_head *l, *n;
  96. struct queue_item *item;
  97. if (!handler_enabled)
  98. return;
  99. handler_enabled = 0;
  100. tasklet_disable(&tipc_tasklet);
  101. tasklet_kill(&tipc_tasklet);
  102. spin_lock_bh(&qitem_lock);
  103. list_for_each_safe(l, n, &signal_queue_head) {
  104. item = list_entry(l, struct queue_item, next_signal);
  105. list_del(&item->next_signal);
  106. kmem_cache_free(tipc_queue_item_cache, item);
  107. }
  108. spin_unlock_bh(&qitem_lock);
  109. kmem_cache_destroy(tipc_queue_item_cache);
  110. }