File: tasktest.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 (122 lines) | stat: -rw-r--r-- 2,149 bytes parent folder | download | duplicates (11)
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
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 *
 * WvTask test program.
 */
#include "wvtask.h"
#include <unistd.h> // for sleep()

WvTask *ga, *gb;

WvTaskMan *gman;

void gentask(void *userdata)
{
    char *str = (char *)userdata;
    int count = 0, delay = 0;
    
    printf("Gentask starting %s\n", str);
    
    while (count < 3)
    {
	printf("%s count %d -- %p\n", str, ++count, &str);
	usleep(delay*1000);
	if (count % 2)
	{
	    if (gman->whoami() == ga)
	    {
		if (gb)
		{
		    printf("Doing gb:\n");
		    gman->run(*gb, 400);
		}
	    }
	    else
	    {
		if (ga)
		{
		    printf("Doing ga:\n");
		    gman->run(*ga, 400);
		}
	    }
	}
	
	delay = gman->yield();
    }
    
    printf("Gentask ending %s\n", str);
}


int main()
{
    WvTaskMan *man = WvTaskMan::get();
    
    gman = man;
    ga = man->start("atask", gentask, (void *)"a");
    gb = man->start("btask", gentask, (void *)"b");
    
    // simple test
    for (int x = 0; x < 10; x++)
    {
	printf("main1:\n");
	man->run(*ga, 400);
	printf("main2:\n");
	man->run(*gb, 400);
	
	// it's still running; can't recycle it yet!
	//gb->recycle();
	
	if (!gb->isrunning())
	    gb = man->start("bbtask", gentask, (void *)"bb");
	if (!ga->isrunning())
	    ga = man->start("aatask", gentask, (void *)"aa");
    }
    
    // finish the tasks
    while (ga->isrunning())
	man->run(*ga, 0);
    while (gb->isrunning())
	man->run(*gb, 0);
    
    ga->recycle();
    gb->recycle();
    ga = NULL;
    gb = NULL;
    
    // stress test
    WvTaskList tasks;
    for (int x = 1; x <= 20; x++)
    {
	printf("x == %d\n", x);
	for (int y = 1; y <= 10; y++)
	{
	    WvTask *t = man->start("stresstask", gentask,
				  (void *)"testy",
				  16384);
	    tasks.append(t, false);
	}
	
	WvTaskList::Iter i(tasks);
	for (i.rewind(); i.next(); )
	    man->run(i(), 10);
    }
    while (tasks.count())
    {
	WvTaskList::Iter i(tasks);
	for (i.rewind(); i.next(); )
	{
	    man->run(i(), 100);
	    if (!i().isrunning())
	    {
		i().recycle();
		i.unlink();
		i.rewind();
	    }
	}
    }
    
    man->unlink();
    return 0;
}