schedwrk.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2009-2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <corosync/totem/totempg.h>
  36. #include <corosync/hdb.h>
  37. #include "schedwrk.h"
  38. static void (*serialize_lock) (void);
  39. static void (*serialize_unlock) (void);
  40. DECLARE_HDB_DATABASE (schedwrk_instance_database,NULL);
  41. struct schedwrk_instance {
  42. int (*schedwrk_fn) (const void *);
  43. const void *context;
  44. void *callback_handle;
  45. int lock;
  46. };
  47. union u {
  48. hdb_handle_t h;
  49. const void *v;
  50. };
  51. static hdb_handle_t
  52. void2handle (const void *v) { union u u; u.v = v; return u.h; }
  53. static const void *
  54. handle2void (hdb_handle_t h) { union u u; u.h = h; return u.v; }
  55. static int schedwrk_do (enum totem_callback_token_type type, const void *context)
  56. {
  57. hdb_handle_t handle = void2handle (context);
  58. struct schedwrk_instance *instance;
  59. int res;
  60. res = hdb_handle_get (&schedwrk_instance_database,
  61. hdb_nocheck_convert (handle),
  62. (void *)&instance);
  63. if (res != 0) {
  64. goto error_exit;
  65. }
  66. if (instance->lock)
  67. serialize_lock ();
  68. res = instance->schedwrk_fn (instance->context);
  69. if (instance->lock)
  70. serialize_unlock ();
  71. if (res == 0) {
  72. hdb_handle_destroy (&schedwrk_instance_database, hdb_nocheck_convert (handle));
  73. }
  74. hdb_handle_put (&schedwrk_instance_database,
  75. hdb_nocheck_convert (handle));
  76. return (res);
  77. error_exit:
  78. return (-1);
  79. }
  80. void schedwrk_init (
  81. void (*serialize_lock_fn) (void),
  82. void (*serialize_unlock_fn) (void))
  83. {
  84. serialize_lock = serialize_lock_fn;
  85. serialize_unlock = serialize_unlock_fn;
  86. }
  87. static int schedwrk_internal_create (
  88. hdb_handle_t *handle,
  89. int (schedwrk_fn) (const void *),
  90. const void *context,
  91. int lock)
  92. {
  93. struct schedwrk_instance *instance;
  94. int res;
  95. res = hdb_handle_create (&schedwrk_instance_database,
  96. sizeof (struct schedwrk_instance), handle);
  97. if (res != 0) {
  98. goto error_exit;
  99. }
  100. res = hdb_handle_get (&schedwrk_instance_database, *handle,
  101. (void *)&instance);
  102. if (res != 0) {
  103. goto error_destroy;
  104. }
  105. totempg_callback_token_create (
  106. &instance->callback_handle,
  107. TOTEM_CALLBACK_TOKEN_SENT,
  108. 1,
  109. schedwrk_do,
  110. handle2void (*handle));
  111. instance->schedwrk_fn = schedwrk_fn;
  112. instance->context = context;
  113. instance->lock = lock;
  114. hdb_handle_put (&schedwrk_instance_database, *handle);
  115. return (0);
  116. error_destroy:
  117. hdb_handle_destroy (&schedwrk_instance_database, *handle);
  118. error_exit:
  119. return (-1);
  120. }
  121. int schedwrk_create (
  122. hdb_handle_t *handle,
  123. int (schedwrk_fn) (const void *),
  124. const void *context)
  125. {
  126. return schedwrk_internal_create (handle, schedwrk_fn, context, 1);
  127. }
  128. int schedwrk_create_nolock (
  129. hdb_handle_t *handle,
  130. int (schedwrk_fn) (const void *),
  131. const void *context)
  132. {
  133. return schedwrk_internal_create (handle, schedwrk_fn, context, 0);
  134. }
  135. void schedwrk_destroy (hdb_handle_t handle)
  136. {
  137. hdb_handle_destroy (&schedwrk_instance_database, handle);
  138. }