...

Text file src/golang.org/x/exp/shootout/threadring.c

Documentation: golang.org/x/exp/shootout

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

View as plain text