client.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Gearman server and library
  2. * Copyright (C) 2008 Brian Aker, Eric Day
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. /**
  9. * @file
  10. * @brief Server client definitions
  11. */
  12. #include <config.h>
  13. #include <libgearman-server/common.h>
  14. #include <cassert>
  15. /*
  16. * Public definitions
  17. */
  18. gearman_server_client_st *
  19. gearman_server_client_add(gearman_server_con_st *con)
  20. {
  21. gearman_server_client_st *client;
  22. if (Server->free_client_count > 0)
  23. {
  24. client= Server->free_client_list;
  25. GEARMAN_LIST_DEL(Server->free_client, client, con_)
  26. }
  27. else
  28. {
  29. client= static_cast<gearman_server_client_st *>(malloc(sizeof(gearman_server_client_st)));
  30. if (not client)
  31. {
  32. gearmand_merror("malloc", gearman_server_client_st, 0);
  33. return NULL;
  34. }
  35. }
  36. assert(client);
  37. if (not client)
  38. {
  39. gearmand_error("In gearman_server_client_add() we failed to either allocorate of find a free one");
  40. return NULL;
  41. }
  42. client->con= con;
  43. GEARMAN_LIST_ADD(con->client, client, con_)
  44. client->job= NULL;
  45. client->job_next= NULL;
  46. client->job_prev= NULL;
  47. return client;
  48. }
  49. void gearman_server_client_free(gearman_server_client_st *client)
  50. {
  51. if (not client)
  52. return;
  53. GEARMAN_LIST_DEL(client->con->client, client, con_)
  54. if (client->job)
  55. {
  56. GEARMAN_LIST_DEL(client->job->client, client, job_)
  57. /* If this was a foreground job and is now abandoned, mark to not run. */
  58. if (client->job->client_list == NULL)
  59. {
  60. client->job->ignore_job= true;
  61. client->job->job_queued= false;
  62. }
  63. }
  64. if (Server->free_client_count < GEARMAN_MAX_FREE_SERVER_CLIENT)
  65. {
  66. GEARMAN_LIST_ADD(Server->free_client, client, con_)
  67. }
  68. else
  69. {
  70. gearmand_debug("free");
  71. free(client);
  72. }
  73. }