test-tls.c 5.47 KB
Newer Older
Bruno Haible's avatar
Bruno Haible committed
1
/* Test of thread-local storage in multithreaded situations.
Eric Blake's avatar
Eric Blake committed
2
   Copyright (C) 2005, 2008-2014 Free Software Foundation, Inc.
Bruno Haible's avatar
Bruno Haible committed
3

4
   This program is free software: you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6 7
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
Bruno Haible's avatar
Bruno Haible committed
8 9 10

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11 12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
Bruno Haible's avatar
Bruno Haible committed
13

14
   You should have received a copy of the GNU General Public License
15
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Bruno Haible's avatar
Bruno Haible committed
16 17 18

/* Written by Bruno Haible <bruno@clisp.org>, 2005.  */

19
#include <config.h>
Bruno Haible's avatar
Bruno Haible committed
20

21
#if USE_POSIX_THREADS || USE_SOLARIS_THREADS || USE_PTH_THREADS || USE_WINDOWS_THREADS
Bruno Haible's avatar
Bruno Haible committed
22 23 24 25 26 27 28 29 30 31

#if USE_POSIX_THREADS
# define TEST_POSIX_THREADS 1
#endif
#if USE_SOLARIS_THREADS
# define TEST_SOLARIS_THREADS 1
#endif
#if USE_PTH_THREADS
# define TEST_PTH_THREADS 1
#endif
32 33
#if USE_WINDOWS_THREADS
# define TEST_WINDOWS_THREADS 1
Bruno Haible's avatar
Bruno Haible committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#endif

/* Whether to help the scheduler through explicit yield().
   Uncomment this to see if the operating system has a fair scheduler.  */
#define EXPLICIT_YIELD 1

/* Whether to print debugging messages.  */
#define ENABLE_DEBUGGING 0

/* Number of simultaneous threads.  */
#define THREAD_COUNT 16

/* Number of operations performed in each thread.  */
#define REPEAT_COUNT 50000

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

53
#include "glthread/tls.h"
54 55
#include "glthread/thread.h"
#include "glthread/yield.h"
Bruno Haible's avatar
Bruno Haible committed
56 57 58 59 60 61 62 63 64 65 66 67 68

#if ENABLE_DEBUGGING
# define dbgprintf printf
#else
# define dbgprintf if (0) printf
#endif

#if EXPLICIT_YIELD
# define yield() gl_thread_yield ()
#else
# define yield()
#endif

69
static void
Bruno Haible's avatar
Bruno Haible committed
70 71 72 73 74 75 76 77
perhaps_yield (void)
{
  /* Call yield () only with a certain probability, otherwise with GNU Pth
     the sequence of thread activations is too predictable.  */
  if ((((unsigned int) rand () >> 3) % 4) == 0)
    yield ();
}

78 79 80

/* ----------------------- Test thread-local storage ----------------------- */

Bruno Haible's avatar
Bruno Haible committed
81 82 83 84 85 86 87 88 89 90 91
#define KEYS_COUNT 4

static gl_tls_key_t mykeys[KEYS_COUNT];

