/*-
* Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 2006 YAMAMOTO Takashi.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Locking notes:
*
* The specdataref_container pointer in the specificdata_reference
* is volatile. To read it, you must hold EITHER the domain lock
* or the ref lock. To write it, you must hold BOTH the domain lock
* and the ref lock. The locks must be acquired in the following
* order:
* domain -> ref
*/
/*
* specificdata_key_delete --
* Destroy a specificdata key for a domain.
*
* Note: This is a rare operation.
*/
void
specificdata_key_delete(specificdata_domain_t sd, specificdata_key_t key)
{
specificdata_container_t sc;
mutex_enter(&sd->sd_lock);
if (key >= sd->sd_nkey)
goto out;
/*
* Traverse all of the specificdata containers in the domain
* and the destroy the datum for the dying key.
*/
LIST_FOREACH(sc, &sd->sd_list, sc_list) {
specificdata_destroy_datum(sd, sc, key);
}
sd->sd_keys[key].ski_dtor = NULL;
out:
mutex_exit(&sd->sd_lock);
}
/*
* specificdata_init --
* Initialize a specificdata container for operation in the
* specified domain.
*/
int
specificdata_init(specificdata_domain_t sd, specificdata_reference *ref)
{
/*
* Just NULL-out the container pointer; we'll allocate the
* container the first time specificdata is put into it.
*/
ref->specdataref_container = NULL;
mutex_init(&ref->specdataref_lock, MUTEX_DEFAULT, IPL_NONE);
return (0);
}
/*
* specificdata_fini --
* Destroy a specificdata container. We destroy all of the datums
* stuffed into the container just as if the key were destroyed.
*/
void
specificdata_fini(specificdata_domain_t sd, specificdata_reference *ref)
{
specificdata_container_t sc;
specificdata_key_t key;
/*
* specificdata_getspecific --
* Get a datum from a container.
*/
void *
specificdata_getspecific(specificdata_domain_t sd, specificdata_reference *ref,
specificdata_key_t key)
{
specificdata_container_t sc;
void *data = NULL;
mutex_enter(&ref->specdataref_lock);
sc = ref->specdataref_container;
if (sc != NULL && key < sc->sc_nkey)
data = sc->sc_data[key];
mutex_exit(&ref->specdataref_lock);
return (data);
}
/*
* specificdata_getspecific_unlocked --
* Get a datum from a container in a lockless fashion.
*
* Note: When using this routine, care must be taken to ensure
* that no other thread could cause the specificdata_reference
* to become invalid (i.e. point at the wrong container) by
* issuing a setspecific call or destroying the container.
*/
void *
specificdata_getspecific_unlocked(specificdata_domain_t sd,
specificdata_reference *ref,
specificdata_key_t key)
{
specificdata_container_t sc;
sc = ref->specdataref_container;
if (sc != NULL) {
if (key < sc->sc_nkey) {
/*
* Someone beat us to the punch. Unwind and put
* the object into the now large enough container.
*/
sc->sc_data[key] = data;
mutex_exit(&ref->specdataref_lock);
mutex_exit(&sd->sd_lock);
kmem_free(newsc, sz);
return;
}
specificdata_container_unlink(sd, sc);
memcpy(newsc->sc_data, sc->sc_data,
sc->sc_nkey * sizeof(void *));
}
newsc->sc_data[key] = data;
specificdata_container_link(sd, newsc);
ref->specdataref_container = newsc;