queue.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Waiting queues */
  15. /* Waiting queues are represented by lists of thread descriptors
  16. linked through their p_nextwaiting field. The lists are kept
  17. sorted by decreasing priority, and then decreasing waiting time. */
  18. static __inline__ void enqueue(pthread_descr * q, pthread_descr th)
  19. {
  20. int prio = th->p_priority;
  21. ASSERT(th->p_nextwaiting == NULL);
  22. for (; *q != NULL; q = &((*q)->p_nextwaiting)) {
  23. if (prio > (*q)->p_priority) {
  24. th->p_nextwaiting = *q;
  25. *q = th;
  26. return;
  27. }
  28. }
  29. *q = th;
  30. }
  31. static __inline__ pthread_descr dequeue(pthread_descr * q)
  32. {
  33. pthread_descr th;
  34. th = *q;
  35. if (th != NULL) {
  36. *q = th->p_nextwaiting;
  37. th->p_nextwaiting = NULL;
  38. }
  39. return th;
  40. }
  41. static __inline__ int remove_from_queue(pthread_descr * q, pthread_descr th)
  42. {
  43. for (; *q != NULL; q = &((*q)->p_nextwaiting)) {
  44. if (*q == th) {
  45. *q = th->p_nextwaiting;
  46. th->p_nextwaiting = NULL;
  47. return 1;
  48. }
  49. }
  50. return 0;
  51. }
  52. static __inline__ int queue_is_empty(pthread_descr * q)
  53. {
  54. return *q == NULL;
  55. }