static void *
worker_thread (void *arg)
{
  unsigned int id = (unsigned int) (unsigned long) arg;
  int i, j, repeat;
  unsigned int values[KEYS_COUNT];

92
  dbgprintf ("Worker %p started\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
93 94 95 96

  /* Initialize the per-thread storage.  */
  for (i = 0; i < KEYS_COUNT; i++)
    {
Bruno Haible's avatar
Bruno Haible committed
97
      values[i] = (((unsigned int) rand () >> 3) % 1000000) * THREAD_COUNT + id;
Bruno Haible's avatar
Bruno Haible committed
98 99
      /* Hopefully no arithmetic overflow.  */
      if ((values[i] % THREAD_COUNT) != id)
100
        abort ();
Bruno Haible's avatar
Bruno Haible committed
101 102 103 104
    }
  perhaps_yield ();

  /* Verify that the initial value is NULL.  */
105
  dbgprintf ("Worker %p before initial verify\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
106 107 108
  for (i = 0; i < KEYS_COUNT; i++)
    if (gl_tls_get (mykeys[i]) != NULL)
      abort ();
109
  dbgprintf ("Worker %p after  initial verify\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
110 111 112
  perhaps_yield ();

  /* Initialize the per-thread storage.  */
113
  dbgprintf ("Worker %p before first tls_set\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
114 115 116 117 118 119
  for (i = 0; i < KEYS_COUNT; i++)
    {
      unsigned int *ptr = (unsigned int *) malloc (sizeof (unsigned int));
      *ptr = values[i];
      gl_tls_set (mykeys[i], ptr);
    }
120
  dbgprintf ("Worker %p after  first tls_set\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
121 122 123 124 125
  perhaps_yield ();

  /* Shuffle around the pointers.  */
  for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
    {
126
      dbgprintf ("Worker %p doing value swapping\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
127 128
      i = ((unsigned int) rand () >> 3) % KEYS_COUNT;
      j = ((unsigned int) rand () >> 3) % KEYS_COUNT;
Bruno Haible's avatar
Bruno Haible committed
129
      if (i != j)
130 131 132
        {
          void *vi = gl_tls_get (mykeys[i]);
          void *vj = gl_tls_get (mykeys[j]);
Bruno Haible's avatar
Bruno Haible committed
133

134 135 136
          gl_tls_set (mykeys[i], vj);
          gl_tls_set (mykeys[j], vi);
        }
Bruno Haible's avatar
Bruno Haible committed
137 138 139 140
      perhaps_yield ();
    }

  /* Verify that all the values are from this thread.  */
141
  dbgprintf ("Worker %p before final verify\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
142 143 144
  for (i = 0; i < KEYS_COUNT; i++)
    if ((*(unsigned int *) gl_tls_get (mykeys[i]) % THREAD_COUNT) != id)
      abort ();
145
  dbgprintf ("Worker %p after  final verify\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
146 147
  perhaps_yield ();

148
  dbgprintf ("Worker %p dying.\n", gl_thread_self_pointer ());
Bruno Haible's avatar
Bruno Haible committed
149 150 151
  return NULL;
}

152
static void
Bruno Haible's avatar
Bruno Haible committed
153 154 155 156 157 158 159 160 161
test_tls (void)
{
  int pass, i;

  for (pass = 0; pass < 2; pass++)
    {
      gl_thread_t threads[THREAD_COUNT];

      if (pass == 0)
162 163
        for (i = 0; i < KEYS_COUNT; i++)
          gl_tls_key_init (mykeys[i], free);
Bruno Haible's avatar
Bruno Haible committed
164
      else
165 166
        for (i = KEYS_COUNT - 1; i >= 0; i--)
          gl_tls_key_init (mykeys[i], free);
Bruno Haible's avatar
Bruno Haible committed
167 168 169

      /* Spawn the threads.  */
      for (i = 0; i < THREAD_COUNT; i++)
170
        threads[i] = gl_thread_create (worker_thread, NULL);
Bruno Haible's avatar
Bruno Haible committed
171 172 173

      /* Wait for the threads to terminate.  */
      for (i = 0; i < THREAD_COUNT; i++)
174
        gl_thread_join (threads[i], NULL);
Bruno Haible's avatar
Bruno Haible committed
175 176

      for (i = 0; i < KEYS_COUNT; i++)
177
        gl_tls_key_destroy (mykeys[i]);
Bruno Haible's avatar
Bruno Haible committed
178 179 180
    }
}

181 182 183

/* -------------------------------------------------------------------------- */

Bruno Haible's avatar
Bruno Haible committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
int
main ()
{
#if TEST_PTH_THREADS
  if (!pth_init ())
    abort ();
#endif

  printf ("Starting test_tls ..."); fflush (stdout);
  test_tls ();
  printf (" OK\n"); fflush (stdout);

  return 0;
}

#else

/* No multithreading available.  */

203 204
#include <stdio.h>

Bruno Haible's avatar
Bruno Haible committed
205 206 207
int
main ()
{
Bruno Haible's avatar
Bruno Haible committed
208
  fputs ("Skipping test: multithreading not enabled\n", stderr);
Bruno Haible's avatar
Bruno Haible committed
209 210 211 212
  return 77;
}

#endif