From patchwork Tue Jun 27 18:31:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 106473 Delivered-To: patch@linaro.org Received: by 10.182.135.102 with SMTP id pr6csp2694774obb; Tue, 27 Jun 2017 11:33:30 -0700 (PDT) X-Received: by 10.84.178.101 with SMTP id y92mr7196025plb.116.1498588410341; Tue, 27 Jun 2017 11:33:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1498588410; cv=none; d=google.com; s=arc-20160816; b=Nqp7/6OrJsFDENj/PZmu/B+OHIvg/e/w+cJzWBNUDGchKmfGm7zqITr+tBkhmRwWr1 Y8YrrvievOzC16fM9XlYaj55YS2KWUsxzzIv5Manhqmh2Q8o+haV7Sk5xzDlYbPNzbPd PZOJcD0PBga6tDKW7M40fki1TgWWKWPuI1vVNBse4HDAAJJlWNTMRsUSf+CaK1VydFYg GesDYCHR2/+/boZvzbSqcgeBUP7V32FtpsWa4PcBqBapret8X2sKNWPUA7Ce1BXUnv8F sjMMjMZVFz4uAY7eFKFsYoFs/do7YQjykqI1I4B+UzwJsqJeJfnkybqy8oxMVTFRgrsv dgig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=9KVzQ12ePywHw9Rxi9Tfy0CM+e+2ctY2Z8uTGw+fjpM=; b=nBNMNpn5cEAHO/K0l1WlaUze9Exxg6TiTViLJ85+8Cj0Ype5hiALDHvf9kligprsvO 4tvzMYzQA55IHT2aGN8LcK81JGGhi8GXM79JvGYEN6ct2BpNHax2xK2i5o6NeZcdjVUp D9i9HsOmoqXbI1T1Tt8Mo8Y7PJrOyUPsEjbagYk2ETnGyX5ursTSKFp3xPXRIgVvv3T1 8dBLAKfa87wXCiSXgGNRgUrs70YckF31OJQpum3cC5mq53Q4bO8IQYFVgzNcItDUtGg3 OOrIj3+zmYD7lwuwN1NRUPA16Z6vcQxCclm47fQXRMh+IOtrQaxJIV9wcCaKdXbk2+G3 +Lyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.b=sVaEoDYI; spf=pass (google.com: domain of libc-alpha-return-81309-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-81309-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id k65si2330103pfg.175.2017.06.27.11.33.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Jun 2017 11:33:30 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-81309-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.b=sVaEoDYI; spf=pass (google.com: domain of libc-alpha-return-81309-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-81309-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=AkduQrEJNEpz69LCNMujHCIb4JCwaW3 1sAsgZom5yRVqiiMsHCgkla5S845KduXwpDzo9UE5fJacqtqhgg7XoL6s4TZfG9l SmeEE6dq3b1Kc/xexisE5UOruaojfiewfN6htWs6LnMnZrEyy8GbPr/aRdzzI3El GnGL9CQqS0PU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=1TJgzT8CE12ECrZCkhFLzPkCkos=; b=sVaEo DYIBAhkXVvCBOuWgPIq63xnrU95xqrgAsi+q40/EJ0i5MjpVWdCdpjL5BrxXVTRG j+icSmlmnMI69rx0XxtUEf65Iz9Xm6PlGZrGpLuFJiVsY3sHId6axB7NIf/OrlKO HgEfUZwwYfU9g5QDT98QSxKgvQo1zTUD8dYAAI= Received: (qmail 21879 invoked by alias); 27 Jun 2017 18:31:57 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 21768 invoked by uid 89); 27 Jun 2017 18:31:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f179.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=9KVzQ12ePywHw9Rxi9Tfy0CM+e+2ctY2Z8uTGw+fjpM=; b=Q3evlXzOwGw9d7QsLr6eXt8L5iWtrPBY5oVnQQv+hIgc4ArMuKRu5wwmub5p5A3T1M sGVb4bS+8RCUnw+xIkwC93CXkhoXdvH6FFc5851t/G5IHAC3y6zAC7g1V7ZlgBF11jlr AEqz82xOf6wPxOIrWFyA6hcP573EM+3KqJc9x83Tz1YQ0w2isAMQ5KaxQIDCsxvV33Jk BVPj5uMXI3whlTPX4hOV8pS/A7mE/COaX7xQSTVoCwZ7eycZjk9+eJykyu5kBjea3QMb QmEEuys91oqLNo1z4twLrsKQPqK0jPOtdZ4Z52LGjqZXRo8RriEIceZmXhGhJdRNEhAj 0PqA== X-Gm-Message-State: AKS2vOw6nDg6qesAQBsp84KfNhPkU3pxpLWYHJf6jpMkiVrxbme4pCfp kMVy7xlmBMqUsogsvQfzrg== X-Received: by 10.200.50.93 with SMTP id y29mr8575739qta.108.1498588306980; Tue, 27 Jun 2017 11:31:46 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 7/8] nptl: Add test cases for ISO C11 threads Date: Tue, 27 Jun 2017 15:31:31 -0300 Message-Id: <1498588292-23093-8-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1498588292-23093-1-git-send-email-adhemerval.zanella@linaro.org> References: <1498588292-23093-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds to testsuite new test cases to test all new introduced C11 threads functions, types and macros are tested. Checked with a build for all major ABI (aarch64-linux-gnu, alpha-linux-gnu, arm-linux-gnueabi, i386-linux-gnu, ia64-linux-gnu, m68k-linux-gnu, microblaze-linux-gnu [1], mips{64}-linux-gnu, nios2-linux-gnu, powerpc{64le}-linux-gnu, s390{x}-linux-gnu, sparc{64}-linux-gnu, tile{pro,gx}-linux-gnu, and x86_64-linux-gnu). Also ran a full check on aarch64-linux-gnu, x86_64-linux-gnu, i686-linux-gnu, arm-linux-gnueabhf, and powerpc64le-linux-gnu. Adhemerval Zanella Juan Manuel Torres Palma * nptl/Makefile (tests): Add new test files. * nptl/tst-call-once.c : New file. Tests C11 functions and types. * nptl/tst-cnd-basic.c: Likewise. * nptl/tst-cnd-broadcast.c: Likewise. * nptl/tst-cnd-timedwait.c: Likewise. * nptl/tst-mtx-basic.c: Likewise. * nptl/tst-mtx-recursive.c: Likewise. * nptl/tst-mtx-timedlock.c: Likewise. * nptl/tst-mtx-trylock.c: Likewise. * nptl/tst-thrd-basic.c: Likewise. * nptl/tst-thrd-detach.c: Likewise. * nptl/tst-thrd-sleep.c: Likewise. * nptl/tst-tss-basic.c: Likewise. --- ChangeLog | 17 +++++++++ nptl/Makefile | 5 ++- nptl/tst-call-once.c | 66 ++++++++++++++++++++++++++++++++ nptl/tst-cnd-basic.c | 68 +++++++++++++++++++++++++++++++++ nptl/tst-cnd-broadcast.c | 83 ++++++++++++++++++++++++++++++++++++++++ nptl/tst-cnd-timedwait.c | 70 ++++++++++++++++++++++++++++++++++ nptl/tst-mtx-basic.c | 73 ++++++++++++++++++++++++++++++++++++ nptl/tst-mtx-recursive.c | 45 ++++++++++++++++++++++ nptl/tst-mtx-timedlock.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ nptl/tst-mtx-trylock.c | 90 ++++++++++++++++++++++++++++++++++++++++++++ nptl/tst-thrd-detach.c | 52 +++++++++++++++++++++++++ nptl/tst-thrd-sleep.c | 51 +++++++++++++++++++++++++ nptl/tst-tss-basic.c | 75 ++++++++++++++++++++++++++++++++++++ 13 files changed, 792 insertions(+), 1 deletion(-) create mode 100644 nptl/tst-call-once.c create mode 100644 nptl/tst-cnd-basic.c create mode 100644 nptl/tst-cnd-broadcast.c create mode 100644 nptl/tst-cnd-timedwait.c create mode 100644 nptl/tst-mtx-basic.c create mode 100644 nptl/tst-mtx-recursive.c create mode 100644 nptl/tst-mtx-timedlock.c create mode 100644 nptl/tst-mtx-trylock.c create mode 100644 nptl/tst-thrd-detach.c create mode 100644 nptl/tst-thrd-sleep.c create mode 100644 nptl/tst-tss-basic.c -- 2.7.4 diff --git a/nptl/tst-call-once.c b/nptl/tst-call-once.c new file mode 100644 index 0000000..331174e --- /dev/null +++ b/nptl/tst-call-once.c @@ -0,0 +1,66 @@ +/* C11 threads call_once test. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Flag that controls the first thread access. */ +static once_flag flag = ONCE_FLAG_INIT; + +static int value = 0; + +static void +do_once (void) +{ + value++; +} + +static int +func (void* data) +{ + call_once (&flag, do_once); + thrd_exit (thrd_success); +} + +#define N 20 + +int +do_test (void) +{ + thrd_t ids[N]; + + for (int i = 0; i < N; ++i) + { + if (thrd_create (&ids[i], func, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + } + + /* Join threads. */ + for (int i = 0; i < N; ++i) + { + if (thrd_join (ids[i], NULL) != thrd_success) + FAIL_EXIT1 ("thrd_join failed"); + } + + return (value != 1); +} + +#include diff --git a/nptl/tst-cnd-basic.c b/nptl/tst-cnd-basic.c new file mode 100644 index 0000000..56de4d7 --- /dev/null +++ b/nptl/tst-cnd-basic.c @@ -0,0 +1,68 @@ +/* C11 threads condition variable tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Shared condition variable between child and parent. */ +static cnd_t cond; + +/* Mutex needed to signal and wait threads. */ +static mtx_t mutex; + +static int +signal_parent (void) +{ + if (cnd_signal (&cond) != thrd_success) + FAIL_EXIT1 ("cnd_signal"); + + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + thrd_t id; + + if (cnd_init (&cond) != thrd_success) + FAIL_EXIT1 ("cnd_init failed"); + if (mtx_init (&mutex, mtx_plain) != thrd_success) + FAIL_EXIT1 ("mtx_init failed"); + + if (thrd_create (&id, (thrd_start_t) signal_parent, NULL) + != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + if (cnd_wait (&cond, &mutex) != thrd_success) + FAIL_EXIT1 ("cnd_wait failed"); + + /* Joining is not mandatory here, but still done to assure child thread + ends correctly. */ + if (thrd_join (id, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_join failed"); + + mtx_destroy (&mutex); + cnd_destroy (&cond); + + return 0; +} + +#include diff --git a/nptl/tst-cnd-broadcast.c b/nptl/tst-cnd-broadcast.c new file mode 100644 index 0000000..1ca87a3 --- /dev/null +++ b/nptl/tst-cnd-broadcast.c @@ -0,0 +1,83 @@ +/* C11 threads condition broadcast variable tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Condition variable where child threads will wait. */ +static cnd_t cond; + +/* Mutex to control wait on cond. */ +static mtx_t mutex; + +/* Code executed by each thread. */ +static int +child_wait (void* data) +{ + /* Wait until parent thread sends broadcast here. */ + mtx_lock (&mutex); + cnd_wait (&cond, &mutex); + mtx_unlock (&mutex); + + thrd_exit (thrd_success); +} + +#define N 5 + +static int +do_test (void) +{ + thrd_t ids[N]; + unsigned char i; + + if (cnd_init (&cond) != thrd_success) + FAIL_EXIT1 ("cnd_init failed"); + if (mtx_init (&mutex, mtx_plain) != thrd_success) + FAIL_EXIT1 ("mtx_init failed"); + + /* Create N new threads. */ + for (i = 0; i < N; ++i) + { + if (thrd_create (&ids[i], child_wait, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + } + + /* Wait for other threads to reach their wait func. */ + thrd_sleep (&((struct timespec){.tv_sec = 2}), NULL); + + mtx_lock (&mutex); + if (cnd_broadcast (&cond) != thrd_success) + FAIL_EXIT1 ("cnd_broadcast failed"); + mtx_unlock (&mutex); + + for (i = 0; i < N; ++i) + { + if (thrd_join (ids[i], NULL) != thrd_success) + FAIL_EXIT1 ("thrd_join failed"); + } + + mtx_destroy (&mutex); + cnd_destroy (&cond); + + return 0; +} + +#include diff --git a/nptl/tst-cnd-timedwait.c b/nptl/tst-cnd-timedwait.c new file mode 100644 index 0000000..64a9546 --- /dev/null +++ b/nptl/tst-cnd-timedwait.c @@ -0,0 +1,70 @@ +/* C11 threads condition timed wait variable tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Shared condition variable between child and parent. */ +static cnd_t cond; + +/* Mutex needed to signal and wait threads. */ +static mtx_t mutex; + +static int +signal_parent (void *arg) +{ + if (cnd_signal (&cond) != thrd_success) + FAIL_EXIT1 ("cnd_signal failed"); + + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + thrd_t id; + struct timespec w_time; + + if (cnd_init (&cond) != thrd_success) + FAIL_EXIT1 ("cnd_init failed"); + if (mtx_init (&mutex, mtx_plain) != thrd_success) + FAIL_EXIT1 ("mtx_init failed"); + + if (clock_gettime (CLOCK_REALTIME, &w_time) != 0) + FAIL_EXIT1 ("clock_gettime failed"); + w_time.tv_nsec += 150000; + + if (thrd_create (&id, signal_parent, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + if (cnd_timedwait (&cond, &mutex, &w_time) != thrd_success) + FAIL_EXIT1 ("cnd_timedwait failed"); + + if (thrd_join (id, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_join failed"); + + mtx_destroy (&mutex); + cnd_destroy (&cond); + + return 0; +} + +#include diff --git a/nptl/tst-mtx-basic.c b/nptl/tst-mtx-basic.c new file mode 100644 index 0000000..c064405 --- /dev/null +++ b/nptl/tst-mtx-basic.c @@ -0,0 +1,73 @@ +/* C11 threads basic mutex tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Shared mutex between child and parent. */ +static mtx_t mutex; + +/* Shared counter to check possible race conditions. */ +static int counter; + +static int +child_add (void *arg) +{ + if (mtx_lock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_lock failed"); + + counter++; + + if (mtx_unlock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_unlock failed"); + + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + mtx_init (&mutex, mtx_plain); + + thrd_t id; + if (thrd_create (&id, child_add, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + if (mtx_lock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_lock failed"); + + counter++; + + if (mtx_unlock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_unlock failed"); + + if (thrd_join (id, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_join failed"); + + if (counter != 2) + FAIL_EXIT1 ("counter (%d) != 2", counter); + + mtx_destroy (&mutex); + + return 0; +} + +#include diff --git a/nptl/tst-mtx-recursive.c b/nptl/tst-mtx-recursive.c new file mode 100644 index 0000000..df959c7 --- /dev/null +++ b/nptl/tst-mtx-recursive.c @@ -0,0 +1,45 @@ +/* C11 threads recursive mutex tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +static int +do_test (void) +{ + static mtx_t mutex; + + if (mtx_init (&mutex, mtx_recursive) != thrd_success) + FAIL_EXIT1 ("mtx_init failed"); + + if (mtx_lock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_lock failed"); + + /* Lock mutex second time, if not recursive should deadlock. */ + if (mtx_lock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_lock failed"); + + mtx_destroy (&mutex); + + return 0; +} + +#include diff --git a/nptl/tst-mtx-timedlock.c b/nptl/tst-mtx-timedlock.c new file mode 100644 index 0000000..1f02bd4 --- /dev/null +++ b/nptl/tst-mtx-timedlock.c @@ -0,0 +1,98 @@ +/* C11 threads timed mutex tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Shared mutex between child and parent. */ +static mtx_t mutex; + +/* Shared counter to check possible race conditions. */ +static char shrd_counter; + +/* Maximum amount of time waiting for mutex. */ +static struct timespec wait_time; + +/* Function to choose an action to do, depending on mtx_timedlock + return value. */ +static inline void +choose_action (int action, char* thread_name) +{ + switch (action) + { + case thrd_success: + ++shrd_counter; + + if (mtx_unlock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_unlock failed"); + break; + + case thrd_timedout: + break; + + case thrd_error: + FAIL_EXIT1 ("%s lock error", thread_name); + break; + } +} + +static int +child_add (void *arg) +{ + char child_name[] = "child"; + + /* Try to lock mutex. */ + choose_action (mtx_timedlock (&mutex, &wait_time), child_name); + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + thrd_t id; + char parent_name[] = "parent"; + + if (mtx_init (&mutex, mtx_timed) != thrd_success) + FAIL_EXIT1 ("mtx_init failed"); + + if (clock_gettime (CLOCK_REALTIME, &wait_time) != 0) + FAIL_EXIT1 ("clock_gettime failed"); + /* Tiny amount of time, to assure that if any thread finds it busy. + It will receive thrd_timedout. */ + wait_time.tv_nsec += 1; + + if (thrd_create (&id, child_add, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + choose_action (mtx_timedlock (&mutex, &wait_time), parent_name); + + if (thrd_join (id, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_join failed"); + + if (shrd_counter != 2 && shrd_counter != 1) + FAIL_EXIT1 ("shrd_counter != {1,2} (%d)", shrd_counter); + + mtx_destroy (&mutex); + + return 0; +} + +#include diff --git a/nptl/tst-mtx-trylock.c b/nptl/tst-mtx-trylock.c new file mode 100644 index 0000000..5c1e804 --- /dev/null +++ b/nptl/tst-mtx-trylock.c @@ -0,0 +1,90 @@ +/* C11 threads trylock mutex tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Shared mutex between child and parent. */ +static mtx_t mutex; + +/* Shared counter to check possible race conditions. */ +static char shrd_counter; + +/* Function to choose an action to do, depending on mtx_trylock + return value. */ +static inline void +choose_action (int action, char* thread_name) +{ + switch (action) + { + case thrd_success: + ++shrd_counter; + + if (mtx_unlock (&mutex) != thrd_success) + FAIL_EXIT1 ("mtx_unlock failed"); + break; + + case thrd_busy: + break; + + case thrd_error: + FAIL_EXIT1 ("%s lock error", thread_name); + break; + } +} + +static int +child_add (void *arg) +{ + char child_name[] = "child"; + + /* Try to lock mutex. */ + choose_action (mtx_trylock (&mutex), child_name); + + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + thrd_t id; + char parent_name[] = "parent"; + + if (mtx_init (&mutex, mtx_timed) != thrd_success) + FAIL_EXIT1 ("mtx_init failed"); + + if (thrd_create (&id, child_add, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + choose_action (mtx_trylock (&mutex), parent_name); + + if (thrd_join (id, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_join failed"); + + if (shrd_counter != 2 && shrd_counter != 1) + FAIL_EXIT1 ("shrd_counter != {1,2} (%d)", shrd_counter); + + mtx_destroy (&mutex); + + return 0; +} + +#include diff --git a/nptl/tst-thrd-detach.c b/nptl/tst-thrd-detach.c new file mode 100644 index 0000000..ec28173 --- /dev/null +++ b/nptl/tst-thrd-detach.c @@ -0,0 +1,52 @@ +/* C11 threads thread detach tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include +#include + +#include + +static int +detach_thrd (void *arg) +{ + if (thrd_detach (thrd_current ()) != thrd_success) + FAIL_EXIT1 ("thrd_detach failed"); + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + thrd_t id; + + /* Create new thread. */ + if (thrd_create (&id, detach_thrd, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + /* Give some time so the thread can finish. */ + thrd_sleep (&(struct timespec){.tv_sec = 2}, NULL); + + if (thrd_join (id, NULL) == thrd_success) + FAIL_EXIT1 ("thrd_join succeed where it should fail"); + + return 0; +} + +#include diff --git a/nptl/tst-thrd-sleep.c b/nptl/tst-thrd-sleep.c new file mode 100644 index 0000000..17380b2 --- /dev/null +++ b/nptl/tst-thrd-sleep.c @@ -0,0 +1,51 @@ +/* C11 threads thread sleep tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include +#include + +#include + +static int +sleep_thrd (void *arg) +{ + struct timespec const *tl = (struct timespec const *) arg; + if (thrd_sleep (tl, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_sleep failed"); + + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + thrd_t id; + struct timespec wait_time = {.tv_sec = 3}; + + if (thrd_create (&id, sleep_thrd, (void *) (&wait_time)) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + if (thrd_join (id, NULL) != thrd_success) + FAIL_EXIT1 ("thrd failed"); + + return 0; +} + +#include diff --git a/nptl/tst-tss-basic.c b/nptl/tst-tss-basic.c new file mode 100644 index 0000000..8181dc1 --- /dev/null +++ b/nptl/tst-tss-basic.c @@ -0,0 +1,75 @@ +/* C11 threads specific storage tests. + Copyright (C) 2017 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 + . */ + +#include +#include +#include + +#include + +/* Thread specific storage. */ +static tss_t key; + +#define TSS_VALUE (void*) 0xFF + +static int +tss_thrd (void *arg) +{ + if (tss_create (&key, NULL) != thrd_success) + FAIL_EXIT1 ("tss_create failed"); + + if (tss_set (key, TSS_VALUE)) + FAIL_EXIT1 ("tss_set failed"); + + void *value = tss_get (key); + if (value == 0) + FAIL_EXIT1 ("tss_get failed"); + if (value != TSS_VALUE) + FAIL_EXIT1 ("tss_get returned %p, expected %p", value, TSS_VALUE); + + thrd_exit (thrd_success); +} + +static int +do_test (void) +{ + /* Setting an invalid key should return an error. */ + if (tss_set (key, TSS_VALUE) == thrd_success) + FAIL_EXIT1 ("tss_set succeed where it should have failed"); + + if (tss_create (&key, NULL) != thrd_success) + FAIL_EXIT1 ("tss_create failed"); + + thrd_t id; + if (thrd_create (&id, tss_thrd, NULL) != thrd_success) + FAIL_EXIT1 ("thrd_create failed"); + + if (thrd_join (id, NULL) != thrd_success) + FAIL_EXIT1 ("thrd failed"); + + /* The value set in tss_thrd should not be visible here. */ + void *value = tss_get (key); + if (value != 0) + FAIL_EXIT1 ("tss_get succeed where it should have failed"); + + tss_delete (key); + + return 0; +} + +#include