thread.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 Thread Definitions
  41. */
  42. #include "gear_config.h"
  43. #include "libgearman-server/common.h"
  44. #include <libgearman/command.h>
  45. #include "libgearman/strcommand.h"
  46. #ifdef __cplusplus
  47. # include <cassert>
  48. # include <cerrno>
  49. #else
  50. # include <assert.h>
  51. # include <errno.h>
  52. #endif
  53. /*
  54. * Private declarations
  55. */
  56. /**
  57. * @addtogroup gearman_server_private Private Server Functions
  58. * @ingroup gearman_server
  59. * @{
  60. */
  61. /**
  62. * Try reading packets for a connection.
  63. */
  64. static gearmand_error_t _thread_packet_read(gearman_server_con_st *con);
  65. /**
  66. * Flush outgoing packets for a connection.
  67. */
  68. static gearmand_error_t _thread_packet_flush(gearman_server_con_st *con);
  69. /**
  70. * Start processing thread for the server.
  71. */
  72. static gearmand_error_t _proc_thread_start(gearman_server_st *server);
  73. /**
  74. * Kill processing thread for the server.
  75. */
  76. static void _proc_thread_kill(gearman_server_st *server);
  77. /** @} */
  78. /*
  79. * Public definitions
  80. */
  81. bool gearman_server_thread_init(gearman_server_st *server,
  82. gearman_server_thread_st *thread,
  83. gearman_log_server_fn *log_function,
  84. gearmand_thread_st *context,
  85. gearmand_event_watch_fn *event_watch)
  86. {
  87. assert(server);
  88. assert(thread);
  89. if (server->thread_count == 1)
  90. {
  91. /* The server is going to be multi-threaded, start processing thread. */
  92. if (_proc_thread_start(server) != GEARMAND_SUCCESS)
  93. {
  94. return false;
  95. }
  96. }
  97. thread->con_count= 0;
  98. thread->io_count= 0;
  99. thread->proc_count= 0;
  100. thread->to_be_freed_count= 0;
  101. thread->free_con_count= 0;
  102. thread->free_packet_count= 0;
  103. thread->log_fn= log_function;
  104. thread->log_context= context;
  105. thread->run_fn= NULL;
  106. thread->run_fn_arg= NULL;
  107. thread->con_list= NULL;
  108. thread->io_list= NULL;
  109. thread->proc_list= NULL;
  110. thread->free_con_list= NULL;
  111. thread->free_packet_list= NULL;
  112. thread->to_be_freed_list= NULL;
  113. int error;
  114. if ((error= pthread_mutex_init(&(thread->lock), NULL)))
  115. {
  116. gearmand_perror(error, "pthread_mutex_init");
  117. return false;
  118. }
  119. GEARMAND_LIST__ADD(server->thread, thread);
  120. thread->gearman= &(thread->gearmand_connection_list_static);
  121. thread->gearman->init(event_watch, NULL);
  122. return true;
  123. }
  124. void gearman_server_thread_free(gearman_server_thread_st *thread)
  125. {
  126. _proc_thread_kill(Server);
  127. while (thread->con_list != NULL)
  128. {
  129. gearman_server_con_free(thread->con_list);
  130. }
  131. while (thread->free_con_list != NULL)
  132. {
  133. gearman_server_con_st *con= thread->free_con_list;
  134. thread->free_con_list= con->next;
  135. delete con;
  136. }
  137. while (thread->free_packet_list != NULL)
  138. {
  139. gearman_server_packet_st *packet= thread->free_packet_list;
  140. thread->free_packet_list= packet->next;
  141. destroy_gearman_server_packet_st(packet);
  142. }
  143. if (thread->gearman != NULL)
  144. {
  145. thread->gearman->list_free();
  146. }
  147. pthread_mutex_destroy(&(thread->lock));
  148. GEARMAND_LIST__DEL(Server->thread, thread);
  149. }
  150. gearmand_con_st *
  151. gearman_server_thread_run(gearman_server_thread_st *thread,
  152. gearmand_error_t *ret_ptr)
  153. {
  154. /* If we are multi-threaded, we may have packets to flush or connections that
  155. should start reading again. */
  156. if (Server->flags.threaded)
  157. {
  158. gearman_server_con_st *server_con;
  159. while ((server_con= gearman_server_con_to_be_freed_next(thread)) != NULL)
  160. {
  161. if (server_con->is_dead && server_con->proc_removed)
  162. gearman_server_con_free(server_con);
  163. else
  164. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "con %p isn't dead %d or proc removed %d, but is in to_be_freed_list",
  165. server_con, server_con->is_dead, server_con->proc_removed);
  166. }
  167. while ((server_con= gearman_server_con_io_next(thread)) != NULL)
  168. {
  169. if (server_con->is_dead)
  170. {
  171. gearman_server_con_attempt_free(server_con);
  172. continue;
  173. }
  174. if (server_con->ret != GEARMAND_SUCCESS)
  175. {
  176. *ret_ptr= server_con->ret;
  177. return gearman_server_con_data(server_con);
  178. }
  179. /* See if any outgoing packets were queued. */
  180. *ret_ptr= _thread_packet_flush(server_con);
  181. if (*ret_ptr != GEARMAND_SUCCESS && *ret_ptr != GEARMAND_IO_WAIT)
  182. {
  183. return gearman_server_con_data(server_con);
  184. }
  185. }
  186. }
  187. /* Check for new activity on connections. */
  188. {
  189. gearman_server_con_st *server_con;
  190. while ((server_con= gearmand_ready(thread->gearman)))
  191. {
  192. /* Try to read new packets. */
  193. if (server_con->con.revents & POLLIN)
  194. {
  195. *ret_ptr= _thread_packet_read(server_con);
  196. if (*ret_ptr != GEARMAND_SUCCESS && *ret_ptr != GEARMAND_IO_WAIT)
  197. return gearman_server_con_data(server_con);
  198. }
  199. /* Flush existing outgoing packets. */
  200. if (server_con->con.revents & POLLOUT)
  201. {
  202. *ret_ptr= _thread_packet_flush(server_con);
  203. if (*ret_ptr != GEARMAND_SUCCESS && *ret_ptr != GEARMAND_IO_WAIT)
  204. {
  205. return gearman_server_con_data(server_con);
  206. }
  207. }
  208. }
  209. }
  210. /* Start flushing new outgoing packets if we are single threaded. */
  211. if (! (Server->flags.threaded))
  212. {
  213. gearman_server_con_st *server_con;
  214. while ((server_con= gearman_server_con_io_next(thread)))
  215. {
  216. *ret_ptr= _thread_packet_flush(server_con);
  217. if (*ret_ptr != GEARMAND_SUCCESS && *ret_ptr != GEARMAND_IO_WAIT)
  218. {
  219. return gearman_server_con_data(server_con);
  220. }
  221. }
  222. }
  223. /* Check for the two shutdown modes. */
  224. if (Server->shutdown)
  225. {
  226. *ret_ptr= GEARMAND_SHUTDOWN;
  227. }
  228. else if (Server->shutdown_graceful)
  229. {
  230. if (Server->job_count == 0)
  231. {
  232. *ret_ptr= GEARMAND_SHUTDOWN;
  233. }
  234. else
  235. {
  236. *ret_ptr= GEARMAND_SHUTDOWN_GRACEFUL;
  237. }
  238. }
  239. else
  240. {
  241. *ret_ptr= GEARMAND_SUCCESS;
  242. }
  243. return NULL;
  244. }
  245. /*
  246. * Private definitions
  247. */
  248. static gearmand_error_t _thread_packet_read(gearman_server_con_st *con)
  249. {
  250. while (1)
  251. {
  252. if (con->packet == NULL)
  253. {
  254. if (! (con->packet= gearman_server_packet_create(con->thread, true)))
  255. {
  256. return GEARMAND_MEMORY_ALLOCATION_FAILURE;
  257. }
  258. }
  259. gearmand_error_t ret;
  260. if (gearmand_failed(ret= gearman_io_recv(con, true)))
  261. {
  262. if (ret == GEARMAND_IO_WAIT)
  263. {
  264. break;
  265. }
  266. gearman_server_packet_free(con->packet, con->thread, true);
  267. con->packet= NULL;
  268. return ret;
  269. }
  270. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  271. "Received %s",
  272. gearmand_strcommand(&con->packet->packet));
  273. /* We read a complete packet. */
  274. if (Server->flags.threaded)
  275. {
  276. /* Multi-threaded, queue for the processing thread to run. */
  277. gearman_server_proc_packet_add(con, con->packet);
  278. con->packet= NULL;
  279. }
  280. else
  281. {
  282. /* Single threaded, run the command here. */
  283. gearmand_error_t rc= gearman_server_run_command(con, &(con->packet->packet));
  284. gearmand_packet_free(&(con->packet->packet));
  285. gearman_server_packet_free(con->packet, con->thread, true);
  286. con->packet= NULL;
  287. if (gearmand_failed(rc))
  288. {
  289. return rc;
  290. }
  291. }
  292. }
  293. return GEARMAND_SUCCESS;
  294. }
  295. static gearmand_error_t _thread_packet_flush(gearman_server_con_st *con)
  296. {
  297. /* Check to see if we've already tried to avoid excessive system calls. */
  298. if (con->con.events & POLLOUT)
  299. {
  300. return GEARMAND_IO_WAIT;
  301. }
  302. while (con->io_packet_list)
  303. {
  304. gearmand_error_t ret= gearman_io_send(con, &(con->io_packet_list->packet),
  305. con->io_packet_list->next == NULL ? true : false);
  306. if (gearmand_failed(ret))
  307. {
  308. return ret;
  309. }
  310. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  311. "Sent %s",
  312. gearman_strcommand(con->io_packet_list->packet.command));
  313. gearman_server_io_packet_remove(con);
  314. }
  315. /* Clear the POLLOUT flag. */
  316. return gearmand_io_set_events(con, POLLIN);
  317. }
  318. static gearmand_error_t _proc_thread_start(gearman_server_st *server)
  319. {
  320. int error;
  321. if ((error= pthread_mutex_init(&(server->proc_lock), NULL)))
  322. {
  323. return gearmand_perror(error, "pthread_mutex_init");
  324. }
  325. if ((error= pthread_cond_init(&(server->proc_cond), NULL)))
  326. {
  327. return gearmand_perror(error, "pthread_cond_init");
  328. }
  329. pthread_attr_t attr;
  330. if ((error= pthread_attr_init(&attr)))
  331. {
  332. return gearmand_perror(error, "pthread_attr_init");
  333. }
  334. if ((error= pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)))
  335. {
  336. (void) pthread_attr_destroy(&attr);
  337. return gearmand_perror(error, "pthread_attr_setscope");
  338. }
  339. if ((error= pthread_create(&(server->proc_id), &attr, _proc, server)))
  340. {
  341. (void) pthread_attr_destroy(&attr);
  342. return gearmand_perror(error, "pthread_create");
  343. }
  344. if ((error= pthread_attr_destroy(&attr)))
  345. {
  346. gearmand_perror(error, "pthread_create");
  347. }
  348. server->flags.threaded= true;
  349. return GEARMAND_SUCCESS;
  350. }
  351. static void _proc_thread_kill(gearman_server_st *server)
  352. {
  353. if (! (server->flags.threaded) || server->proc_shutdown)
  354. {
  355. return;
  356. }
  357. server->proc_shutdown= true;
  358. /* Signal proc thread to shutdown. */
  359. int error;
  360. if ((error= pthread_mutex_lock(&(server->proc_lock))) == 0)
  361. {
  362. if ((error= pthread_cond_signal(&(server->proc_cond))))
  363. {
  364. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_cond_signal");
  365. }
  366. if ((error= pthread_mutex_unlock(&(server->proc_lock))))
  367. {
  368. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock");
  369. }
  370. }
  371. else
  372. {
  373. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock");
  374. }
  375. /* Wait for the proc thread to exit and then cleanup. */
  376. if ((error= pthread_join(server->proc_id, NULL)))
  377. {
  378. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_join");
  379. }
  380. if ((error= pthread_cond_destroy(&(server->proc_cond))))
  381. {
  382. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_cond_destroy");
  383. }
  384. if ((error= pthread_mutex_destroy(&(server->proc_lock))))
  385. {
  386. gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_destroy");
  387. }
  388. }