db_pool.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2005 iptel.org
  5. * Copyright (C) 2006-2007 iptelorg GmbH
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. /** \ingroup DB_API
  29. * @{
  30. */
  31. #include "db_pool.h"
  32. #include "../../dprint.h"
  33. #include <unistd.h>
  34. #include <string.h>
  35. SLIST_HEAD(db_pool_head, db_pool_entry);
  36. /* The global connection pool */
  37. struct db_pool_head db_pool = SLIST_HEAD_INITIALIZER(db_pool);
  38. int db_pool_entry_init(struct db_pool_entry *entry, void* free_func, db_uri_t* uri)
  39. {
  40. if (db_drv_init(&entry->drv_gen, free_func) < 0) return -1;
  41. SLIST_NEXT(entry, next) = NULL;
  42. entry->uri = uri;
  43. entry->ref = 1;
  44. return 0;
  45. }
  46. void db_pool_entry_free(struct db_pool_entry* entry)
  47. {
  48. db_drv_free(&entry->drv_gen);
  49. entry->uri = NULL;
  50. entry->ref = 0;
  51. }
  52. /*
  53. * Search the pool for a connection with
  54. * the URI equal to uri, NULL is returned
  55. * when no connection is found
  56. */
  57. struct db_pool_entry* db_pool_get(db_uri_t* uri)
  58. {
  59. db_pool_entry_t* ptr;
  60. SLIST_FOREACH(ptr, &db_pool, next) {
  61. if (db_uri_cmp(ptr->uri, uri)) {
  62. ptr->ref++;
  63. return ptr;
  64. }
  65. }
  66. return 0;
  67. }
  68. /*
  69. * Insert a new connection into the pool
  70. */
  71. void db_pool_put(db_pool_entry_t* entry)
  72. {
  73. SLIST_INSERT_HEAD(&db_pool, entry, next);
  74. }
  75. /*
  76. * Release connection from the pool, the function
  77. * would return 1 when if the connection is not
  78. * referenced anymore and thus can be closed and
  79. * deleted by the backend. The function returns
  80. * 0 if the connection should still be kept open
  81. * because some other module is still using it.
  82. * The function returns -1 if the connection is
  83. * not in the pool.
  84. */
  85. int db_pool_remove(db_pool_entry_t* entry)
  86. {
  87. if (!entry) return -2;
  88. if (entry->ref > 1) {
  89. /* There are still other users, just
  90. * decrease the reference count and return
  91. */
  92. DBG("db_pool_remove: Connection still kept in the pool\n");
  93. entry->ref--;
  94. return 0;
  95. }
  96. DBG("db_pool_remove: Removing connection from the pool\n");
  97. SLIST_REMOVE(&db_pool, entry, db_pool_entry, next);
  98. return 1;
  99. }
  100. /** @} */