aio_misc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* Handle general operations.
  2. Copyright (C) 1997-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <aio.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <pthreadP.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <sys/param.h>
  23. #include <sys/stat.h>
  24. #include <sys/time.h>
  25. #include <aio_misc.h>
  26. #if !PTHREAD_IN_LIBC
  27. /* The available function names differ outside of libc. (In libc, we
  28. need to use hidden aliases to avoid the PLT.) */
  29. # define __pread __libc_pread
  30. # define __pthread_attr_destroy pthread_attr_destroy
  31. # define __pthread_attr_init pthread_attr_init
  32. # define __pthread_attr_setdetachstate pthread_attr_setdetachstate
  33. # define __pthread_cond_signal pthread_cond_signal
  34. # define __pthread_cond_timedwait pthread_cond_timedwait
  35. # define __pthread_getschedparam pthread_getschedparam
  36. # define __pthread_setschedparam pthread_setschedparam
  37. # define __pwrite __libc_pwrite
  38. #endif
  39. #ifndef aio_create_helper_thread
  40. # define aio_create_helper_thread __aio_create_helper_thread
  41. extern inline int
  42. __aio_create_helper_thread (pthread_t *threadp, void *(*tf) (void *), void *arg)
  43. {
  44. pthread_attr_t attr;
  45. /* Make sure the thread is created detached. */
  46. __pthread_attr_init (&attr);
  47. __pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  48. int ret = __pthread_create (threadp, &attr, tf, arg);
  49. __pthread_attr_destroy (&attr);
  50. return ret;
  51. }
  52. #endif
  53. static void add_request_to_runlist (struct requestlist *newrequest);
  54. /* Pool of request list entries. */
  55. static struct requestlist **pool;
  56. /* Number of total and allocated pool entries. */
  57. static size_t pool_max_size;
  58. static size_t pool_size;
  59. /* We implement a two dimensional array but allocate each row separately.
  60. The macro below determines how many entries should be used per row.
  61. It should better be a power of two. */
  62. #define ENTRIES_PER_ROW 32
  63. /* How many rows we allocate at once. */
  64. #define ROWS_STEP 8
  65. /* List of available entries. */
  66. static struct requestlist *freelist;
  67. /* List of request waiting to be processed. */
  68. static struct requestlist *runlist;
  69. /* Structure list of all currently processed requests. */
  70. static struct requestlist *requests;
  71. /* Number of threads currently running. */
  72. static int nthreads;
  73. /* Number of threads waiting for work to arrive. */
  74. static int idle_thread_count;
  75. /* These are the values used to optimize the use of AIO. The user can
  76. overwrite them by using the `aio_init' function. */
  77. static struct aioinit optim =
  78. {
  79. 20, /* int aio_threads; Maximal number of threads. */
  80. 64, /* int aio_num; Number of expected simultaneous requests. */
  81. 0,
  82. 0,
  83. 0,
  84. 0,
  85. 1,
  86. 0
  87. };
  88. /* Since the list is global we need a mutex protecting it. */
  89. pthread_mutex_t __aio_requests_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  90. /* When you add a request to the list and there are idle threads present,
  91. you signal this condition variable. When a thread finishes work, it waits
  92. on this condition variable for a time before it actually exits. */
  93. pthread_cond_t __aio_new_request_notification = PTHREAD_COND_INITIALIZER;
  94. /* Functions to handle request list pool. */
  95. static struct requestlist *
  96. get_elem (void)
  97. {
  98. struct requestlist *result;
  99. if (freelist == NULL)
  100. {
  101. struct requestlist *new_row;
  102. int cnt;
  103. assert (sizeof (struct aiocb) == sizeof (struct aiocb64));
  104. if (pool_size + 1 >= pool_max_size)
  105. {
  106. size_t new_max_size = pool_max_size + ROWS_STEP;
  107. struct requestlist **new_tab;
  108. new_tab = (struct requestlist **)
  109. realloc (pool, new_max_size * sizeof (struct requestlist *));
  110. if (new_tab == NULL)
  111. return NULL;
  112. pool_max_size = new_max_size;
  113. pool = new_tab;
  114. }
  115. /* Allocate the new row. */
  116. cnt = pool_size == 0 ? optim.aio_num : ENTRIES_PER_ROW;
  117. new_row = (struct requestlist *) calloc (cnt,
  118. sizeof (struct requestlist));
  119. if (new_row == NULL)
  120. return NULL;
  121. pool[pool_size++] = new_row;
  122. /* Put all the new entries in the freelist. */
  123. do
  124. {
  125. new_row->next_prio = freelist;
  126. freelist = new_row++;
  127. }
  128. while (--cnt > 0);
  129. }
  130. result = freelist;
  131. freelist = freelist->next_prio;
  132. return result;
  133. }
  134. void
  135. __aio_free_request (struct requestlist *elem)
  136. {
  137. elem->running = no;
  138. elem->next_prio = freelist;
  139. freelist = elem;
  140. }
  141. struct requestlist *
  142. __aio_find_req (aiocb_union *elem)
  143. {
  144. struct requestlist *runp = requests;
  145. int fildes = elem->aiocb.aio_fildes;
  146. while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes)
  147. runp = runp->next_fd;
  148. if (runp != NULL)
  149. {
  150. if (runp->aiocbp->aiocb.aio_fildes != fildes)
  151. runp = NULL;
  152. else
  153. while (runp != NULL && runp->aiocbp != elem)
  154. runp = runp->next_prio;
  155. }
  156. return runp;
  157. }
  158. struct requestlist *
  159. __aio_find_req_fd (int fildes)
  160. {
  161. struct requestlist *runp = requests;
  162. while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes)
  163. runp = runp->next_fd;
  164. return (runp != NULL && runp->aiocbp->aiocb.aio_fildes == fildes
  165. ? runp : NULL);
  166. }
  167. void
  168. __aio_remove_request (struct requestlist *last, struct requestlist *req,
  169. int all)
  170. {
  171. assert (req->running == yes || req->running == queued
  172. || req->running == done);
  173. if (last != NULL)
  174. last->next_prio = all ? NULL : req->next_prio;
  175. else
  176. {
  177. if (all || req->next_prio == NULL)
  178. {
  179. if (req->last_fd != NULL)
  180. req->last_fd->next_fd = req->next_fd;
  181. else
  182. requests = req->next_fd;
  183. if (req->next_fd != NULL)
  184. req->next_fd->last_fd = req->last_fd;
  185. }
  186. else
  187. {
  188. if (req->last_fd != NULL)
  189. req->last_fd->next_fd = req->next_prio;
  190. else
  191. requests = req->next_prio;
  192. if (req->next_fd != NULL)
  193. req->next_fd->last_fd = req->next_prio;
  194. req->next_prio->last_fd = req->last_fd;
  195. req->next_prio->next_fd = req->next_fd;
  196. /* Mark this entry as runnable. */
  197. req->next_prio->running = yes;
  198. }
  199. if (req->running == yes)
  200. {
  201. struct requestlist *runp = runlist;
  202. last = NULL;
  203. while (runp != NULL)
  204. {
  205. if (runp == req)
  206. {
  207. if (last == NULL)
  208. runlist = runp->next_run;
  209. else
  210. last->next_run = runp->next_run;
  211. break;
  212. }
  213. last = runp;
  214. runp = runp->next_run;
  215. }
  216. }
  217. }
  218. }
  219. /* The thread handler. */
  220. static void *handle_fildes_io (void *arg);
  221. /* User optimization. */
  222. void
  223. __aio_init (const struct aioinit *init)
  224. {
  225. /* Get the mutex. */
  226. __pthread_mutex_lock (&__aio_requests_mutex);
  227. /* Only allow writing new values if the table is not yet allocated. */
  228. if (pool == NULL)
  229. {
  230. optim.aio_threads = init->aio_threads < 1 ? 1 : init->aio_threads;
  231. assert (powerof2 (ENTRIES_PER_ROW));
  232. optim.aio_num = (init->aio_num < ENTRIES_PER_ROW
  233. ? ENTRIES_PER_ROW
  234. : init->aio_num & ~(ENTRIES_PER_ROW - 1));
  235. }
  236. if (init->aio_idle_time != 0)
  237. optim.aio_idle_time = init->aio_idle_time;
  238. /* Release the mutex. */
  239. __pthread_mutex_unlock (&__aio_requests_mutex);
  240. }
  241. /* The main function of the async I/O handling. It enqueues requests
  242. and if necessary starts and handles threads. */
  243. struct requestlist *
  244. __aio_enqueue_request (aiocb_union *aiocbp, int operation)
  245. {
  246. int result = 0;
  247. int policy, prio;
  248. struct sched_param param;
  249. struct requestlist *last, *runp, *newp;
  250. int running = no;
  251. if (operation == LIO_SYNC || operation == LIO_DSYNC)
  252. aiocbp->aiocb.aio_reqprio = 0;
  253. else if (aiocbp->aiocb.aio_reqprio < 0
  254. #ifdef AIO_PRIO_DELTA_MAX
  255. || aiocbp->aiocb.aio_reqprio > AIO_PRIO_DELTA_MAX
  256. #endif
  257. )
  258. {
  259. /* Invalid priority value. */
  260. __set_errno (EINVAL);
  261. aiocbp->aiocb.__error_code = EINVAL;
  262. aiocbp->aiocb.__return_value = -1;
  263. return NULL;
  264. }
  265. /* Compute priority for this request. */
  266. __pthread_getschedparam (__pthread_self (), &policy, &param);
  267. prio = param.sched_priority - aiocbp->aiocb.aio_reqprio;
  268. /* Get the mutex. */
  269. __pthread_mutex_lock (&__aio_requests_mutex);
  270. last = NULL;
  271. runp = requests;
  272. /* First look whether the current file descriptor is currently
  273. worked with. */
  274. while (runp != NULL
  275. && runp->aiocbp->aiocb.aio_fildes < aiocbp->aiocb.aio_fildes)
  276. {
  277. last = runp;
  278. runp = runp->next_fd;
  279. }
  280. /* Get a new element for the waiting list. */
  281. newp = get_elem ();
  282. if (newp == NULL)
  283. {
  284. __pthread_mutex_unlock (&__aio_requests_mutex);
  285. __set_errno (EAGAIN);
  286. return NULL;
  287. }
  288. newp->aiocbp = aiocbp;
  289. newp->waiting = NULL;
  290. aiocbp->aiocb.__abs_prio = prio;
  291. aiocbp->aiocb.__policy = policy;
  292. aiocbp->aiocb.aio_lio_opcode = operation;
  293. aiocbp->aiocb.__error_code = EINPROGRESS;
  294. aiocbp->aiocb.__return_value = 0;
  295. if (runp != NULL
  296. && runp->aiocbp->aiocb.aio_fildes == aiocbp->aiocb.aio_fildes)
  297. {
  298. /* The current file descriptor is worked on. It makes no sense
  299. to start another thread since this new thread would fight
  300. with the running thread for the resources. But we also cannot
  301. say that the thread processing this descriptor shall immediately
  302. after finishing the current job process this request if there
  303. are other threads in the running queue which have a higher
  304. priority. */
  305. /* Simply enqueue it after the running one according to the
  306. priority. */
  307. last = NULL;
  308. while (runp->next_prio != NULL
  309. && runp->next_prio->aiocbp->aiocb.__abs_prio >= prio)
  310. {
  311. last = runp;
  312. runp = runp->next_prio;
  313. }
  314. newp->next_prio = runp->next_prio;
  315. runp->next_prio = newp;
  316. running = queued;
  317. }
  318. else
  319. {
  320. running = yes;
  321. /* Enqueue this request for a new descriptor. */
  322. if (last == NULL)
  323. {
  324. newp->last_fd = NULL;
  325. newp->next_fd = requests;
  326. if (requests != NULL)
  327. requests->last_fd = newp;
  328. requests = newp;
  329. }
  330. else
  331. {
  332. newp->next_fd = last->next_fd;
  333. newp->last_fd = last;
  334. last->next_fd = newp;
  335. if (newp->next_fd != NULL)
  336. newp->next_fd->last_fd = newp;
  337. }
  338. newp->next_prio = NULL;
  339. last = NULL;
  340. }
  341. if (running == yes)
  342. {
  343. /* We try to create a new thread for this file descriptor. The
  344. function which gets called will handle all available requests
  345. for this descriptor and when all are processed it will
  346. terminate.
  347. If no new thread can be created or if the specified limit of
  348. threads for AIO is reached we queue the request. */
  349. /* See if we need to and are able to create a thread. */
  350. if (nthreads < optim.aio_threads && idle_thread_count == 0)
  351. {
  352. pthread_t thid;
  353. running = newp->running = allocated;
  354. /* Now try to start a thread. */
  355. result = aio_create_helper_thread (&thid, handle_fildes_io, newp);
  356. if (result == 0)
  357. /* We managed to enqueue the request. All errors which can
  358. happen now can be recognized by calls to `aio_return' and
  359. `aio_error'. */
  360. ++nthreads;
  361. else
  362. {
  363. /* Reset the running flag. The new request is not running. */
  364. running = newp->running = yes;
  365. if (nthreads == 0)
  366. {
  367. /* We cannot create a thread in the moment and there is
  368. also no thread running. This is a problem. `errno' is
  369. set to EAGAIN if this is only a temporary problem. */
  370. __aio_remove_request (last, newp, 0);
  371. }
  372. else
  373. result = 0;
  374. }
  375. }
  376. }
  377. /* Enqueue the request in the run queue if it is not yet running. */
  378. if (running == yes && result == 0)
  379. {
  380. add_request_to_runlist (newp);
  381. /* If there is a thread waiting for work, then let it know that we
  382. have just given it something to do. */
  383. if (idle_thread_count > 0)
  384. __pthread_cond_signal (&__aio_new_request_notification);
  385. }
  386. if (result == 0)
  387. newp->running = running;
  388. else
  389. {
  390. /* Something went wrong. */
  391. __aio_free_request (newp);
  392. aiocbp->aiocb.__error_code = result;
  393. __set_errno (result);
  394. newp = NULL;
  395. }
  396. /* Release the mutex. */
  397. __pthread_mutex_unlock (&__aio_requests_mutex);
  398. return newp;
  399. }
  400. static void *
  401. handle_fildes_io (void *arg)
  402. {
  403. pthread_t self = __pthread_self ();
  404. struct sched_param param;
  405. struct requestlist *runp = (struct requestlist *) arg;
  406. aiocb_union *aiocbp;
  407. int policy;
  408. int fildes;
  409. __pthread_getschedparam (self, &policy, &param);
  410. do
  411. {
  412. /* If runp is NULL, then we were created to service the work queue
  413. in general, not to handle any particular request. In that case we
  414. skip the "do work" stuff on the first pass, and go directly to the
  415. "get work off the work queue" part of this loop, which is near the
  416. end. */
  417. if (runp == NULL)
  418. __pthread_mutex_lock (&__aio_requests_mutex);
  419. else
  420. {
  421. /* Hopefully this request is marked as running. */
  422. assert (runp->running == allocated);
  423. /* Update our variables. */
  424. aiocbp = runp->aiocbp;
  425. fildes = aiocbp->aiocb.aio_fildes;
  426. /* Change the priority to the requested value (if necessary). */
  427. if (aiocbp->aiocb.__abs_prio != param.sched_priority
  428. || aiocbp->aiocb.__policy != policy)
  429. {
  430. param.sched_priority = aiocbp->aiocb.__abs_prio;
  431. policy = aiocbp->aiocb.__policy;
  432. __pthread_setschedparam (self, policy, &param);
  433. }
  434. /* Process request pointed to by RUNP. We must not be disturbed
  435. by signals. */
  436. if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_READ)
  437. {
  438. if (sizeof (off_t) != sizeof (off64_t)
  439. && aiocbp->aiocb.aio_lio_opcode & 128)
  440. aiocbp->aiocb.__return_value =
  441. TEMP_FAILURE_RETRY (__pread64 (fildes, (void *)
  442. aiocbp->aiocb64.aio_buf,
  443. aiocbp->aiocb64.aio_nbytes,
  444. aiocbp->aiocb64.aio_offset));
  445. else
  446. aiocbp->aiocb.__return_value =
  447. TEMP_FAILURE_RETRY (__pread (fildes,
  448. (void *)
  449. aiocbp->aiocb.aio_buf,
  450. aiocbp->aiocb.aio_nbytes,
  451. aiocbp->aiocb.aio_offset));
  452. if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE)
  453. /* The Linux kernel is different from others. It returns
  454. ESPIPE if using pread on a socket. Other platforms
  455. simply ignore the offset parameter and behave like
  456. read. */
  457. aiocbp->aiocb.__return_value =
  458. TEMP_FAILURE_RETRY (read (fildes,
  459. (void *) aiocbp->aiocb64.aio_buf,
  460. aiocbp->aiocb64.aio_nbytes));
  461. }
  462. else if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_WRITE)
  463. {
  464. if (sizeof (off_t) != sizeof (off64_t)
  465. && aiocbp->aiocb.aio_lio_opcode & 128)
  466. aiocbp->aiocb.__return_value =
  467. TEMP_FAILURE_RETRY (__pwrite64 (fildes, (const void *)
  468. aiocbp->aiocb64.aio_buf,
  469. aiocbp->aiocb64.aio_nbytes,
  470. aiocbp->aiocb64.aio_offset));
  471. else
  472. aiocbp->aiocb.__return_value =
  473. TEMP_FAILURE_RETRY (__pwrite (fildes, (const void *)
  474. aiocbp->aiocb.aio_buf,
  475. aiocbp->aiocb.aio_nbytes,
  476. aiocbp->aiocb.aio_offset));
  477. if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE)
  478. /* The Linux kernel is different from others. It returns
  479. ESPIPE if using pwrite on a socket. Other platforms
  480. simply ignore the offset parameter and behave like
  481. write. */
  482. aiocbp->aiocb.__return_value =
  483. TEMP_FAILURE_RETRY (write (fildes,
  484. (void *) aiocbp->aiocb64.aio_buf,
  485. aiocbp->aiocb64.aio_nbytes));
  486. }
  487. else if (aiocbp->aiocb.aio_lio_opcode == LIO_DSYNC)
  488. aiocbp->aiocb.__return_value =
  489. TEMP_FAILURE_RETRY (fdatasync (fildes));
  490. else if (aiocbp->aiocb.aio_lio_opcode == LIO_SYNC)
  491. aiocbp->aiocb.__return_value =
  492. TEMP_FAILURE_RETRY (fsync (fildes));
  493. else
  494. {
  495. /* This is an invalid opcode. */
  496. aiocbp->aiocb.__return_value = -1;
  497. __set_errno (EINVAL);
  498. }
  499. /* Get the mutex. */
  500. __pthread_mutex_lock (&__aio_requests_mutex);
  501. if (aiocbp->aiocb.__return_value == -1)
  502. aiocbp->aiocb.__error_code = errno;
  503. else
  504. aiocbp->aiocb.__error_code = 0;
  505. /* Send the signal to notify about finished processing of the
  506. request. */
  507. __aio_notify (runp);
  508. /* For debugging purposes we reset the running flag of the
  509. finished request. */
  510. assert (runp->running == allocated);
  511. runp->running = done;
  512. /* Now dequeue the current request. */
  513. __aio_remove_request (NULL, runp, 0);
  514. if (runp->next_prio != NULL)
  515. add_request_to_runlist (runp->next_prio);
  516. /* Free the old element. */
  517. __aio_free_request (runp);
  518. }
  519. runp = runlist;
  520. /* If the runlist is empty, then we sleep for a while, waiting for
  521. something to arrive in it. */
  522. if (runp == NULL && optim.aio_idle_time >= 0)
  523. {
  524. struct timespec now;
  525. struct timespec wakeup_time;
  526. ++idle_thread_count;
  527. __clock_gettime (CLOCK_REALTIME, &now);
  528. wakeup_time.tv_sec = now.tv_sec + optim.aio_idle_time;
  529. wakeup_time.tv_nsec = now.tv_nsec;
  530. if (wakeup_time.tv_nsec >= 1000000000)
  531. {
  532. wakeup_time.tv_nsec -= 1000000000;
  533. ++wakeup_time.tv_sec;
  534. }
  535. __pthread_cond_timedwait (&__aio_new_request_notification,
  536. &__aio_requests_mutex,
  537. &wakeup_time);
  538. --idle_thread_count;
  539. runp = runlist;
  540. }
  541. if (runp == NULL)
  542. --nthreads;
  543. else
  544. {
  545. assert (runp->running == yes);
  546. runp->running = allocated;
  547. runlist = runp->next_run;
  548. /* If we have a request to process, and there's still another in
  549. the run list, then we need to either wake up or create a new
  550. thread to service the request that is still in the run list. */
  551. if (runlist != NULL)
  552. {
  553. /* There are at least two items in the work queue to work on.
  554. If there are other idle threads, then we should wake them
  555. up for these other work elements; otherwise, we should try
  556. to create a new thread. */
  557. if (idle_thread_count > 0)
  558. __pthread_cond_signal (&__aio_new_request_notification);
  559. else if (nthreads < optim.aio_threads)
  560. {
  561. pthread_t thid;
  562. pthread_attr_t attr;
  563. /* Make sure the thread is created detached. */
  564. __pthread_attr_init (&attr);
  565. __pthread_attr_setdetachstate (&attr,
  566. PTHREAD_CREATE_DETACHED);
  567. /* Now try to start a thread. If we fail, no big deal,
  568. because we know that there is at least one thread (us)
  569. that is working on AIO operations. */
  570. if (__pthread_create (&thid, &attr, handle_fildes_io, NULL)
  571. == 0)
  572. ++nthreads;
  573. }
  574. }
  575. }
  576. /* Release the mutex. */
  577. __pthread_mutex_unlock (&__aio_requests_mutex);
  578. }
  579. while (runp != NULL);
  580. return NULL;
  581. }
  582. /* Free allocated resources. */
  583. #if !PTHREAD_IN_LIBC
  584. __attribute__ ((__destructor__)) static
  585. #endif
  586. void
  587. __aio_freemem (void)
  588. {
  589. size_t row;
  590. for (row = 0; row < pool_size; ++row)
  591. free (pool[row]);
  592. free (pool);
  593. }
  594. /* Add newrequest to the runlist. The __abs_prio flag of newrequest must
  595. be correctly set to do this. Also, you had better set newrequest's
  596. "running" flag to "yes" before you release your lock or you'll throw an
  597. assertion. */
  598. static void
  599. add_request_to_runlist (struct requestlist *newrequest)
  600. {
  601. int prio = newrequest->aiocbp->aiocb.__abs_prio;
  602. struct requestlist *runp;
  603. if (runlist == NULL || runlist->aiocbp->aiocb.__abs_prio < prio)
  604. {
  605. newrequest->next_run = runlist;
  606. runlist = newrequest;
  607. }
  608. else
  609. {
  610. runp = runlist;
  611. while (runp->next_run != NULL
  612. && runp->next_run->aiocbp->aiocb.__abs_prio >= prio)
  613. runp = runp->next_run;
  614. newrequest->next_run = runp->next_run;
  615. runp->next_run = newrequest;
  616. }
  617. }
  618. #if PTHREAD_IN_LIBC
  619. versioned_symbol (libc, __aio_init, aio_init, GLIBC_2_34);
  620. # if OTHER_SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_34)
  621. compat_symbol (librt, __aio_init, aio_init, GLIBC_2_1);
  622. # endif
  623. #else /* !PTHREAD_IN_LIBC */
  624. weak_alias (__aio_init, aio_init)
  625. #endif /* !PTHREAD_IN_LIBC */