Subject: Re: p_flag in struct proc: int -> uint64_t
To: None <elad@NetBSD.org>
From: YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
List: tech-kern
Date: 10/04/2006 01:31:13
--NextPart-20061004011314-1565600
Content-Type: Text/Plain; charset=us-ascii
> > Another option would be to provide some generic way for various
> > subsystems to extend the data associated with a process on an as-needed
> > basis... oh, hey, we could add a properties dictionary to the proc
> > structure to get that! :-)
>
> That is... a good idea, actually. But I think you'll have to get more
> than just *my* agreement to proceed with it. :)
although a dictionary in the proc structure might be useful for
some purposes, i'm not sure if i like it as a replacement of
simple integer or pointer members. it seems overkill to me.
i have an implementation of lwp-specific data.
(attached. i wrote it to replace l_emuldata, l_savp, etc.)
proc-specific data can be similar except locking.
YAMAMOTO Takashi
--NextPart-20061004011314-1565600
Content-Type: Text/Plain; charset=us-ascii
Content-Disposition: attachment; filename="lwpspecific.h"
/* $NetBSD$ */
/*-
* 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.
*/
#ifndef _SYS_LWPSPECIFIC_H_
#define _SYS_LWPSPECIFIC_H_
typedef int lwpspecific_key_t;
typedef void (*lwpspecific_dtor_t)(void *);
lwpspecific_key_t lwpspecific_key_alloc(lwpspecific_dtor_t);
void lwpspecific_key_free(lwpspecific_key_t);
void *lwpspecific_get(lwpspecific_key_t);
void lwpspecific_set(lwpspecific_key_t, void *);
struct lwp;
void *lwpspecific_get_remote(struct lwp *, lwpspecific_key_t);
void lwpspecific_set_remote(struct lwp *, lwpspecific_key_t, void *);
void lwpspecific_lwp_exit(void);
#endif /* !_SYS_LWPSPECIFIC_H_ */
--NextPart-20061004011314-1565600
Content-Type: Text/Plain; charset=us-ascii
Content-Disposition: attachment; filename="kern_lwpspecific.c"
/* $NetBSD$ */
/*-
* 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD$");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/lock.h>
#include <sys/lwpspecific.h>
#include <sys/kmem.h>
typedef struct lwpspecific_key_impl {
lwpspecific_dtor_t lki_dtor;
} lwpspecific_key_impl_t;
typedef struct lwpspecific {
size_t ls_nkey;
LIST_ENTRY(lwpspecific) ls_list;
void *ls_data[];
} lwpspecific_t;
#define LWPSPECIFIC__BYTESIZE(sz) \
(sizeof(lwpspecific_t) + (sz) * sizeof(void *))
static struct lock lwpspecific__lock =
LOCK_INITIALIZER(PLOCK, "lwpspecific", 0, 0);
static int lwpspecific__nkey;
static LIST_HEAD(, lwpspecific) lwpspecific__alllist =
LIST_HEAD_INITIALIZER(lwpspecific__alllist);
static lwpspecific_key_impl_t *lwpspecific__keys;
static void
lwpspecific__mutex_enter(void)
{
ASSERT_SLEEPABLE(NULL, __func__);
lockmgr(&lwpspecific__lock, LK_EXCLUSIVE, 0);
}
static void
lwpspecific__mutex_exit(void)
{
lockmgr(&lwpspecific__lock, LK_RELEASE, 0);
}
static void
lwpspecific__link(lwpspecific_t *ls)
{
LIST_INSERT_HEAD(&lwpspecific__alllist, ls, ls_list);
}
static void
lwpspecific__unlink(lwpspecific_t *ls)
{
LIST_REMOVE(ls, ls_list);
}
static void
lwpspecific__destroy(lwpspecific_t *ls, lwpspecific_key_t key)
{
lwpspecific_dtor_t dtor;
void *data;
if (key >= ls->ls_nkey) {
return;
}
data = ls->ls_data[key];
dtor = lwpspecific__keys[key].lki_dtor;
if (dtor != NULL) {
if (data != NULL) {
ls->ls_data[key] = NULL;
(*dtor)(data);
}
} else {
KASSERT(data == NULL);
}
}
static void
lwpspecific__noop_dtor(void *data)
{
/* nothing */
}
/*
* lwpspecific_key_alloc: allocate a key.
*
* supposed to be a very rare operation.
*/
lwpspecific_key_t
lwpspecific_key_alloc(lwpspecific_dtor_t dtor)
{
lwpspecific_key_impl_t *newkeys;
lwpspecific_key_t key;
size_t nsz;
ASSERT_SLEEPABLE(NULL, __func__);
if (dtor == NULL) {
dtor = lwpspecific__noop_dtor;
}
lwpspecific__mutex_enter();
if (lwpspecific__keys == NULL) {
goto needalloc;
}
for (key = 0; key < lwpspecific__nkey; key++) {
if (lwpspecific__keys[key].lki_dtor == NULL) {
goto gotit;
}
}
needalloc:
nsz = (lwpspecific__nkey + 1) * sizeof(lwpspecific_key_impl_t);
newkeys = kmem_zalloc(nsz, KM_SLEEP);
if (lwpspecific__keys != NULL) {
size_t osz = lwpspecific__nkey * sizeof(lwpspecific_key_impl_t);
memcpy(newkeys, lwpspecific__keys, osz);
kmem_free(lwpspecific__keys, osz);
}
lwpspecific__keys = newkeys;
key = lwpspecific__nkey;
lwpspecific__nkey++;
gotit:
lwpspecific__keys[key].lki_dtor = dtor;
lwpspecific__mutex_exit();
return key;
}
/*
* lwpspecific_key_free: free a key.
*
* supposed to be a very rare operation.
*/
void
lwpspecific_key_free(lwpspecific_key_t key)
{
lwpspecific_t *ls;
lwpspecific__mutex_enter();
/*
* traverse all lwp specific data in the system.
*/
LIST_FOREACH(ls, &lwpspecific__alllist, ls_list) {
lwpspecific__destroy(ls, key);
}
lwpspecific__mutex_exit();
}
/*
* lwpspecific_get_remote:
*
* => caller must ensure to exclude concurrent accesses.
*/
void *
lwpspecific_get_remote(struct lwp *l, lwpspecific_key_t key)
{
lwpspecific_t *ls;
ls = l->l_specific;
if (ls != NULL && key < ls->ls_nkey) {
return ls->ls_data[key];
}
return NULL;
}
/*
* lwpspecific_get: get curlwp's lwp specific data for the given key.
*/
void *
lwpspecific_get(lwpspecific_key_t key)
{
return lwpspecific_get_remote(curlwp, key);
}
/*
* lwpspecific_set_remote:
*
* => caller must ensure to exclude concurrent accesses.
*/
void
lwpspecific_set_remote(struct lwp *l, lwpspecific_key_t key, void *data)
{
lwpspecific_t *ls;
lwpspecific_t *newls;
int newnkey;
size_t sz;
ASSERT_SLEEPABLE(NULL, __func__);
ls = l->l_specific;
if (__predict_true(ls != NULL && key < ls->ls_nkey)) {
ls->ls_data[key] = data;
return;
}
/* slow path: need to resize */
lwpspecific__mutex_enter();
newnkey = lwpspecific__nkey;
if (key >= newnkey) {
lwpspecific__mutex_exit();
panic("lwpspecific_set");
}
sz = LWPSPECIFIC__BYTESIZE(newnkey);
newls = kmem_zalloc(sz, KM_SLEEP);
newls->ls_nkey = newnkey;
if (ls != NULL) {
lwpspecific__unlink(ls);
memcpy(newls->ls_data, ls->ls_data, ls->ls_nkey);
kmem_free(ls, LWPSPECIFIC__BYTESIZE(ls->ls_nkey));
}
newls->ls_data[key] = data;
lwpspecific__link(newls);
l->l_specific = newls;
lwpspecific__mutex_exit();
}
/*
* lwpspecific_set: set curlwp's lwp specific data for the given key.
*/
void
lwpspecific_set(lwpspecific_key_t key, void *data)
{
lwpspecific_set_remote(curlwp, key, data);
}
/*
* lwpspecific_lwp_exit: free lwp specific data on lwp exit.
*/
void
lwpspecific_lwp_exit(void)
{
struct lwp *l;
lwpspecific_t *ls;
lwpspecific_key_t key;
ASSERT_SLEEPABLE(NULL, __func__);
l = curlwp;
ls = l->l_specific;
if (ls == NULL) {
return;
}
l->l_specific = NULL;
lwpspecific__mutex_enter();
lwpspecific__unlink(ls);
for (key = 0; key < ls->ls_nkey; key++) {
lwpspecific__destroy(ls, key);
}
lwpspecific__mutex_exit();
kmem_free(ls, LWPSPECIFIC__BYTESIZE(ls->ls_nkey));
}
--NextPart-20061004011314-1565600--