From patchwork Thu Feb 5 21:56:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xin Tong X-Patchwork-Id: 436968 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F7831401DC for ; Fri, 6 Feb 2015 08:57:19 +1100 (AEDT) Received: from localhost ([::1]:45719 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YJUQH-0001kL-Cw for incoming@patchwork.ozlabs.org; Thu, 05 Feb 2015 16:57:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60809) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YJUPp-00014v-0j for qemu-devel@nongnu.org; Thu, 05 Feb 2015 16:56:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YJUPl-0000ov-S3 for qemu-devel@nongnu.org; Thu, 05 Feb 2015 16:56:48 -0500 Received: from mail-qg0-x229.google.com ([2607:f8b0:400d:c04::229]:65304) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YJUPl-0000oo-Nq for qemu-devel@nongnu.org; Thu, 05 Feb 2015 16:56:45 -0500 Received: by mail-qg0-f41.google.com with SMTP id i50so7680123qgf.0 for ; Thu, 05 Feb 2015 13:56:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=M2SmEYfEDKFhA3eN75j+mt0x7FWaKZ/MiUFY28jn8m4=; b=x/TZtiHpE4EMrxCeEb7lGqvTfEz+Nd/WqnzVtKNzwMPaoNYZK+Y9hqzeJdo2amFNm2 slyKyxXs8nsa2aI6xjYIvPpek2VEFYpJeNcf+nyK+Ia+CY3vT8GH0uApQ21rinBLFzd+ KyZQOTm05wviKMy649gm9yuzblIU9BaudpFDma9RyJaBvdSimzMbfgTO0XLiX9c3WQ/I +lYqSjDr5o5cOvBTlnJ32mv73MxrxLqUd8zsrJwqnYvc6C00Zr+MWXiLQ2IpE76mz2GJ upg5U6dusRb/8lx/UQqnhpdc5mu+KFBKSN7oJRhH8dirYXcJdc4qMfbtg7xv9W7tUFBI D1sg== X-Received: by 10.224.46.8 with SMTP id h8mr810003qaf.0.1423173405174; Thu, 05 Feb 2015 13:56:45 -0800 (PST) Received: from localhost.localdomain ([104.236.198.220]) by mx.google.com with ESMTPSA id s63sm479216qgd.26.2015.02.05.13.56.44 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Feb 2015 13:56:44 -0800 (PST) From: trent.tong@gmail.com To: qemu-devel@nongnu.org Date: Thu, 5 Feb 2015 16:56:39 -0500 Message-Id: <1423173399-14467-1-git-send-email-trent.tong@gmail.com> X-Mailer: git-send-email 1.9.1 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::229 Cc: Xin Tong Subject: [Qemu-devel] [PATCH v1] add intel restricted transactional memory test case. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add test case for intel restrict transactional memory. compiled with Intel ICC 15.0 as well as GCC 4.8. This test case can be used to test intel RTM support in the target-i386 frontend. Signed-off-by: Xin Tong diff --git a/tests/tcg/test-intelrtm.c b/tests/tcg/test-intelrtm.c new file mode 100644 index 0000000..ca1ce16 --- /dev/null +++ b/tests/tcg/test-intelrtm.c @@ -0,0 +1,86 @@ +/**************************************************************************** +This file contains an example of Intel Restricted Transactional Memory (RTM). +In the example multiple threads are created to simulatenously increment +elements in a shared array. + +This test case should be compiled with Intel ICC compiler as it uses ICC RTM +intrnsics. +****************************************************************************/ +#include +#include +#include /* _XBEGIN_START */ + +#define ARRAY_SIZE 1024 +#define TESTTHREAD_COUNT 32 +/* lock for atomic access to the testarray */ +static pthread_mutex_t arraylock; +static pthread_t *threadarray; +static int testarray[ARRAY_SIZE]; + +static void increment_func(void *arg) +{ + int i; + long int threadnum = (long int)arg; + unsigned int status = _xbegin(); + if (status == _XBEGIN_STARTED) { + /* test _xtest() and _xabort(). fallback path for odd-num thread */ + if (_xtest() && (threadnum % 2)) { + _xabort(_XABORT_EXPLICIT); + } + for (i = 0; i < ARRAY_SIZE; ++i) { + testarray[i]++; + } + _xend(); + } else { + /* fallback path. hold array lock */ + pthread_mutex_lock(&arraylock); + for (i = 0; i < ARRAY_SIZE; ++i) { + testarray[i]++; + } + /* release array lock */ + pthread_mutex_unlock(&arraylock); + } +} + +static void run_test(int threadcount) +{ + int i; + /* create the test threads */ + for (i = 0; i < threadcount; ++i) { + pthread_create(&threadarray[i], NULL, + (void *(*)(void *))increment_func, (void*)i); + } + /* wait for the test threads to finish */ + for (i = 0; i < threadcount; ++i) { + pthread_join(threadarray[i], NULL); + } + /* print the end results of the array */ + for (i = 0; i < ARRAY_SIZE; ++i) { + printf("testarray[%d] is %d\n", i, testarray[i]); + } +} + +static void run_setup(int threadcount) +{ + /* allocate the pthread_t structures */ + threadarray = (pthread_t *) malloc(sizeof(pthread_t)*threadcount); + /* initialize the arraylock mutex */ + if (pthread_mutex_init(&arraylock, NULL) != 0) { + printf("arraylock mutex init failed. exiting ...\n"); + exit(-1); + } +} + +static void run_cleanup(void) +{ + free(threadarray); + pthread_mutex_destroy(&arraylock); +} + +int main(void) +{ + run_setup(TESTTHREAD_COUNT); + run_test(TESTTHREAD_COUNT); + run_cleanup(); + return 0; +}