apibroker.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /* ABAPI Broker */
  39. /* Pete Rowley */
  40. #include "stdio.h"
  41. #include "slap.h"
  42. #include "prlock.h"
  43. #include "prerror.h"
  44. #include "prcvar.h"
  45. #include "prio.h"
  46. static Slapi_Mutex *buffer_lock = 0;
  47. /* circular api buffer */
  48. typedef struct _THEABAPI
  49. {
  50. char *guid;
  51. void **api;
  52. struct _THEABAPI *next;
  53. struct _THEABAPI *prev;
  54. } ABAPI;
  55. typedef struct _API_FEATURES
  56. {
  57. int refcount;
  58. slapi_apib_callback_on_zero callback_on_zero;
  59. Slapi_Mutex *lock;
  60. } APIB_FEATURES;
  61. static ABAPI *head = NULL;
  62. static ABAPI **ABAPIBroker_FindInterface(char *guid);
  63. int slapi_apib_register(char *guid, void **api )
  64. {
  65. int ret = -1;
  66. ABAPI *item;
  67. if(buffer_lock == 0)
  68. {
  69. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  70. /* badness */
  71. return -1;
  72. }
  73. /* simple - we don't check for duplicates */
  74. item = (ABAPI*)slapi_ch_malloc(sizeof(ABAPI));
  75. if(item)
  76. {
  77. item->guid = guid;
  78. item->api = api;
  79. slapi_lock_mutex(buffer_lock);
  80. if(head == NULL)
  81. {
  82. head = item;
  83. head->next = head;
  84. head->prev = head;
  85. }
  86. else
  87. {
  88. item->next = head;
  89. item->prev = head->prev;
  90. head->prev = item;
  91. item->prev->next = item;
  92. }
  93. slapi_unlock_mutex(buffer_lock);
  94. ret = 0;
  95. }
  96. return ret;
  97. }
  98. int slapi_apib_unregister(char *guid)
  99. {
  100. int ret = -1;
  101. ABAPI **api;
  102. if(buffer_lock == 0)
  103. return ret;
  104. if(buffer_lock == 0)
  105. {
  106. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  107. /* badness */
  108. return -1;
  109. }
  110. slapi_lock_mutex(buffer_lock);
  111. if((api = ABAPIBroker_FindInterface(guid)) != NULL)
  112. {
  113. (*api)->prev->next = (*api)->next;
  114. (*api)->next->prev = (*api)->prev;
  115. if(*api == head)
  116. {
  117. head = (*api)->next;
  118. }
  119. if(*api == head) /* must be the last item, turn off the lights */
  120. head = 0;
  121. slapi_ch_free((void**)api);
  122. *api = 0;
  123. ret = 0;
  124. }
  125. slapi_unlock_mutex(buffer_lock);
  126. return ret;
  127. }
  128. int slapi_apib_get_interface(char *guid, void ***api)
  129. {
  130. int ret = -1;
  131. ABAPI **theapi;
  132. if(buffer_lock == 0)
  133. return ret;
  134. if(buffer_lock == 0)
  135. {
  136. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  137. /* badness */
  138. return -1;
  139. }
  140. slapi_lock_mutex(buffer_lock);
  141. if((theapi = ABAPIBroker_FindInterface(guid)) != NULL)
  142. {
  143. *api = (*theapi)->api;
  144. if((*api)[0])
  145. {
  146. slapi_apib_addref(*api);
  147. }
  148. ret = 0;
  149. }
  150. slapi_unlock_mutex(buffer_lock);
  151. return ret;
  152. }
  153. int slapi_apib_make_reference_counted(void **api, slapi_apib_callback_on_zero callback_on_zero)
  154. {
  155. int ret = -1;
  156. if(api[0] == 0)
  157. {
  158. api[0] = slapi_ch_malloc(sizeof(APIB_FEATURES));
  159. if(api[0])
  160. {
  161. ((APIB_FEATURES*)(api[0]))->lock = slapi_new_mutex();
  162. if(((APIB_FEATURES*)(api[0]))->lock)
  163. {
  164. ((APIB_FEATURES*)(api[0]))->refcount = 0; /* the ref count */
  165. ((APIB_FEATURES*)(api[0]))->callback_on_zero = callback_on_zero;
  166. ret = 0;
  167. }
  168. else
  169. slapi_ch_free(&(api[0]));
  170. }
  171. }
  172. return ret;
  173. }
  174. int slapi_apib_addref(void **api)
  175. {
  176. int ret;
  177. slapi_lock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  178. ret = ++(((APIB_FEATURES*)(api[0]))->refcount);
  179. slapi_unlock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  180. return ret;
  181. }
  182. int slapi_apib_release(void **api)
  183. {
  184. APIB_FEATURES *features;
  185. int ret;
  186. slapi_lock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  187. ret = --(((APIB_FEATURES*)(api[0]))->refcount);
  188. if(((APIB_FEATURES*)(api[0]))->refcount == 0 && ((APIB_FEATURES*)(api[0]))->callback_on_zero)
  189. {
  190. /* save our stuff for when it gets zapped */
  191. features = (APIB_FEATURES*)api[0];
  192. if(0==((APIB_FEATURES*)(api[0]))->callback_on_zero(api)) /* this should deregister the interface */
  193. {
  194. slapi_unlock_mutex(features->lock);
  195. slapi_destroy_mutex(features->lock);
  196. slapi_ch_free((void **)&features);
  197. }
  198. else
  199. slapi_unlock_mutex(features->lock);
  200. }
  201. else
  202. slapi_unlock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  203. return ret;
  204. }
  205. static ABAPI **ABAPIBroker_FindInterface(char *guid)
  206. {
  207. static ABAPI *api = 0; /* simple gut feeling optimization for constant calls on same api */
  208. ABAPI *start_api = api;
  209. if(!api) {
  210. start_api = api = head;
  211. }
  212. if(api)
  213. {
  214. do
  215. {
  216. if(0 == strcmp(guid, api->guid))
  217. {
  218. return &api;
  219. }
  220. api = api->next;
  221. }
  222. while(api != start_api);
  223. }
  224. return 0;
  225. }