client.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <libgearman-server/common.h>
  13. #include <assert.h>
  14. /*
  15. * Public definitions
  16. */
  17. gearman_server_client_st *
  18. gearman_server_client_add(gearman_server_con_st *con)
  19. {
  20. gearman_server_client_st *client;
  21. if (Server->free_client_count > 0)
  22. {
  23. client= Server->free_client_list;
  24. GEARMAN_LIST_DEL(Server->free_client, client, con_)
  25. }
  26. else
  27. {
  28. client= static_cast<gearman_server_client_st *>(malloc(sizeof(gearman_server_client_st)));
  29. if (client == NULL)
  30. {
  31. gearmand_log_error("gearman_server_client_create", "malloc");
  32. return NULL;
  33. }
  34. }
  35. assert(client);
  36. if (!client)
  37. {
  38. gearmand_error("In gearman_server_client_add() we failed to either allocorate of find a free one");
  39. return NULL;
  40. }
  41. client->con= con;
  42. GEARMAN_LIST_ADD(con->client, client, con_)
  43. client->job= NULL;
  44. client->job_next= NULL;
  45. client->job_prev= NULL;
  46. return client;
  47. }
  48. void gearman_server_client_free(gearman_server_client_st *client)
  49. {
  50. if (not client)
  51. return;
  52. GEARMAN_LIST_DEL(client->con->client, client, con_)
  53. if (client->job)
  54. {
  55. GEARMAN_LIST_DEL(client->job->client, client, job_)
  56. /* If this was a foreground job and is now abandoned, mark to not run. */
  57. if (client->job->client_list == NULL)
  58. {
  59. client->job->ignore_job= true;
  60. client->job->job_queued= false;
  61. }
  62. }
  63. if (Server->free_client_count < GEARMAN_MAX_FREE_SERVER_CLIENT)
  64. {
  65. GEARMAN_LIST_ADD(Server->free_client, client, con_)
  66. }
  67. else
  68. {
  69. gearmand_crazy("free");
  70. free(client);
  71. }
  72. }