ref.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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__REF_H__
  24. #define __FUSION__REF_H__
  25. #include <pthread.h>
  26. #include <fusion/types.h>
  27. #include <fusion/call.h>
  28. #include <fusion/lock.h>
  29. typedef union {
  30. /* multi app */
  31. struct {
  32. int id;
  33. const FusionWorldShared *shared;
  34. FusionID creator;
  35. /* builtin impl */
  36. struct {
  37. int local;
  38. int global;
  39. FusionSkirmish lock;
  40. FusionCall *call;
  41. int call_arg;
  42. } builtin;
  43. } multi;
  44. /* single app */
  45. struct {
  46. int refs;
  47. pthread_cond_t cond;
  48. pthread_mutex_t lock;
  49. bool destroyed;
  50. int locked;
  51. FusionCall *call;
  52. int call_arg;
  53. } single;
  54. } FusionRef;
  55. /*
  56. * Initialize.
  57. */
  58. DirectResult fusion_ref_init (FusionRef *ref,
  59. const char *name,
  60. const FusionWorld *world);
  61. DirectResult fusion_ref_set_name (FusionRef *ref,
  62. const char *name);
  63. /*
  64. * Lock, increase, unlock.
  65. */
  66. DirectResult fusion_ref_up (FusionRef *ref, bool global);
  67. /*
  68. * Lock, decrease, unlock.
  69. */
  70. DirectResult fusion_ref_down (FusionRef *ref, bool global);
  71. /*
  72. * Catch reference
  73. */
  74. DirectResult fusion_ref_catch (FusionRef *ref);
  75. /*
  76. * Throw reference
  77. */
  78. DirectResult fusion_ref_throw (FusionRef *ref, FusionID catcher);
  79. /*
  80. * Get the current reference count. Meant for debugging only.
  81. * This value is not reliable, because no locking will be performed
  82. * and the value may change after or even while returning it.
  83. */
  84. DirectResult fusion_ref_stat (FusionRef *ref, int *refs);
  85. /*
  86. * Wait for zero and lock.
  87. */
  88. DirectResult fusion_ref_zero_lock (FusionRef *ref);
  89. /*
  90. * Check for zero and lock if true.
  91. */
  92. DirectResult fusion_ref_zero_trylock (FusionRef *ref);
  93. /*
  94. * Unlock the counter.
  95. * Only to be called after successful zero_lock or zero_trylock.
  96. */
  97. DirectResult fusion_ref_unlock (FusionRef *ref);
  98. /*
  99. * Have the call executed when reference counter reaches zero.
  100. */
  101. DirectResult fusion_ref_watch (FusionRef *ref,
  102. FusionCall *call,
  103. int call_arg);
  104. /*
  105. * Inherit local reference count from another reference.
  106. *
  107. * The local count of the other reference (and its inherited references) is added to this reference.
  108. */
  109. DirectResult fusion_ref_inherit (FusionRef *ref,
  110. FusionRef *from);
  111. /*
  112. * Deinitialize.
  113. * Can be called after successful zero_lock or zero_trylock
  114. * so that waiting fusion_ref_up calls return with DR_DESTROYED.
  115. */
  116. DirectResult fusion_ref_destroy (FusionRef *ref);
  117. typedef enum {
  118. FUSION_REF_PERMIT_NONE = 0x00000000,
  119. FUSION_REF_PERMIT_REF_UNREF_LOCAL = 0x00000001,
  120. FUSION_REF_PERMIT_REF_UNREF_GLOBAL = 0x00000002,
  121. FUSION_REF_PERMIT_ZERO_LOCK_UNLOCK = 0x00000004,
  122. FUSION_REF_PERMIT_WATCH = 0x00000008,
  123. FUSION_REF_PERMIT_INHERIT = 0x00000010,
  124. FUSION_REF_PERMIT_DESTROY = 0x00000020,
  125. FUSION_REF_PERMIT_CATCH = 0x00000040,
  126. FUSION_REF_PERMIT_THROW = 0x00000080,
  127. FUSION_REF_PERMIT_ALL = 0x000000FF,
  128. } FusionRefPermissions;
  129. /*
  130. * Give permissions to another fusionee to use the reference.
  131. */
  132. DirectResult fusion_ref_add_permissions( FusionRef *ref,
  133. FusionID fusion_id,
  134. FusionRefPermissions permissions );
  135. #endif