reactor.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. (c) Copyright 2001-2009 The world wide DirectFB Open Source Community (directfb.org)
  3. (c) Copyright 2000-2004 Convergence (integrated media) GmbH
  4. All rights reserved.
  5. Written by Denis Oliver Kropp <dok@directfb.org>,
  6. Andreas Hundt <andi@fischlustig.de>,
  7. Sven Neumann <neo@directfb.org>,
  8. Ville Syrjälä <syrjala@sci.fi> and
  9. Claudio Ciccani <klan@users.sf.net>.
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the
  20. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22. */
  23. #ifndef __FUSION__REACTOR_H__
  24. #define __FUSION__REACTOR_H__
  25. #include <direct/list.h>
  26. #include <fusion/types.h>
  27. #include <fusion/call.h>
  28. #include <fusion/lock.h>
  29. typedef enum {
  30. RS_OK,
  31. RS_REMOVE,
  32. RS_DROP
  33. } ReactionResult;
  34. typedef ReactionResult (*ReactionFunc)( const void *msg_data,
  35. void *ctx );
  36. typedef struct {
  37. DirectLink link;
  38. ReactionFunc func;
  39. void *ctx;
  40. void *node_link;
  41. } Reaction;
  42. typedef struct {
  43. DirectLink link;
  44. int index;
  45. void *ctx;
  46. bool attached;
  47. } GlobalReaction;
  48. /*
  49. * Create a new reactor configured for the specified message data size.
  50. */
  51. FusionReactor *fusion_reactor_new ( int msg_size,
  52. const char *name,
  53. const FusionWorld *world );
  54. /*
  55. * Destroy the reactor.
  56. */
  57. DirectResult fusion_reactor_destroy ( FusionReactor *reactor );
  58. /*
  59. * Free the reactor.
  60. */
  61. DirectResult fusion_reactor_free ( FusionReactor *reactor );
  62. /*
  63. * Makes the reactor use the specified lock for managing global reactions.
  64. *
  65. * After creating the reactor a global default lock is set which is created
  66. * by Fusion once during initialization.
  67. *
  68. * To avoid dead locks caused by alternating lock orders of the global reaction
  69. * lock and another lock, the default lock is replaced by the other lock.
  70. */
  71. DirectResult fusion_reactor_set_lock ( FusionReactor *reactor,
  72. FusionSkirmish *skirmish );
  73. DirectResult fusion_reactor_set_lock_only( FusionReactor *reactor,
  74. FusionSkirmish *lock );
  75. /*
  76. * Attach a local reaction to the reactor (channel 0).
  77. */
  78. DirectResult fusion_reactor_attach ( FusionReactor *reactor,
  79. ReactionFunc func,
  80. void *ctx,
  81. Reaction *reaction );
  82. /*
  83. * Attach a local reaction to a specific reactor channel (0-1023).
  84. */
  85. DirectResult fusion_reactor_attach_channel( FusionReactor *reactor,
  86. int channel,
  87. ReactionFunc func,
  88. void *ctx,
  89. Reaction *reaction );
  90. /*
  91. * Detach an attached local reaction from the reactor.
  92. */
  93. DirectResult fusion_reactor_detach ( FusionReactor *reactor,
  94. Reaction *reaction );
  95. /*
  96. * Attach a global reaction to the reactor.
  97. *
  98. * It's always called directly, no matter which Fusionee calls fusion_reactor_dispatch().
  99. * Any data referenced by the reaction function has to be in shared memory, unless it uses a
  100. * mechanism to lookup a local counter part or representative, based on shared information.
  101. *
  102. * A global reaction is not defined directly as a function pointer, because that's always a
  103. * local address. Instead, it's specified by an index into a built in function table that
  104. * must be passed to fusion_reactor_dispatch() each time it is called.
  105. */
  106. DirectResult fusion_reactor_attach_global( FusionReactor *reactor,
  107. int index,
  108. void *ctx,
  109. GlobalReaction *reaction );
  110. /*
  111. * Detach an attached global reaction from the reactor.
  112. */
  113. DirectResult fusion_reactor_detach_global( FusionReactor *reactor,
  114. GlobalReaction *reaction );
  115. /*
  116. * Dispatch a message to any attached reaction (channel 0).
  117. *
  118. * Setting 'self' to false excludes the caller's local reactions.
  119. */
  120. DirectResult fusion_reactor_dispatch ( FusionReactor *reactor,
  121. const void *msg_data,
  122. bool self,
  123. const ReactionFunc *globals );
  124. /*
  125. * Dispatch a message to any attached reaction with a given size. Instead of
  126. * using the size defined by the reactor, the caller can specify the size of
  127. * the data.
  128. *
  129. * Setting 'self' to false excludes the caller's local reactions.
  130. */
  131. DirectResult fusion_reactor_sized_dispatch( FusionReactor *reactor,
  132. const void *msg_data,
  133. int msg_size,
  134. bool self,
  135. const ReactionFunc *globals );
  136. /*
  137. * Dispatch a message via a specific channel (0-1023).
  138. *
  139. * Setting 'self' to false excludes the caller's local reactions.
  140. */
  141. DirectResult fusion_reactor_dispatch_channel( FusionReactor *reactor,
  142. int channel,
  143. const void *msg_data,
  144. int msg_size,
  145. bool self,
  146. const ReactionFunc *globals );
  147. /*
  148. * Have the call executed when a dispatched message has been processed by all recipients.
  149. */
  150. DirectResult fusion_reactor_set_dispatch_callback( FusionReactor *reactor,
  151. FusionCall *call,
  152. void *call_ptr );
  153. /*
  154. * Change the name of the reactor (debug).
  155. */
  156. DirectResult fusion_reactor_set_name ( FusionReactor *reactor,
  157. const char *name );
  158. /*
  159. * Specify whether local message handlers (reactions) should be called directly.
  160. */
  161. DirectResult fusion_reactor_direct ( FusionReactor *reactor,
  162. bool direct );
  163. typedef enum {
  164. FUSION_REACTOR_PERMIT_NONE = 0x00000000,
  165. FUSION_REACTOR_PERMIT_ATTACH_DETACH = 0x00000001,
  166. FUSION_REACTOR_PERMIT_DISPATCH = 0x00000002,
  167. FUSION_REACTOR_PERMIT_ALL = 0x00000003,
  168. } FusionReactorPermissions;
  169. /*
  170. * Give permissions to another fusionee to use the reactor.
  171. */
  172. DirectResult fusion_reactor_add_permissions( FusionReactor *reactor,
  173. FusionID fusion_id,
  174. FusionReactorPermissions permissions );
  175. #endif