/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Kernel protection serializers: general purpose synchronization mechanism.
*
* Serializers provide a simple way to serialize access to some resource. They
* can be used as an alternative to locks or STREAMS perimeters. They scale
* much better than STREAMS outer serializers.
*
* Serializer is an abstraction that guarantees that all functions executed
* within the serializer are serialized: they are executed in the order they
* entered serializer one at a time.
*
* INTERFACES:
*
* serializer_t *serializer_create(flags);
*
* Create a serializer. The flags may be either SER_SLEEP or SER_NOSLEEP
* which are the same as KM_SLEEP and KM_NOSLEEP respectively.
*
* serializer_enter(serializer, proc, mblk, arg);
*
* Execute 'proc(mblk, arg)' within the serializer.
*
* serializer_wait(serializer);
*
* Wait for pending serializer jobs to complete. This function should never
* be called within the serializer or it will deadlock.
*
* serializer_destroy(serializer);
*
* Destroy serializer.
*
* Serializers export three DTrace SDT probes:
*
* serializer-enqueue(serializer, mblk, arg, proc)
*
* The probe triggers when serializer is busy and the request is
* queued.
*
* serializer-exec-start(serializer, mblk, arg, proc)
*
* The probe triggers before the request is executed
*
* serializer-exec-end(serializer, mblk, arg, proc)
*
* The probe triggers after the request is executed
*
*
* IMPLEMENTATION.
*
* Serializer consists of a "owner" and a list of queued jobs. The first thread
* entering serializer sets the owner and executes its job directly without
* context switch. Then it processes jobs which may have been enqueued while it
* was executing a job and drops the owner, leaving the serializer empty. Any
* thread entering an owned serializer enqueues its job and returns immediately.
*
* Serializer data structure holds several fields used for debugging only. They
* are not relevant for the proper serializer functioning.
*
* When new requests arrive faster then they are processed it is possible that a
* thread that started processing serializer will continue doing so for a long
* time. To avoid such pathological behavior the amount of requests drained by
* serializer_enter() is limited by `serializer_credit' value. After the credit
* is expired serializer_enter() schedules a taskq request to continue draining.
* The taskq thread draining is not limited by serializer_credit. Note that it
* is possible that another serializer_enter() will drain the serializer before
* a taskq thread will get to it.
*/
#include <sys/serializer.h>
/*
* Serializer abstraction.
* Fields marked (D) are used for debugging purposes only.
*/
struct serializer_s {
};
/*
* How many drains are allowed before we switch to taskq processing.
*/
/* Statistics for debugging */
static int perim_context_swtch = 0;
static int serializer_constructor(void *, void *, int);
static void serializer_destructor(void *, void *);
static void serializer_drain(serializer_t *, int);
static void serializer_drain_completely(serializer_t *);
/*
* SERIALIZER Implementation.
*/
/*
* Record debugging information and execute single request.
*/
static void
{
}
/*
* Enqueue a single request on serializer.
*/
static void
{
s->ser_count++;
else
}
/*
* Drain serializer, limiting drain to `credit' requests at most.
*/
static void
{
} else {
}
s->ser_count--;
mutex_exit(&s->ser_lock);
mutex_enter(&s->ser_lock);
}
}
/*
* Drain serializer completely if serializer is free.
*/
static void
{
mutex_enter(&s->ser_lock);
serializer_drain(s, INT_MAX);
}
/*
* Wake up serializer_wait().
*/
mutex_exit(&s->ser_lock);
}
/*
* Call proc(mp, arg) within serializer.
*
* If serializer is empty and not owned, proc(mp, arg) is called right
* away. Otherwise the request is queued.
*/
void
{
mutex_enter(&s->ser_lock);
/*
* Serializer is owned. Enqueue and return.
*/
} else {
/*
* If the request list is empty, can process right away,
* otherwise enqueue and process.
*/
} else {
mutex_exit(&s->ser_lock);
/*
* Execute request
*/
mutex_enter(&s->ser_lock);
}
/*
* Drain whatever has arrived in the meantime.
* If we spend too much time draining, continue draining by the
* taskq thread.
*/
/*
* If there is a taskq pending for this
* serializer, no need to schedule a new one.
*/
if (s->ser_taskq) {
break;
} else {
(task_func_t *)
s, TQ_NOSLEEP | TQ_NOQUEUE);
if (tid != 0)
}
}
}
}
/*
* Wakeup serializer_wait().
*/
mutex_exit(&s->ser_lock);
}
/*
* Wait for pending serializer jobs to complete. This function should never be
* called within the serializer or it will deadlock.
*/
void
{
mutex_enter(&s->ser_lock);
/*
* Wakeup other potential waiters.
*/
mutex_exit(&s->ser_lock);
}
/*
* Create a new serializer.
*/
{
}
/*
* Wait for all pending entries to drain and then destroy serializer.
*/
void
{
serializer_wait(s);
}
/*ARGSUSED*/
static int
{
serializer_t *s = buf;
s->ser_taskq = 0;
s->ser_count = 0;
return (0);
}
/*ARGSUSED*/
static void
{
serializer_t *s = buf;
mutex_destroy(&s->ser_lock);
cv_destroy(&s->ser_cv);
}
void
serializer_init(void)
{
sizeof (serializer_t), 0, serializer_constructor,
}