aboutsummaryrefslogtreecommitdiff
path: root/benchtests/thread_create-source.c
blob: 4a72168224a7348a3dcfa9a4f2d545d1e87eb74e (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
/* Measure pthread_create thread creation with different stack
   and guard sizes.

   Copyright (C) 2017-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C 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 2.1 of the License, or (at your option) any later version.

   The GNU C 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 the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <stdio.h>
#include <unistd.h>
#include <support/xthread.h>

static size_t pgsize;

static void
thread_create_init (void)
{
  pgsize = sysconf (_SC_PAGESIZE);
}

static void *
thread_dummy (void *arg)
{
  return NULL;
}

static void
thread_create (int nthreads, size_t stacksize, size_t guardsize)
{
  pthread_attr_t attr;
  xpthread_attr_init (&attr);

  stacksize = stacksize * pgsize;
  guardsize = guardsize * pgsize;

  xpthread_attr_setstacksize (&attr, stacksize);
  xpthread_attr_setguardsize (&attr, guardsize);

  pthread_t ts[nthreads];

  for (int i = 0; i < nthreads; i++)
    ts[i] = xpthread_create (&attr, thread_dummy, NULL);

  for (int i = 0; i < nthreads; i++)
    xpthread_join (ts[i]);
}