task.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #pragma once
  38. #define TASK_MAGIC 134
  39. #define TASK_ANTI_MAGIC 157
  40. struct Task
  41. {
  42. struct Options {
  43. bool allocated;
  44. bool send_in_use;
  45. bool is_known;
  46. bool is_running;
  47. bool was_reduced;
  48. bool is_paused;
  49. bool is_initialized;
  50. Options() :
  51. allocated(true),
  52. send_in_use(false),
  53. is_known(false),
  54. is_running(false),
  55. was_reduced(false),
  56. is_paused(false),
  57. is_initialized(true)
  58. { }
  59. } options;
  60. enum gearman_task_kind_t type;
  61. enum gearman_task_state_t state;
  62. uint32_t magic_;
  63. uint32_t created_id;
  64. uint32_t numerator;
  65. uint32_t denominator;
  66. uint32_t client_count;
  67. gearman_client_st *client;
  68. gearman_task_st *next;
  69. gearman_task_st *prev;
  70. void *context;
  71. gearman_connection_st *con;
  72. gearman_packet_st *recv;
  73. gearman_packet_st send;
  74. struct gearman_actions_t func;
  75. gearman_return_t result_rc;
  76. struct gearman_result_st *result_ptr;
  77. gearman_task_st* _shell;
  78. char job_handle[GEARMAN_JOB_HANDLE_SIZE];
  79. size_t unique_length;
  80. char unique[GEARMAN_MAX_UNIQUE_SIZE];
  81. gearman_task_st* shell()
  82. {
  83. return _shell;
  84. }
  85. Task(gearman_client_st& client_, gearman_task_st* shell_) :
  86. type(GEARMAN_TASK_KIND_ADD_TASK),
  87. state(GEARMAN_TASK_STATE_NEW),
  88. magic_(TASK_MAGIC),
  89. created_id(0),
  90. numerator(0),
  91. denominator(0),
  92. client_count(0),
  93. client(&client_),
  94. next(NULL),
  95. prev(NULL),
  96. context(NULL),
  97. con(NULL),
  98. recv(NULL),
  99. func(client_.actions),
  100. result_rc(GEARMAN_UNKNOWN_STATE),
  101. result_ptr(NULL),
  102. _shell(shell_),
  103. unique_length(0)
  104. {
  105. job_handle[0]= 0;
  106. unique[0]= 0;
  107. // Add the task to the client
  108. {
  109. if (client_.task_list)
  110. {
  111. client_.task_list->impl()->prev= shell_;
  112. }
  113. next= client_.task_list;
  114. prev= NULL;
  115. client_.task_list= shell_;
  116. client_.task_count++;
  117. }
  118. shell_->impl(this);
  119. }
  120. };