gai_misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* Copyright (C) 2001-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdlib.h>
  19. #include <sys/time.h>
  20. #include <gai_misc.h>
  21. #ifndef gai_create_helper_thread
  22. # define gai_create_helper_thread __gai_create_helper_thread
  23. extern inline int
  24. __gai_create_helper_thread (pthread_t *threadp, void *(*tf) (void *),
  25. void *arg)
  26. {
  27. pthread_attr_t attr;
  28. /* Make sure the thread is created detached. */
  29. pthread_attr_init (&attr);
  30. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  31. int ret = pthread_create (threadp, &attr, tf, arg);
  32. (void) pthread_attr_destroy (&attr);
  33. return ret;
  34. }
  35. #endif
  36. /* Pool of request list entries. */
  37. static struct requestlist **pool;
  38. /* Number of total and allocated pool entries. */
  39. static size_t pool_max_size;
  40. static size_t pool_size;
  41. /* We implement a two dimensional array but allocate each row separately.
  42. The macro below determines how many entries should be used per row.
  43. It should better be a power of two. */
  44. #define ENTRIES_PER_ROW 32
  45. /* How many rows we allocate at once. */
  46. #define ROWS_STEP 8
  47. /* List of available entries. */
  48. static struct requestlist *freelist;
  49. /* Structure list of all currently processed requests. */
  50. static struct requestlist *requests;
  51. static struct requestlist *requests_tail;
  52. /* Number of threads currently running. */
  53. static int nthreads;
  54. /* Number of threads waiting for work to arrive. */
  55. static int idle_thread_count;
  56. /* These are the values used for optimization. We will probably
  57. create a funcion to set these values. */
  58. static struct gaiinit optim =
  59. {
  60. 20, /* int gai_threads; Maximal number of threads. */
  61. 64, /* int gai_num; Number of expected simultanious requests. */
  62. 0,
  63. 0,
  64. 0,
  65. 0,
  66. 1,
  67. 0
  68. };
  69. /* Since the list is global we need a mutex protecting it. */
  70. pthread_mutex_t __gai_requests_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  71. /* When you add a request to the list and there are idle threads present,
  72. you signal this condition variable. When a thread finishes work, it waits
  73. on this condition variable for a time before it actually exits. */
  74. pthread_cond_t __gai_new_request_notification = PTHREAD_COND_INITIALIZER;
  75. /* Functions to handle request list pool. */
  76. static struct requestlist *
  77. get_elem (void)
  78. {
  79. struct requestlist *result;
  80. if (freelist == NULL)
  81. {
  82. struct requestlist *new_row;
  83. int cnt;
  84. if (pool_size + 1 >= pool_max_size)
  85. {
  86. size_t new_max_size = pool_max_size + ROWS_STEP;
  87. struct requestlist **new_tab;
  88. new_tab = (struct requestlist **)
  89. realloc (pool, new_max_size * sizeof (struct requestlist *));
  90. if (new_tab == NULL)
  91. return NULL;
  92. pool_max_size = new_max_size;
  93. pool = new_tab;
  94. }
  95. /* Allocate the new row. */
  96. cnt = pool_size == 0 ? optim.gai_num : ENTRIES_PER_ROW;
  97. new_row = (struct requestlist *) calloc (cnt,
  98. sizeof (struct requestlist));
  99. if (new_row == NULL)
  100. return NULL;
  101. pool[pool_size++] = new_row;
  102. /* Put all the new entries in the freelist. */
  103. do
  104. {
  105. new_row->next = freelist;
  106. freelist = new_row++;
  107. }
  108. while (--cnt > 0);
  109. }
  110. result = freelist;
  111. freelist = freelist->next;
  112. return result;
  113. }
  114. struct requestlist *
  115. __gai_find_request (const struct gaicb *gaicbp)
  116. {
  117. struct requestlist *runp;
  118. runp = requests;
  119. while (runp != NULL)
  120. if (runp->gaicbp == gaicbp)
  121. return runp;
  122. else
  123. runp = runp->next;
  124. return NULL;
  125. }
  126. int
  127. __gai_remove_request (struct gaicb *gaicbp)
  128. {
  129. struct requestlist *runp;
  130. struct requestlist *lastp;
  131. runp = requests;
  132. lastp = NULL;
  133. while (runp != NULL)
  134. if (runp->gaicbp == gaicbp)
  135. break;
  136. else
  137. {
  138. lastp = runp;
  139. runp = runp->next;
  140. }
  141. if (runp == NULL)
  142. /* Not known. */
  143. return -1;
  144. if (runp->running != 0)
  145. /* Currently handled. */
  146. return 1;
  147. /* Dequeue the request. */
  148. if (lastp == NULL)
  149. requests = runp->next;
  150. else
  151. lastp->next = runp->next;
  152. if (runp == requests_tail)
  153. requests_tail = lastp;
  154. return 0;
  155. }
  156. /* The thread handler. */
  157. static void *handle_requests (void *arg);
  158. /* The main function of the async I/O handling. It enqueues requests
  159. and if necessary starts and handles threads. */
  160. struct requestlist *
  161. __gai_enqueue_request (struct gaicb *gaicbp)
  162. {
  163. struct requestlist *newp;
  164. struct requestlist *lastp;
  165. /* Get the mutex. */
  166. pthread_mutex_lock (&__gai_requests_mutex);
  167. /* Get a new element for the waiting list. */
  168. newp = get_elem ();
  169. if (newp == NULL)
  170. {
  171. pthread_mutex_unlock (&__gai_requests_mutex);
  172. __set_errno (EAGAIN);
  173. return NULL;
  174. }
  175. newp->running = 0;
  176. newp->gaicbp = gaicbp;
  177. newp->waiting = NULL;
  178. newp->next = NULL;
  179. lastp = requests_tail;
  180. if (requests_tail == NULL)
  181. requests = requests_tail = newp;
  182. else
  183. {
  184. requests_tail->next = newp;
  185. requests_tail = newp;
  186. }
  187. gaicbp->__return = EAI_INPROGRESS;
  188. /* See if we need to and are able to create a thread. */
  189. if (nthreads < optim.gai_threads && idle_thread_count == 0)
  190. {
  191. pthread_t thid;
  192. newp->running = 1;
  193. /* Now try to start a thread. */
  194. if (gai_create_helper_thread (&thid, handle_requests, newp) == 0)
  195. /* We managed to enqueue the request. All errors which can
  196. happen now can be recognized by calls to `gai_error'. */
  197. ++nthreads;
  198. else
  199. {
  200. if (nthreads == 0)
  201. {
  202. /* We cannot create a thread in the moment and there is
  203. also no thread running. This is a problem. `errno' is
  204. set to EAGAIN if this is only a temporary problem. */
  205. assert (requests == newp || lastp->next == newp);
  206. if (lastp != NULL)
  207. lastp->next = NULL;
  208. else
  209. requests = NULL;
  210. requests_tail = lastp;
  211. newp->next = freelist;
  212. freelist = newp;
  213. newp = NULL;
  214. }
  215. else
  216. /* We are not handling the request after all. */
  217. newp->running = 0;
  218. }
  219. }
  220. /* Enqueue the request in the request queue. */
  221. if (newp != NULL)
  222. {
  223. /* If there is a thread waiting for work, then let it know that we
  224. have just given it something to do. */
  225. if (idle_thread_count > 0)
  226. pthread_cond_signal (&__gai_new_request_notification);
  227. }
  228. /* Release the mutex. */
  229. pthread_mutex_unlock (&__gai_requests_mutex);
  230. return newp;
  231. }
  232. static void *
  233. __attribute__ ((noreturn))
  234. handle_requests (void *arg)
  235. {
  236. struct requestlist *runp = (struct requestlist *) arg;
  237. do
  238. {
  239. /* If runp is NULL, then we were created to service the work queue
  240. in general, not to handle any particular request. In that case we
  241. skip the "do work" stuff on the first pass, and go directly to the
  242. "get work off the work queue" part of this loop, which is near the
  243. end. */
  244. if (runp == NULL)
  245. pthread_mutex_lock (&__gai_requests_mutex);
  246. else
  247. {
  248. /* Make the request. */
  249. struct gaicb *req = runp->gaicbp;
  250. struct requestlist *srchp;
  251. struct requestlist *lastp;
  252. req->__return = getaddrinfo (req->ar_name, req->ar_service,
  253. req->ar_request, &req->ar_result);
  254. /* Get the mutex. */
  255. pthread_mutex_lock (&__gai_requests_mutex);
  256. /* Send the signal to notify about finished processing of the
  257. request. */
  258. __gai_notify (runp);
  259. /* Now dequeue the current request. */
  260. lastp = NULL;
  261. srchp = requests;
  262. while (srchp != runp)
  263. {
  264. lastp = srchp;
  265. srchp = srchp->next;
  266. }
  267. assert (runp->running == 1);
  268. if (requests_tail == runp)
  269. requests_tail = lastp;
  270. if (lastp == NULL)
  271. requests = requests->next;
  272. else
  273. lastp->next = runp->next;
  274. /* Free the old element. */
  275. runp->next = freelist;
  276. freelist = runp;
  277. }
  278. runp = requests;
  279. while (runp != NULL && runp->running != 0)
  280. runp = runp->next;
  281. /* If the runlist is empty, then we sleep for a while, waiting for
  282. something to arrive in it. */
  283. if (runp == NULL && optim.gai_idle_time >= 0)
  284. {
  285. struct timeval now;
  286. struct timespec wakeup_time;
  287. ++idle_thread_count;
  288. gettimeofday (&now, NULL);
  289. wakeup_time.tv_sec = now.tv_sec + optim.gai_idle_time;
  290. wakeup_time.tv_nsec = now.tv_usec * 1000;
  291. if (wakeup_time.tv_nsec >= 1000000000)
  292. {
  293. wakeup_time.tv_nsec -= 1000000000;
  294. ++wakeup_time.tv_sec;
  295. }
  296. pthread_cond_timedwait (&__gai_new_request_notification,
  297. &__gai_requests_mutex, &wakeup_time);
  298. --idle_thread_count;
  299. runp = requests;
  300. while (runp != NULL && runp->running != 0)
  301. runp = runp->next;
  302. }
  303. if (runp == NULL)
  304. --nthreads;
  305. else
  306. {
  307. /* Mark the request as being worked on. */
  308. assert (runp->running == 0);
  309. runp->running = 1;
  310. /* If we have a request to process, and there's still another in
  311. the run list, then we need to either wake up or create a new
  312. thread to service the request that is still in the run list. */
  313. if (requests != NULL)
  314. {
  315. /* There are at least two items in the work queue to work on.
  316. If there are other idle threads, then we should wake them
  317. up for these other work elements; otherwise, we should try
  318. to create a new thread. */
  319. if (idle_thread_count > 0)
  320. pthread_cond_signal (&__gai_new_request_notification);
  321. else if (nthreads < optim.gai_threads)
  322. {
  323. pthread_t thid;
  324. pthread_attr_t attr;
  325. /* Make sure the thread is created detached. */
  326. pthread_attr_init (&attr);
  327. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  328. /* Now try to start a thread. If we fail, no big deal,
  329. because we know that there is at least one thread (us)
  330. that is working on lookup operations. */
  331. if (pthread_create (&thid, &attr, handle_requests, NULL)
  332. == 0)
  333. ++nthreads;
  334. }
  335. }
  336. }
  337. /* Release the mutex. */
  338. pthread_mutex_unlock (&__gai_requests_mutex);
  339. }
  340. while (runp != NULL);
  341. pthread_exit (NULL);
  342. }
  343. /* Free allocated resources. */
  344. libc_freeres_fn (free_res)
  345. {
  346. size_t row;
  347. for (row = 0; row < pool_max_size; ++row)
  348. free (pool[row]);
  349. free (pool);
  350. }