summaryrefslogtreecommitdiff
path: root/tests/palette-concurrency-stress-test.c
blob: a42b15d95c47c83969f37fa5d2b300b22cd6d06f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* babl - dynamically extendable universal pixel conversion library.
 * Copyright (C) 2009 Martin Nordholts
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see
 * <https://www.gnu.org/licenses/>.
 */


#include "config.h"

#include <stdlib.h>
#include <pthread.h>

#include "babl.h"


#define N_THREADS 10
#define N_PIXELS  1000000 /* (per thread) */


/* should be the same as HASH_TABLE_SIZE in babl/babl-palette.c */
#define BABL_PALETTE_HASH_TABLE_SIZE 1111


typedef struct
{
  const Babl    *fish;
  unsigned char  src[4 * N_PIXELS];
  unsigned char  dest[N_PIXELS];
} ThreadContext;


static void *
thread_proc (void *data)
{
  ThreadContext *ctx = data;

  babl_process (ctx->fish, ctx->src, ctx->dest, N_PIXELS);

  return NULL;
}

int
main (int    argc,
      char **argv)
{
  const Babl    *pal;
  const Babl    *pal_format;
  unsigned char  colors[4 * N_THREADS];
  pthread_t      threads[N_THREADS];
  ThreadContext *ctx[N_THREADS];
  int            i, j;
  int            OK = 1;

  babl_init ();

  /* create a palette of N_THREADS different colors, all of which have the same
   * hash
   */
  pal = babl_new_palette (NULL, &pal_format, NULL);

  for (i = 0; i < N_THREADS; i++)
    {
      unsigned char *p = &colors[4 * i];
      unsigned int   v;

      v = i * BABL_PALETTE_HASH_TABLE_SIZE;

      p[0] = (v >>  0) & 0xff;
      p[1] = (v >>  8) & 0xff;
      p[2] = (v >> 16) & 0xff;
      p[3] = 0xff;
    }

  babl_palette_set_palette (pal, babl_format ("R'G'B'A u8"), colors, N_THREADS);

  /* initialize the thread contexts such that each thread processes a buffer
   * containing a single, distinct color
   */
  for (i = 0; i < N_THREADS; i++)
    {
      ctx[i] = malloc (sizeof (ThreadContext));

      ctx[i]->fish = babl_fish (babl_format ("R'G'B'A u8"), pal_format);

      for (j = 0; j < 4 * N_PIXELS; j++)
        {
          ctx[i]->src[j] = colors[4 * i + j % 4];
        }
    }

  /* run all threads at the same time */
  for (i = 0; i < N_THREADS; i++)
    {
      pthread_create (&threads[i],
                      NULL, /* attr */
                      thread_proc,
                      ctx[i]);
    }

  /* wait for them to finish */
  for (i = 0; i < N_THREADS; i++)
    {
      pthread_join (threads[i],
                    NULL /* thread_return */);
    }

  /* verify the results */
  for (i = 0; i < N_THREADS; i++)
    {
      for (j = 0; OK && j < N_PIXELS; j++)
        {
          OK = (ctx[i]->dest[j] == i);
        }

      free (ctx[i]);
    }

  babl_exit ();

  return ! OK;
}