0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms
0N/A * of the Common Development and Distribution License
0N/A * (the "License"). You may not use this file except
0N/A * in compliance with the License.
0N/A *
0N/A * You can obtain a copy of the license at
0N/A * src/OPENSOLARIS.LICENSE
0N/A * or http://www.opensolaris.org/os/licensing.
0N/A * See the License for the specific language governing
0N/A * permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL
0N/A * HEADER in each file and include the License file at
0N/A * usr/src/OPENSOLARIS.LICENSE. If applicable,
0N/A * add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your
0N/A * own identifying information: Portions Copyright [yyyy]
0N/A * [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/A/*
0N/A * The "cascade" test case is a multiprocess/multithread batten-passing model
0N/A * using lock primitives alone for synchronisation. Threads are arranged in a
0N/A * ring. Each thread has two locks of its own on which it blocks, and is able
0N/A * to manipulate the two locks belonging to the thread which follows it in the
0N/A * ring.
0N/A *
0N/A * The number of threads (nthreads) is specified by the generic libMicro -P/-T
0N/A * options. With nthreads == 1 (the default) the uncontended case can be timed.
0N/A *
0N/A * The main logic is generic and allows any simple blocking API to be tested.
0N/A * The API-specific component is clearly indicated.
0N/A */
0N/A
0N/A#include <unistd.h>
0N/A#include <stdlib.h>
0N/A#include <stdio.h>
0N/A#include <pthread.h>
0N/A#include <sys/mman.h>
0N/A
0N/A#include "libmicro.h"
0N/A
0N/Atypedef struct {
0N/A int ts_once;
0N/A int ts_id;
0N/A int ts_us0; /* our lock indices */
0N/A int ts_us1;
0N/A int ts_them0; /* their lock indices */
0N/A int ts_them1;
0N/A} tsd_t;
0N/A
0N/Astatic int nthreads;
0N/A
0N/A/*
0N/A * API-specific code BEGINS here
0N/A */
0N/A
0N/Astatic int opto = 0;
0N/Astatic int opts = 0;
0N/Astatic int nlocks;
0N/Astatic pthread_mutex_t *mxs;
0N/Astatic pthread_cond_t *cvs;
0N/Astatic int *conds;
0N/A
0N/Aint
0N/Abenchmark_init()
0N/A{
0N/A lm_tsdsize = sizeof (tsd_t);
0N/A
0N/A (void) sprintf(lm_optstr, "os");
0N/A
0N/A lm_defN = "cscd_cond";
0N/A
0N/A (void) sprintf(lm_usage,
0N/A " [-o] (do signal outside mutex)\n"
0N/A " [-s] (force PTHREAD_PROCESS_SHARED)\n"
0N/A "notes: thread cascade using pthread_conds\n");
0N/A
0N/A return (0);
0N/A}
0N/A
0N/A/*ARGSUSED*/
0N/Aint
0N/Abenchmark_optswitch(int opt, char *optarg)
0N/A{
0N/A switch (opt) {
0N/A case 'o':
0N/A opto = 1;
0N/A break;
0N/A case 's':
0N/A opts = 1;
0N/A break;
0N/A default:
0N/A return (-1);
0N/A }
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Abenchmark_initrun()
0N/A{
0N/A int i;
0N/A int e = 0;
0N/A pthread_mutexattr_t ma;
0N/A pthread_condattr_t ca;
0N/A
0N/A nthreads = lm_optP * lm_optT;
0N/A nlocks = nthreads * 2;
0N/A /*LINTED*/
0N/A mxs = (pthread_mutex_t *)mmap(NULL,
0N/A nlocks * sizeof (pthread_mutex_t),
0N/A PROT_READ | PROT_WRITE,
0N/A MAP_ANON | MAP_SHARED,
0N/A -1, 0L);
0N/A if (mxs == MAP_FAILED) {
0N/A return (1);
0N/A }
0N/A
0N/A /*LINTED*/
0N/A cvs = (pthread_cond_t *)mmap(NULL,
0N/A nlocks * sizeof (pthread_cond_t),
0N/A PROT_READ | PROT_WRITE,
0N/A MAP_ANON | MAP_SHARED,
0N/A -1, 0L);
0N/A if (cvs == MAP_FAILED) {
0N/A return (1);
0N/A }
0N/A
0N/A /*LINTED*/
0N/A conds = (int *)mmap(NULL,
0N/A nlocks * sizeof (pthread_cond_t),
0N/A PROT_READ | PROT_WRITE,
0N/A MAP_ANON | MAP_SHARED,
0N/A -1, 0L);
0N/A if (conds == MAP_FAILED) {
0N/A return (1);
0N/A }
0N/A
0N/A (void) pthread_mutexattr_init(&ma);
0N/A (void) pthread_condattr_init(&ca);
0N/A if (lm_optP > 1 || opts) {
0N/A (void) pthread_mutexattr_setpshared(&ma,
0N/A PTHREAD_PROCESS_SHARED);
0N/A (void) pthread_condattr_setpshared(&ca,
0N/A PTHREAD_PROCESS_SHARED);
0N/A } else {
0N/A (void) pthread_mutexattr_setpshared(&ma,
0N/A PTHREAD_PROCESS_PRIVATE);
0N/A (void) pthread_condattr_setpshared(&ca,
0N/A PTHREAD_PROCESS_PRIVATE);
0N/A }
0N/A
0N/A for (i = 0; i < nlocks; i++) {
0N/A (void) pthread_mutex_init(&mxs[i], &ma);
0N/A (void) pthread_cond_init(&cvs[i], &ca);
0N/A conds[i] = 0;
0N/A }
0N/A
0N/A return (e);
0N/A}
0N/A
0N/Aint
0N/Ablock(int index)
0N/A{
0N/A (void) pthread_mutex_lock(&mxs[index]);
0N/A while (conds[index] != 0) {
0N/A (void) pthread_cond_wait(&cvs[index], &mxs[index]);
0N/A }
0N/A conds[index] = 1;
0N/A (void) pthread_mutex_unlock(&mxs[index]);
0N/A
0N/A return (0);
0N/A}
0N/A
0N/Aint
0N/Aunblock(int index)
0N/A{
0N/A (void) pthread_mutex_lock(&mxs[index]);
0N/A conds[index] = 0;
0N/A if (opto) {
0N/A (void) pthread_mutex_unlock(&mxs[index]);
0N/A (void) pthread_cond_signal(&cvs[index]);
0N/A } else {
0N/A (void) pthread_cond_signal(&cvs[index]);
0N/A (void) pthread_mutex_unlock(&mxs[index]);
0N/A }
0N/A return (0);
0N/A}
0N/A
0N/A/*
0N/A * API-specific code ENDS here
0N/A */
0N/A
0N/Aint
0N/Abenchmark_initbatch(void *tsd)
0N/A{
0N/A tsd_t *ts = (tsd_t *)tsd;
0N/A int e = 0;
0N/A
0N/A if (ts->ts_once == 0) {
0N/A int us, them;
0N/A
0N/A us = (getpindex() * lm_optT) + gettindex();
0N/A them = (us + 1) % (lm_optP * lm_optT);
0N/A
0N/A ts->ts_id = us;
0N/A
0N/A /* lock index asignment for us and them */
0N/A ts->ts_us0 = (us * 2);
0N/A ts->ts_us1 = (us * 2) + 1;
0N/A if (us < nthreads - 1) {
0N/A /* straight-thru connection to them */
0N/A ts->ts_them0 = (them * 2);
0N/A ts->ts_them1 = (them * 2) + 1;
0N/A } else {
0N/A /* cross-over connection to them */
0N/A ts->ts_them0 = (them * 2) + 1;
0N/A ts->ts_them1 = (them * 2);
0N/A }
0N/A
0N/A ts->ts_once = 1;
0N/A }
0N/A
0N/A /* block their first move */
0N/A e += block(ts->ts_them0);
0N/A
0N/A return (e);
0N/A}
0N/A
0N/Aint
0N/Abenchmark(void *tsd, result_t *res)
0N/A{
0N/A tsd_t *ts = (tsd_t *)tsd;
0N/A int i;
0N/A int e = 0;
0N/A
0N/A /* wait to be unblocked (id == 0 will not block) */
0N/A e += block(ts->ts_us0);
0N/A
0N/A for (i = 0; i < lm_optB; i += 2) {
0N/A /* allow them to block us again */
0N/A e += unblock(ts->ts_us0);
0N/A
0N/A /* block their next + 1 move */
0N/A e += block(ts->ts_them1);
0N/A
0N/A /* unblock their next move */
0N/A e += unblock(ts->ts_them0);
0N/A
0N/A /* wait for them to unblock us */
0N/A e += block(ts->ts_us1);
0N/A
0N/A /* repeat with locks reversed */
0N/A e += unblock(ts->ts_us1);
0N/A e += block(ts->ts_them0);
0N/A e += unblock(ts->ts_them1);
0N/A e += block(ts->ts_us0);
0N/A }
0N/A
0N/A /* finish batch with nothing blocked */
0N/A e += unblock(ts->ts_them0);
0N/A e += unblock(ts->ts_us0);
0N/A
0N/A res->re_count = i;
0N/A res->re_errors = e;
0N/A
0N/A return (0);
0N/A}