From patchwork Fri May 23 15:07:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guo Yixuan X-Patchwork-Id: 351874 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 693EA14009C for ; Sat, 24 May 2014 01:08:14 +1000 (EST) 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:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=uEuXyU+EeAPkc/kwDvoBzkWxyaGN3it z61tLWqglEBqfXAwQ4s2CC7KMSd6L4N6fa5xJkgPTvryuTWU2mkSR3o/gHjWlV+o GqZaOd1u3vin7ovFOoeiykjq4n8Z7IN7NTqFczuH/+axI3+GB/xcgIj10z3FcemT /ZkDswGtE7ho= 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:cc:subject:date:message-id:in-reply-to :references; s=default; bh=jjfa2odWRrHbeP9bodwK8PAA1Bw=; b=XmqoO 9nAcYby9AcZZrwDJjBWLYZsBSXbSXnIo77FQXUwwO9DcIjPgGnlLYT3IKBZLtfil 9YCibEehL8BvIrBInXnUv3dh0oFoCB9NdHvhDQ7Bj/ww8VrIM18QjkYAKiu5YpTm Ysqn61Z7uDsuEwm/OIUqH7Bmpck7xNtuHcVXt4= Received: (qmail 4355 invoked by alias); 23 May 2014 15:07:48 -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 4288 invoked by uid 89); 23 May 2014 15:07:47 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-qg0-f42.google.com X-Received: by 10.140.107.67 with SMTP id g61mr7149871qgf.100.1400857664123; Fri, 23 May 2014 08:07:44 -0700 (PDT) From: Guo Yixuan To: libc-alpha@sourceware.org Cc: GUO Yixuan Subject: [PATCH 2/2] New test for pthread_spin_lock (bug 16882) Date: Fri, 23 May 2014 11:07:11 -0400 Message-Id: <1400857631-15954-3-git-send-email-culu.gyx@gmail.com> In-Reply-To: <1400857631-15954-1-git-send-email-culu.gyx@gmail.com> References: <1400857631-15954-1-git-send-email-culu.gyx@gmail.com> From: GUO Yixuan --- nptl/Makefile | 2 +- nptl/tst-spin4.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 nptl/tst-spin4.c diff --git a/nptl/Makefile b/nptl/Makefile index 7551406..0bb6ab1 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -210,7 +210,7 @@ tests = tst-typesizes \ tst-mutexpi1 tst-mutexpi2 tst-mutexpi3 tst-mutexpi4 tst-mutexpi5 \ tst-mutexpi5a tst-mutexpi6 tst-mutexpi7 tst-mutexpi7a tst-mutexpi8 \ tst-mutexpi9 \ - tst-spin1 tst-spin2 tst-spin3 \ + tst-spin1 tst-spin2 tst-spin3 tst-spin4 \ tst-cond1 tst-cond2 tst-cond3 tst-cond4 tst-cond5 tst-cond6 tst-cond7 \ tst-cond8 tst-cond9 tst-cond10 tst-cond11 tst-cond12 tst-cond13 \ tst-cond14 tst-cond15 tst-cond16 tst-cond17 tst-cond18 tst-cond19 \ diff --git a/nptl/tst-spin4.c b/nptl/tst-spin4.c new file mode 100644 index 0000000..5b23a17 --- /dev/null +++ b/nptl/tst-spin4.c @@ -0,0 +1,109 @@ +#include +#include +#include + +static int count = 0; + +static void * +thread_add_one (void *arg) +{ + int tmp; + pthread_spinlock_t *lock = (pthread_spinlock_t *) arg; + + /* When do_test holds the lock for 1 sec, the two thread will be + in contention for the lock. */ + if (pthread_spin_lock (lock) != 0) + { + puts ("thread_add_one(): spin_lock failed"); + pthread_exit ((void *) 1l); + } + + /* sleep 1s before modifying count */ + tmp = count; + sleep (1); + count = tmp + 1; + + if (pthread_spin_unlock (lock) != 0) + { + puts ("thread_add_one(): spin_unlock failed"); + pthread_exit ((void *) 1l); + } + + return NULL; +} + +static int +do_test (void) +{ + pthread_t thr1, thr2; + pthread_spinlock_t lock; + int tmp; + + if (pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE) != 0) + { + puts ("spin_init failed"); + return 1; + } + + if (pthread_spin_lock (&lock) != 0) + { + puts ("1st spin_lock failed"); + return 1; + } + + if (pthread_create (&thr1, NULL, thread_add_one, (void *) &lock) != 0) + { + puts ("1st pthread_create failed"); + return 1; + } + + if (pthread_create (&thr2, NULL, thread_add_one, (void *) &lock) != 0) + { + puts ("2nd pthread_create failed"); + return 1; + } + + /* sleep 1s before modifying count */ + tmp = count; + sleep (1); + count = tmp + 1; + + if (pthread_spin_unlock (&lock) != 0) + { + puts ("1st spin_unlock failed"); + return 1; + } + + void *status; + if (pthread_join (thr1, &status) != 0) + { + puts ("1st pthread_join failed"); + return 1; + } + if (status != NULL) + { + puts ("failure in the 1st thread"); + return 1; + } + if (pthread_join (thr2, &status) != 0) + { + puts ("2nd pthread_join failed"); + return 1; + } + if (status != NULL) + { + puts ("failure in the 2nd thread"); + return 1; + } + + if (count != 3) + { + printf ("count is %d, should be 3\n", count); + return 1; + } + return 0; +} + +#define TIMEOUT 5 +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c"