* src/threads/lock.cpp (sable_flc_waiting): Slight optimization, avoiding
[cacao.git] / tests / threads / sableContention.java
1 // This pathological test tries to grow a FLC list as long as possible before
2 // the threads from the previous FLC list have resumed running. Every one on
3 // the old list then has to scan the entire new list.
4
5 // The CACAO patch in file sableContention.patch can be used to examine the
6 // maximum length. With 500 threads on a quad-core system, I managed to get to
7 // about 120.
8
9 class sableContention {
10         public Object a[] = null;
11         public tt ts[] = null;
12         final int NUM = 500;
13
14         class tt extends Thread {
15                 sableContention y;
16                 int x;
17                 tt(sableContention y, int x) {
18                         this.y = y;
19                         this.x = x;
20                 }
21                 public void run() {
22                         int i = 1;
23                         synchronized(y.a[x]) {
24                                 if (x==0) {
25                                         for (; i<NUM*3/4; i++)
26                                                 y.ts[i].start();
27                                         y.a[x].notify();
28                                 }
29                         }
30                         if (x==0)
31                                 for (; i<NUM; i++)
32                                         y.ts[i].start();
33                         for (int j=0; j<NUM/10; j++)
34                                 synchronized(y.a[(x+j)%(NUM-1)+1]) {
35                                 }
36                 }
37         }
38
39         private void l(int f) {
40                 try {
41                         synchronized(a[NUM-1-f]) {
42                                 if (f > 0) {
43                                         l(f-1);
44                                         if (f<10)
45                                                 Thread.sleep(0, f*100 * 1000);
46                                 }
47                                 else {
48                                         ts = new tt[NUM];
49                                         for (int i=0; i<NUM; i++)
50                                                 ts[i] = new tt(this, i);
51                                         ts[0].start();
52                                         a[0].wait();
53                                 }
54                         }
55                 } catch (InterruptedException e) {
56                 }
57         }
58
59         private void r() {
60                 for (;;) {
61                         a = new Object[NUM];
62                         for (int i=0; i<NUM; i++)
63                                 a[i] = new Object();
64                         l(NUM-1);
65                         for (int i=0; i<NUM; i++)
66                                 try {
67                                         ts[i].join();
68                                 } catch (InterruptedException e) {
69                                 }
70                         System.out.println("running");
71                 }
72         }
73
74         public static void main(String[] args) {
75                 new sableContention().r();
76         }
77 }
78
79 /*
80  * These are local overrides for various environment variables in Emacs.
81  * Please do not remove this and leave it at the end of the file, where
82  * Emacs will automagically detect them.
83  * ---------------------------------------------------------------------
84  * Local variables:
85  * mode: java
86  * indent-tabs-mode: t
87  * c-basic-offset: 4
88  * tab-width: 4
89  * End:
90  * vim:noexpandtab:sw=4:ts=4:
91  */