File: wvsubprocqueue.cc

package info (click to toggle)
wvstreams 4.6.1-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,420 kB
  • sloc: cpp: 64,196; ansic: 4,154; sh: 4,025; makefile: 546; perl: 402
file content (160 lines) | stat: -rw-r--r-- 3,121 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 * 
 * A way to enqueue a series of WvSubProc objects.  See wvsubprocqueue.h.
 */
#include "wvsubprocqueue.h"
#include <unistd.h>
#include <assert.h>


WvSubProcQueue::WvSubProcQueue(unsigned _maxrunning)
{
    maxrunning = _maxrunning;
}


WvSubProcQueue::~WvSubProcQueue()
{
}


void WvSubProcQueue::add(void *cookie, WvSubProc *proc)
{
    assert(proc);
    assert(!proc->running);
    if (cookie)
    {
	// search for other enqueued objects with this cookie
	EntList::Iter i(waitq);
	for (i.rewind(); i.next(); )
	{
	    if (i->cookie == cookie)
	    {
		// already enqueued; mark it as "redo" unless it's already
		// the last one.  That way we guarantee it'll still run
		// in the future from now, and it'll come later than anything
		// else in the queue, but it won't pointlessly run twice at
		// the end.
		Ent *e = i.ptr();
		if (i.next())
		    e->redo = true;
		delete proc;
		return;
	    }
	}
    }
    
    waitq.append(new Ent(cookie, proc), true);
}


void WvSubProcQueue::add(void *cookie,
			 const char *cmd, const char * const *argv)
{
    WvSubProc *p = new WvSubProc;
    p->preparev(cmd, argv);
    add(cookie, p);
}


bool WvSubProcQueue::cookie_running()
{
    EntList::Iter i(runq);
    for (i.rewind(); i.next(); )
	if (i->cookie)
	    return true;
    return false;
}


int WvSubProcQueue::go()
{
    int started = 0;
    
    //fprintf(stderr, "go: %d waiting, %d running\n",
    //	waitq.count(), runq.count());
    
    // first we need to clean up any finished processes
    {
	EntList::Iter i(runq);
	for (i.rewind(); i.next(); )
	{
	    Ent *e = i.ptr();
	    
	    e->proc->wait(0, true);
	    if (!e->proc->running)
	    {
		if (e->redo)
		{
		    // someone re-enqueued this task while it was
		    // waiting/running
		    e->redo = false;
		    i.xunlink(false);
		    waitq.append(e, true);
		}
		else
		    i.xunlink();
	    }
	}
    }
    
    while (!waitq.isempty() && runq.count() < maxrunning)
    {
	EntList::Iter i(waitq);
	for (i.rewind(); i.next(); )
	{
	    // elements with cookies are "sync points" in the queue;
	    // they guarantee that everything before that point has
	    // finished running before they run, and don't let anything
	    // after them run until they've finished.
	    if (i->cookie && !runq.isempty())
		goto out;
	    if (cookie_running())
		goto out;
	    
	    // jump it into the running queue, but be careful not to
	    // delete the object when removing!
	    Ent *e = i.ptr();
	    i.xunlink(false);
	    runq.append(e, true);
	    e->proc->start_again();
	    started++;
	    break;
	}
    }
    
out:
    assert(runq.count() <= maxrunning);
    return started;
}


unsigned WvSubProcQueue::running() const
{
    return runq.count();
}


unsigned WvSubProcQueue::remaining() const
{
    return runq.count() + waitq.count();
}


bool WvSubProcQueue::isempty() const
{
    return runq.isempty() && waitq.isempty();
}


void WvSubProcQueue::finish()
{
    while (!isempty())
    {
	go();
	if (!isempty())
	    usleep(100*1000);
    }
}