6f3e57ac9d0b054c3169579f3422080b8ba10105mx/*
47693af92e50a1ad81825eb01b7157a211269613mx * CDDL HEADER START
47693af92e50a1ad81825eb01b7157a211269613mx *
47693af92e50a1ad81825eb01b7157a211269613mx * The contents of this file are subject to the terms of the
47693af92e50a1ad81825eb01b7157a211269613mx * Common Development and Distribution License (the "License").
47693af92e50a1ad81825eb01b7157a211269613mx * You may not use this file except in compliance with the License.
47693af92e50a1ad81825eb01b7157a211269613mx *
47693af92e50a1ad81825eb01b7157a211269613mx * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
47693af92e50a1ad81825eb01b7157a211269613mx * or http://www.opensolaris.org/os/licensing.
47693af92e50a1ad81825eb01b7157a211269613mx * See the License for the specific language governing permissions
47693af92e50a1ad81825eb01b7157a211269613mx * and limitations under the License.
47693af92e50a1ad81825eb01b7157a211269613mx *
47693af92e50a1ad81825eb01b7157a211269613mx * When distributing Covered Code, include this CDDL HEADER in each
47693af92e50a1ad81825eb01b7157a211269613mx * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
47693af92e50a1ad81825eb01b7157a211269613mx * If applicable, add the following below this CDDL HEADER, with the
47693af92e50a1ad81825eb01b7157a211269613mx * fields enclosed by brackets "[]" replaced with your own identifying
47693af92e50a1ad81825eb01b7157a211269613mx * information: Portions Copyright [yyyy] [name of copyright owner]
47693af92e50a1ad81825eb01b7157a211269613mx *
47693af92e50a1ad81825eb01b7157a211269613mx * CDDL HEADER END
6f3e57ac9d0b054c3169579f3422080b8ba10105mx */
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx/*
47693af92e50a1ad81825eb01b7157a211269613mx * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
47693af92e50a1ad81825eb01b7157a211269613mx * Use is subject to license terms.
6f3e57ac9d0b054c3169579f3422080b8ba10105mx */
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx#include "nge.h"
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx/*
6f3e57ac9d0b054c3169579f3422080b8ba10105mx * Atomically decrement a counter, but only if it will remain
6f3e57ac9d0b054c3169579f3422080b8ba10105mx * positive (>=0) afterwards.
6f3e57ac9d0b054c3169579f3422080b8ba10105mx */
6f3e57ac9d0b054c3169579f3422080b8ba10105mxboolean_t
6f3e57ac9d0b054c3169579f3422080b8ba10105mxnge_atomic_decrease(uint64_t *count_p, uint64_t n)
6f3e57ac9d0b054c3169579f3422080b8ba10105mx{
6f3e57ac9d0b054c3169579f3422080b8ba10105mx uint64_t oldval;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx uint64_t newval;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx /* ATOMICALLY */
6f3e57ac9d0b054c3169579f3422080b8ba10105mx do {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx oldval = *count_p;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx newval = oldval - n;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx if (oldval < n)
6f3e57ac9d0b054c3169579f3422080b8ba10105mx return (B_FALSE);
75d94465dbafa487b716482dc36d5150a4ec9853Josef 'Jeff' Sipek } while (atomic_cas_64(count_p, oldval, newval) != oldval);
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx return (B_TRUE);
6f3e57ac9d0b054c3169579f3422080b8ba10105mx}
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx/*
6f3e57ac9d0b054c3169579f3422080b8ba10105mx * Atomically increment a counter
6f3e57ac9d0b054c3169579f3422080b8ba10105mx */
6f3e57ac9d0b054c3169579f3422080b8ba10105mxvoid
6f3e57ac9d0b054c3169579f3422080b8ba10105mxnge_atomic_increase(uint64_t *count_p, uint64_t n)
6f3e57ac9d0b054c3169579f3422080b8ba10105mx{
6f3e57ac9d0b054c3169579f3422080b8ba10105mx uint64_t oldval;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx uint64_t newval;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx /* ATOMICALLY */
6f3e57ac9d0b054c3169579f3422080b8ba10105mx do {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx oldval = *count_p;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx newval = oldval + n;
75d94465dbafa487b716482dc36d5150a4ec9853Josef 'Jeff' Sipek } while (atomic_cas_64(count_p, oldval, newval) != oldval);
6f3e57ac9d0b054c3169579f3422080b8ba10105mx}
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx/*
6f3e57ac9d0b054c3169579f3422080b8ba10105mx * Atomically shift a 32-bit word left, returning
6f3e57ac9d0b054c3169579f3422080b8ba10105mx * the value it had *before* the shift was applied
6f3e57ac9d0b054c3169579f3422080b8ba10105mx */
6f3e57ac9d0b054c3169579f3422080b8ba10105mxuint32_t
6f3e57ac9d0b054c3169579f3422080b8ba10105mxnge_atomic_shl32(uint32_t *sp, uint_t count)
6f3e57ac9d0b054c3169579f3422080b8ba10105mx{
6f3e57ac9d0b054c3169579f3422080b8ba10105mx uint32_t oldval;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx uint32_t newval;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx /* ATOMICALLY */
6f3e57ac9d0b054c3169579f3422080b8ba10105mx do {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx oldval = *sp;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx newval = oldval << count;
75d94465dbafa487b716482dc36d5150a4ec9853Josef 'Jeff' Sipek } while (atomic_cas_32(sp, oldval, newval) != oldval);
6f3e57ac9d0b054c3169579f3422080b8ba10105mx
6f3e57ac9d0b054c3169579f3422080b8ba10105mx return (oldval);
6f3e57ac9d0b054c3169579f3422080b8ba10105mx}