2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * A way to enqueue a series of WvSubProc objects. See wvsubprocqueue.h.
7 #include "wvsubprocqueue.h"
12 WvSubProcQueue::WvSubProcQueue(unsigned _maxrunning
)
14 maxrunning
= _maxrunning
;
18 WvSubProcQueue::~WvSubProcQueue()
23 void WvSubProcQueue::add(void *cookie
, WvSubProc
*proc
)
26 assert(!proc
->running
);
29 // search for other enqueued objects with this cookie
30 EntList::Iter
i(waitq
);
31 for (i
.rewind(); i
.next(); )
33 if (i
->cookie
== cookie
)
35 // already enqueued; mark it as "redo" unless it's already
36 // the last one. That way we guarantee it'll still run
37 // in the future from now, and it'll come later than anything
38 // else in the queue, but it won't pointlessly run twice at
49 waitq
.append(new Ent(cookie
, proc
), true);
53 void WvSubProcQueue::add(void *cookie
,
54 const char *cmd
, const char * const *argv
)
56 WvSubProc
*p
= new WvSubProc
;
57 p
->preparev(cmd
, argv
);
62 bool WvSubProcQueue::cookie_running()
64 EntList::Iter
i(runq
);
65 for (i
.rewind(); i
.next(); )
72 int WvSubProcQueue::go()
76 //fprintf(stderr, "go: %d waiting, %d running\n",
77 // waitq.count(), runq.count());
79 // first we need to clean up any finished processes
81 EntList::Iter
i(runq
);
82 for (i
.rewind(); i
.next(); )
86 e
->proc
->wait(0, true);
87 if (!e
->proc
->running
)
91 // someone re-enqueued this task while it was
95 waitq
.append(e
, true);
103 while (!waitq
.isempty() && runq
.count() < maxrunning
)
105 EntList::Iter
i(waitq
);
106 for (i
.rewind(); i
.next(); )
108 // elements with cookies are "sync points" in the queue;
109 // they guarantee that everything before that point has
110 // finished running before they run, and don't let anything
111 // after them run until they've finished.
112 if (i
->cookie
&& !runq
.isempty())
114 if (cookie_running())
117 // jump it into the running queue, but be careful not to
118 // delete the object when removing!
121 runq
.append(e
, true);
122 e
->proc
->start_again();
129 assert(runq
.count() <= maxrunning
);
134 unsigned WvSubProcQueue::running() const
140 unsigned WvSubProcQueue::remaining() const
142 return runq
.count() + waitq
.count();
146 bool WvSubProcQueue::isempty() const
148 return runq
.isempty() && waitq
.isempty();
152 void WvSubProcQueue::finish()