2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
12 #include "threading.h"
17 #define ASSERT_TRUE(p) \
23 #define ASSERT_FALSE(p) ASSERT_TRUE(!(p))
24 #define ASSERT_EQ(lhs, rhs) ASSERT_TRUE((lhs) == (rhs))
27 pthread_mutex_t mutex;
32 void fn(void *opaque) {
33 struct data *data = (struct data *)opaque;
34 pthread_mutex_lock(&data->mutex);
35 data->data[data->i] = data->i;
37 pthread_mutex_unlock(&data->mutex);
40 int testOrder(size_t numThreads, size_t queueSize) {
42 POOL_ctx *ctx = POOL_create(numThreads, queueSize);
45 pthread_mutex_init(&data.mutex, NULL);
48 for (i = 0; i < 16; ++i) {
49 POOL_add(ctx, &fn, &data);
53 ASSERT_EQ(16, data.i);
56 for (i = 0; i < data.i; ++i) {
57 ASSERT_EQ(i, data.data[i]);
60 pthread_mutex_destroy(&data.mutex);
64 void waitFn(void *opaque) {
69 /* Tests for deadlock */
70 int testWait(size_t numThreads, size_t queueSize) {
72 POOL_ctx *ctx = POOL_create(numThreads, queueSize);
76 for (i = 0; i < 16; ++i) {
77 POOL_add(ctx, &waitFn, &data);
84 int main(int argc, const char **argv) {
86 for (numThreads = 1; numThreads <= 4; ++numThreads) {
88 for (queueSize = 0; queueSize <= 2; ++queueSize) {
89 if (testOrder(numThreads, queueSize)) {
90 printf("FAIL: testOrder\n");
93 if (testWait(numThreads, queueSize)) {
94 printf("FAIL: testWait\n");
99 printf("PASS: testOrder\n");
102 return (POOL_create(0, 1)) ? printf("FAIL: testInvalid\n"), 1
103 : printf("PASS: testInvalid\n"), 0;