apibroker.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* ABAPI Broker */
  42. /* Pete Rowley */
  43. #include "stdio.h"
  44. #include "slap.h"
  45. #include "prlock.h"
  46. #include "prerror.h"
  47. #include "prcvar.h"
  48. #include "prio.h"
  49. static Slapi_Mutex *buffer_lock = 0;
  50. /* circular api buffer */
  51. typedef struct _THEABAPI
  52. {
  53. char *guid;
  54. void **api;
  55. struct _THEABAPI *next;
  56. struct _THEABAPI *prev;
  57. } ABAPI;
  58. typedef struct _API_FEATURES
  59. {
  60. int refcount;
  61. slapi_apib_callback_on_zero callback_on_zero;
  62. Slapi_Mutex *lock;
  63. } APIB_FEATURES;
  64. static ABAPI *head = NULL;
  65. static ABAPI **ABAPIBroker_FindInterface(char *guid);
  66. int slapi_apib_register(char *guid, void **api )
  67. {
  68. int ret = -1;
  69. ABAPI *item;
  70. if(buffer_lock == 0)
  71. {
  72. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  73. /* badness */
  74. return -1;
  75. }
  76. /* simple - we don't check for duplicates */
  77. item = (ABAPI*)slapi_ch_malloc(sizeof(ABAPI));
  78. if(item)
  79. {
  80. item->guid = guid;
  81. item->api = api;
  82. slapi_lock_mutex(buffer_lock);
  83. if(head == NULL)
  84. {
  85. head = item;
  86. head->next = head;
  87. head->prev = head;
  88. }
  89. else
  90. {
  91. item->next = head;
  92. item->prev = head->prev;
  93. head->prev = item;
  94. item->prev->next = item;
  95. }
  96. slapi_unlock_mutex(buffer_lock);
  97. ret = 0;
  98. }
  99. return ret;
  100. }
  101. int slapi_apib_unregister(char *guid)
  102. {
  103. int ret = -1;
  104. ABAPI **api;
  105. if(buffer_lock == 0)
  106. return ret;
  107. if(buffer_lock == 0)
  108. {
  109. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  110. /* badness */
  111. return -1;
  112. }
  113. slapi_lock_mutex(buffer_lock);
  114. if((api = ABAPIBroker_FindInterface(guid)) != NULL)
  115. {
  116. (*api)->prev->next = (*api)->next;
  117. (*api)->next->prev = (*api)->prev;
  118. if(*api == head)
  119. {
  120. head = (*api)->next;
  121. }
  122. if(*api == head) /* must be the last item, turn off the lights */
  123. head = 0;
  124. slapi_ch_free((void**)api);
  125. *api = 0;
  126. ret = 0;
  127. }
  128. slapi_unlock_mutex(buffer_lock);
  129. return ret;
  130. }
  131. int slapi_apib_get_interface(char *guid, void ***api)
  132. {
  133. int ret = -1;
  134. ABAPI **theapi;
  135. if(buffer_lock == 0)
  136. return ret;
  137. if(buffer_lock == 0)
  138. {
  139. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  140. /* badness */
  141. return -1;
  142. }
  143. slapi_lock_mutex(buffer_lock);
  144. if((theapi = ABAPIBroker_FindInterface(guid)) != NULL)
  145. {
  146. *api = (*theapi)->api;
  147. if((*api)[0])
  148. {
  149. slapi_apib_addref(*api);
  150. }
  151. ret = 0;
  152. }
  153. slapi_unlock_mutex(buffer_lock);
  154. return ret;
  155. }
  156. int slapi_apib_make_reference_counted(void **api, slapi_apib_callback_on_zero callback_on_zero)
  157. {
  158. int ret = -1;
  159. if(api[0] == 0)
  160. {
  161. api[0] = slapi_ch_malloc(sizeof(APIB_FEATURES));
  162. if(api[0])
  163. {
  164. ((APIB_FEATURES*)(api[0]))->lock = slapi_new_mutex();
  165. if(((APIB_FEATURES*)(api[0]))->lock)
  166. {
  167. ((APIB_FEATURES*)(api[0]))->refcount = 0; /* the ref count */
  168. ((APIB_FEATURES*)(api[0]))->callback_on_zero = callback_on_zero;
  169. ret = 0;
  170. }
  171. else
  172. slapi_ch_free(&(api[0]));
  173. }
  174. }
  175. return ret;
  176. }
  177. int slapi_apib_addref(void **api)
  178. {
  179. int ret;
  180. slapi_lock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  181. ret = ++(((APIB_FEATURES*)(api[0]))->refcount);
  182. slapi_unlock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  183. return ret;
  184. }
  185. int slapi_apib_release(void **api)
  186. {
  187. APIB_FEATURES *features;
  188. int ret;
  189. slapi_lock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  190. ret = --(((APIB_FEATURES*)(api[0]))->refcount);
  191. if(((APIB_FEATURES*)(api[0]))->refcount == 0 && ((APIB_FEATURES*)(api[0]))->callback_on_zero)
  192. {
  193. /* save our stuff for when it gets zapped */
  194. features = (APIB_FEATURES*)api[0];
  195. if(0==((APIB_FEATURES*)(api[0]))->callback_on_zero(api)) /* this should deregister the interface */
  196. {
  197. slapi_unlock_mutex(features->lock);
  198. slapi_destroy_mutex(features->lock);
  199. slapi_ch_free((void **)&features);
  200. }
  201. else
  202. slapi_unlock_mutex(features->lock);
  203. }
  204. else
  205. slapi_unlock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  206. return ret;
  207. }
  208. static ABAPI **ABAPIBroker_FindInterface(char *guid)
  209. {
  210. static ABAPI *api = 0; /* simple gut feeling optimization for constant calls on same api */
  211. ABAPI *start_api = api;
  212. if(!api) {
  213. start_api = api = head;
  214. }
  215. if(api)
  216. {
  217. do
  218. {
  219. if(0 == strcmp(guid, api->guid))
  220. {
  221. return &api;
  222. }
  223. api = api->next;
  224. }
  225. while(api != start_api);
  226. }
  227. return 0;
  228. }