~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/js/src/xpconnect/tests/js/old/threads.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
const LOOP_COUNT   = 10000;
 
3
const THREAD_COUNT = 10;
 
4
 
 
5
const nsIThread = Components.interfaces.nsIThread;
 
6
const ThreadContractID = "@mozilla.org/thread;1";
 
7
const Thread = new Components.Constructor(ThreadContractID, "nsIThread", "init");
 
8
 
 
9
var foo;
 
10
var bar;
 
11
 
 
12
function threadProc() {
 
13
    foo = this.id+1;
 
14
    bar[this.id] = {p : 0};
 
15
    var n, m;
 
16
    for(n in bar) {
 
17
        // Uncommenting the line below makes this a lot more likely to deadlock.
 
18
        for(m in bar[n]) {}
 
19
    } 
 
20
    // Commented this because I was thinking that the printf impl being called 
 
21
    // on so many thread might be contributing to some apparent memory 
 
22
    // corruption cases.
 
23
    // print("  printed from other thread "+this.id+". foo is: "+foo);
 
24
    for(n in this.__parent__) /* print(n) */;
 
25
}
 
26
 
 
27
function runThreads() {
 
28
    print("  printed from main thread. foo is "+foo);
 
29
 
 
30
    var threads = new Array(THREAD_COUNT);
 
31
    var i;
 
32
 
 
33
    for(i = 0; i < THREAD_COUNT; i++) {
 
34
        var runable = {run : threadProc, id : i};
 
35
        threads[i] = new Thread(runable, 0, 
 
36
                                nsIThread.PRIORITY_NORMAL,
 
37
                                nsIThread.SCOPE_GLOBAL,
 
38
                                nsIThread.STATE_JOINABLE);
 
39
    }
 
40
 
 
41
    print("  printed from main thread. foo is "+foo);
 
42
 
 
43
    // var main_thread = threads[0].currentThread;
 
44
 
 
45
    for(i = THREAD_COUNT-1; i >= 0; i--) {
 
46
        // main_thread.sleep(1);
 
47
        threads[i].join();
 
48
    }
 
49
 
 
50
    print("  printed from main thread. foo is "+foo);
 
51
}
 
52
 
 
53
var total_interval = 0;
 
54
 
 
55
for(i = 0; i < LOOP_COUNT; i++) {
 
56
    var start_time = new Date().getTime()/1000;
 
57
 
 
58
    foo = 0;
 
59
    bar = new Array(THREAD_COUNT);
 
60
    print("--- loop number "+i);
 
61
    runThreads();
 
62
 
 
63
    var end_time = new Date().getTime()/1000;
 
64
 
 
65
    var interval = parseInt(100*(end_time - start_time),10)/100;
 
66
    print("        Interval = "+interval+ " seconds.");
 
67
    // ignore first loop
 
68
    if(i) {
 
69
        total_interval += end_time - start_time;
 
70
        var average_interval = parseInt(100*(total_interval / i),10)/100
 
71
        print("Average Interval = "+average_interval+" seconds.");
 
72
    }
 
73
}    
 
74