github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/test/bench/shootout/threadring.c (about) 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 <stdint.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <pthread.h> 41 #include <string.h> 42 #include <limits.h> 43 44 // PTHREAD_STACK_MIN undeclared on mingw 45 #ifndef PTHREAD_STACK_MIN 46 #define PTHREAD_STACK_MIN 65535 47 #endif 48 49 #define THREADS (503) 50 51 struct stack { 52 char x[PTHREAD_STACK_MIN]; 53 }; 54 55 56 /* staticaly initialize mutex[0] mutex */ 57 static pthread_mutex_t mutex[THREADS]; 58 static int data[THREADS]; 59 static struct stack stacks[THREADS]; 60 /* stacks must be defined staticaly, or my i386 box run of virtual memory for this 61 * process while creating thread +- #400 */ 62 63 static void* thread(void *num) 64 { 65 int l = (int)(uintptr_t)num; 66 int r = (l+1) % THREADS; 67 int token; 68 69 while(1) { 70 pthread_mutex_lock(mutex + l); 71 token = data[l]; 72 if (token) { 73 data[r] = token - 1; 74 pthread_mutex_unlock(mutex + r); 75 } 76 else { 77 printf("%i\n", l+1); 78 exit(0); 79 } 80 } 81 } 82 83 84 85 int main(int argc, char **argv) 86 { 87 int i; 88 pthread_t cthread; 89 pthread_attr_t stack_attr; 90 91 if (argc != 2) 92 exit(255); 93 data[0] = atoi(argv[1]); 94 95 pthread_attr_init(&stack_attr); 96 97 for (i = 0; i < THREADS; i++) { 98 pthread_mutex_init(mutex + i, NULL); 99 pthread_mutex_lock(mutex + i); 100 101 #if defined(__MINGW32__) || defined(__MINGW64__) 102 pthread_attr_setstackaddr(&stack_attr, &stacks[i]); 103 pthread_attr_setstacksize(&stack_attr, sizeof(struct stack)); 104 #else 105 pthread_attr_setstack(&stack_attr, &stacks[i], sizeof(struct stack)); 106 #endif 107 108 pthread_create(&cthread, &stack_attr, thread, (void*)(uintptr_t)i); 109 } 110 111 pthread_mutex_unlock(mutex + 0); 112 pthread_join(cthread, NULL); 113 }