OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / bench / threadring.c
1 /*
2 Redistribution and use in source and binary forms, with or without
3 modification, are permitted provided that the following conditions are met:
4
5     * Redistributions of source code must retain the above copyright
6     notice, this list of conditions and the following disclaimer.
7
8     * Redistributions in binary form must reproduce the above copyright
9     notice, this list of conditions and the following disclaimer in the
10     documentation and/or other materials provided with the distribution.
11
12     * Neither the name of "The Computer Language Benchmarks Game" nor the
13     name of "The Computer Language Shootout Benchmarks" nor the names of
14     its contributors may be used to endorse or promote products derived
15     from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * The Computer Language Benchmarks Game
32 * http://shootout.alioth.debian.org/
33
34 * contributed by Premysl Hruby
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <pthread.h>
40 #include <string.h>
41 #include <limits.h>
42
43 #define THREADS (503)
44
45
46 struct stack {
47    char x[PTHREAD_STACK_MIN];
48 };
49
50
51 /* staticaly initialize mutex[0] mutex */
52 static pthread_mutex_t mutex[THREADS];
53 static int data[THREADS];
54 static struct stack stacks[THREADS];
55 /* stacks must be defined staticaly, or my i386 box run of virtual memory for this
56  * process while creating thread +- #400 */
57
58 static void* thread(void *num)
59 {
60    int l = (int)num;
61    int r = (l+1) % THREADS;
62    int token;
63
64    while(1) {
65       pthread_mutex_lock(mutex + l);
66       token = data[l];
67       if (token) {
68          data[r] = token - 1;
69          pthread_mutex_unlock(mutex + r);
70       }
71       else {
72          printf("%i\n", l+1);
73          exit(0);
74       }
75    }
76 }
77
78
79
80 int main(int argc, char **argv)
81 {
82    int i;
83    pthread_t cthread;
84    pthread_attr_t stack_attr;
85
86    if (argc != 2)
87       exit(255);
88    data[0] = atoi(argv[1]);
89
90    pthread_attr_init(&stack_attr);
91
92    for (i = 0; i < THREADS; i++) {
93       pthread_mutex_init(mutex + i, NULL);
94       pthread_mutex_lock(mutex + i);
95
96       pthread_attr_setstack(&stack_attr, &stacks[i], sizeof(struct stack));
97       pthread_create(&cthread, &stack_attr, thread, (void*)i);
98    }
99
100    pthread_mutex_unlock(mutex + 0);
101    pthread_join(cthread, NULL);
102 }