summaryrefslogtreecommitdiffstats
path: root/libdimension/tests/concurrency/future.c
blob: b226e4ff7e5f2525050e139e6712bc8f19381338 (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
/*************************************************************************
 * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com>          *
 *                                                                       *
 * This file is part of The Dimension Test Suite.                        *
 *                                                                       *
 * The Dimension Test Suite is free software; you can redistribute it    *
 * and/or modify it under the terms of the GNU General Public License as *
 * published by the Free Software Foundation; either version 3 of the    *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension Test Suite 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  *
 * General Public License for more details.                              *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *************************************************************************/

/**
 * @file
 * Tests for dmnsn_future.
 */

#include "../../platform/platform.c"
#include "../../concurrency/future.c"
#include "../../concurrency/threads.c"
#include "tests.h"
#include <stdatomic.h>

static dmnsn_future *future;
static atomic_int counter = ATOMIC_VAR_INIT(0);
static const int NTHREADS = 8;
static const int CHUNK = 100;

static int
dmnsn_future_test_ccthread(void *ptr, unsigned int thread, unsigned int nthreads)
{
  for (int i = 0; i < CHUNK; ++i) {
    for (int j = 0; j < CHUNK; ++j) {
      atomic_fetch_add(&counter, 1);
    }
    dmnsn_future_increment(future);
  }
  return 0;
}

static int
dmnsn_future_test_thread(void *ptr)
{
  dmnsn_future_set_total(future, NTHREADS*CHUNK);
  return dmnsn_execute_concurrently(future, dmnsn_future_test_ccthread, ptr, NTHREADS);
}

DMNSN_TEST_SETUP(future)
{
  future = dmnsn_new_future();
  dmnsn_new_thread(future, dmnsn_future_test_thread, NULL);
}

DMNSN_TEST_TEARDOWN(future)
{
  int res = dmnsn_future_join(future);
  ck_assert_int_eq(res, 0);
}

void *
dmnsn_future_pause_and_check(void *ptr)
{
  for (int i = 0; i < CHUNK; ++i) {
    dmnsn_future_pause(future);

      int value = atomic_load(&counter);
      ck_assert_int_eq(value%CHUNK, 0);

      for (int j = 0; j < CHUNK; ++j) {
        int new_value = atomic_load(&counter);
        ck_assert_int_eq(new_value, value);
      }

    dmnsn_future_resume(future);
  }

  return NULL;
}

DMNSN_TEST(future, pause_safely)
{
  // Check dmnsn_future_pause() behaviour from two concurrent threads
  pthread_t thread;
  if (pthread_create(&thread, NULL, dmnsn_future_pause_and_check, NULL) != 0) {
    dmnsn_error("Couldn't start thread.");
  }

  dmnsn_future_pause_and_check(NULL);

  dmnsn_join_thread(thread, NULL);
}