queue.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. for (; *q != NULL; q = &((*q)->p_nextwaiting)) {
  22. if (prio > (*q)->p_priority) {
  23. th->p_nextwaiting = *q;
  24. *q = th;
  25. return;
  26. }
  27. }
  28. *q = th;
  29. }
  30. static __inline__ pthread_descr dequeue(pthread_descr * q)
  31. {
  32. pthread_descr th;
  33. th = *q;
  34. if (th != NULL) {
  35. *q = th->p_nextwaiting;
  36. th->p_nextwaiting = NULL;
  37. }
  38. return th;
  39. }
  40. static __inline__ int remove_from_queue(pthread_descr * q, pthread_descr th)
  41. {
  42. for (; *q != NULL; q = &((*q)->p_nextwaiting)) {
  43. if (*q == th) {
  44. *q = th->p_nextwaiting;
  45. th->p_nextwaiting = NULL;
  46. return 1;
  47. }
  48. }
  49. return 0;
  50. }
  51. static __inline__ int queue_is_empty(pthread_descr * q)
  52. {
  53. return *q == NULL;
  54. }