function.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief Server function definitions
  41. */
  42. #include "gear_config.h"
  43. #include "libgearman-server/common.h"
  44. #include <cstring>
  45. #include <memory>
  46. /*
  47. * Public definitions
  48. */
  49. static uint32_t _server_function_hash(const char *name, size_t size)
  50. {
  51. const char *ptr= name;
  52. int32_t value= 0;
  53. while (size--)
  54. {
  55. value += (int32_t)*ptr++;
  56. value += (value << 10);
  57. value ^= (value >> 6);
  58. }
  59. value += (value << 3);
  60. value ^= (value >> 11);
  61. value += (value << 15);
  62. return (uint32_t)(value == 0 ? 1 : value);
  63. }
  64. #pragma GCC diagnostic push
  65. #ifndef __INTEL_COMPILER
  66. # pragma GCC diagnostic ignored "-Wold-style-cast"
  67. #endif
  68. static gearman_server_function_st* gearman_server_function_create(gearman_server_st *server,
  69. const char *function_name,
  70. size_t function_name_size,
  71. uint32_t function_key)
  72. {
  73. gearman_server_function_st* function= new (std::nothrow) gearman_server_function_st;
  74. if (function == NULL)
  75. {
  76. gearmand_merror("new gearman_server_function_st", gearman_server_function_st, 0);
  77. return NULL;
  78. }
  79. function->worker_count= 0;
  80. function->job_count= 0;
  81. function->job_total= 0;
  82. function->job_running= 0;
  83. memset(function->max_queue_size, GEARMAND_DEFAULT_MAX_QUEUE_SIZE, sizeof(uint32_t) * GEARMAN_JOB_PRIORITY_MAX);
  84. function->function_name= new char[function_name_size +1];
  85. if (function->function_name == NULL)
  86. {
  87. gearmand_merror("new[]", char, function_name_size +1);
  88. delete function;
  89. return NULL;
  90. }
  91. memcpy(function->function_name, function_name, function_name_size);
  92. function->function_name[function_name_size]= 0;
  93. function->function_name_size= function_name_size;
  94. function->worker_list= NULL;
  95. memset(function->job_list, 0,
  96. sizeof(gearman_server_job_st *) * GEARMAN_JOB_PRIORITY_MAX);
  97. memset(function->job_end, 0,
  98. sizeof(gearman_server_job_st *) * GEARMAN_JOB_PRIORITY_MAX);
  99. GEARMAND_HASH__ADD(server->function, function_key, function);
  100. return function;
  101. }
  102. gearman_server_function_st *
  103. gearman_server_function_get(gearman_server_st *server,
  104. const char *function_name,
  105. size_t function_name_size)
  106. {
  107. gearman_server_function_st *function;
  108. uint32_t function_hash = _server_function_hash(function_name, function_name_size) % GEARMAND_DEFAULT_HASH_SIZE;
  109. for (function= server->function_hash[function_hash]; function != NULL;
  110. function= function->next)
  111. {
  112. if (function->function_name_size == function_name_size and
  113. memcmp(function->function_name, function_name, function_name_size) == 0)
  114. {
  115. return function;
  116. }
  117. }
  118. return gearman_server_function_create(server, function_name, function_name_size, function_hash);
  119. }
  120. void gearman_server_function_free(gearman_server_st *server, gearman_server_function_st *function)
  121. {
  122. uint32_t function_key;
  123. function_key= _server_function_hash(function->function_name, function->function_name_size);
  124. function_key= function_key % GEARMAND_DEFAULT_HASH_SIZE;
  125. GEARMAND_HASH__DEL(server->function, function_key, function);
  126. delete [] function->function_name;
  127. delete function;
  128. }
  129. #pragma GCC diagnostic pop