/contrib/bind9/lib/isc/app_api.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 136 lines · 85 code · 35 blank · 16 comment · 11 complexity · d2c62df8aa639fb23343a5cc669468ad MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $Id: app_api.c,v 1.5 2009/09/02 23:48:02 tbox Exp $ */
  17. #include <config.h>
  18. #include <unistd.h>
  19. #include <isc/app.h>
  20. #include <isc/magic.h>
  21. #include <isc/mutex.h>
  22. #include <isc/once.h>
  23. #include <isc/util.h>
  24. static isc_mutex_t createlock;
  25. static isc_once_t once = ISC_ONCE_INIT;
  26. static isc_appctxcreatefunc_t appctx_createfunc = NULL;
  27. #define ISCAPI_APPMETHODS_VALID(m) ISC_MAGIC_VALID(m, ISCAPI_APPMETHODS_MAGIC)
  28. static void
  29. initialize(void) {
  30. RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
  31. }
  32. isc_result_t
  33. isc_app_register(isc_appctxcreatefunc_t createfunc) {
  34. isc_result_t result = ISC_R_SUCCESS;
  35. RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
  36. LOCK(&createlock);
  37. if (appctx_createfunc == NULL)
  38. appctx_createfunc = createfunc;
  39. else
  40. result = ISC_R_EXISTS;
  41. UNLOCK(&createlock);
  42. return (result);
  43. }
  44. isc_result_t
  45. isc_appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
  46. isc_result_t result;
  47. LOCK(&createlock);
  48. REQUIRE(appctx_createfunc != NULL);
  49. result = (*appctx_createfunc)(mctx, ctxp);
  50. UNLOCK(&createlock);
  51. return (result);
  52. }
  53. void
  54. isc_appctx_destroy(isc_appctx_t **ctxp) {
  55. REQUIRE(ctxp != NULL && ISCAPI_APPCTX_VALID(*ctxp));
  56. (*ctxp)->methods->ctxdestroy(ctxp);
  57. ENSURE(*ctxp == NULL);
  58. }
  59. isc_result_t
  60. isc_app_ctxstart(isc_appctx_t *ctx) {
  61. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  62. return (ctx->methods->ctxstart(ctx));
  63. }
  64. isc_result_t
  65. isc_app_ctxrun(isc_appctx_t *ctx) {
  66. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  67. return (ctx->methods->ctxrun(ctx));
  68. }
  69. isc_result_t
  70. isc_app_ctxsuspend(isc_appctx_t *ctx) {
  71. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  72. return (ctx->methods->ctxsuspend(ctx));
  73. }
  74. isc_result_t
  75. isc_app_ctxshutdown(isc_appctx_t *ctx) {
  76. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  77. return (ctx->methods->ctxshutdown(ctx));
  78. }
  79. void
  80. isc_app_ctxfinish(isc_appctx_t *ctx) {
  81. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  82. ctx->methods->ctxfinish(ctx);
  83. }
  84. void
  85. isc_appctx_settaskmgr(isc_appctx_t *ctx, isc_taskmgr_t *taskmgr) {
  86. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  87. REQUIRE(taskmgr != NULL);
  88. ctx->methods->settaskmgr(ctx, taskmgr);
  89. }
  90. void
  91. isc_appctx_setsocketmgr(isc_appctx_t *ctx, isc_socketmgr_t *socketmgr) {
  92. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  93. REQUIRE(socketmgr != NULL);
  94. ctx->methods->setsocketmgr(ctx, socketmgr);
  95. }
  96. void
  97. isc_appctx_settimermgr(isc_appctx_t *ctx, isc_timermgr_t *timermgr) {
  98. REQUIRE(ISCAPI_APPCTX_VALID(ctx));
  99. REQUIRE(timermgr != NULL);
  100. ctx->methods->settimermgr(ctx, timermgr);
  101. }