(main): Fix loop printing cleanup output.
[kopensolaris-gnu/glibc.git] / linuxthreads / queue.h
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
15 /* Waiting queues */
16
17 /* Waiting queues are represented by lists of thread descriptors
18    linked through their p_nextwaiting field.  The lists are kept
19    sorted by decreasing priority, and then decreasing waiting time. */
20
21 static inline void enqueue(pthread_descr * q, pthread_descr th)
22 {
23   int prio = th->p_priority;
24   ASSERT(th->p_nextwaiting == NULL);
25   for (; *q != NULL; q = &((*q)->p_nextwaiting)) {
26     if (prio > (*q)->p_priority) {
27       th->p_nextwaiting = *q;
28       *q = th;
29       return;
30     }
31   }
32   *q = th;
33 }
34
35 static inline pthread_descr dequeue(pthread_descr * q)
36 {
37   pthread_descr th;
38   th = *q;
39   if (th != NULL) {
40     *q = th->p_nextwaiting;
41     th->p_nextwaiting = NULL;
42   }
43   return th;
44 }
45
46 static inline int remove_from_queue(pthread_descr * q, pthread_descr th)
47 {
48   for (; *q != NULL; q = &((*q)->p_nextwaiting)) {
49     if (*q == th) {
50       *q = th->p_nextwaiting;
51       th->p_nextwaiting = NULL;
52       return 1;
53     }
54   }
55   return 0;
56 }
57
58 static inline int queue_is_empty(pthread_descr * q)
59 {
60     return *q == NULL;
61 }