port.c revision 3348528f7ec68bf2f11d0cbd5c3b9932ea7f0d5c
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/vfs_opreg.h>
#include <sys/sysmacros.h>
#include <sys/port_impl.h>
/*
* Event Ports can be shared across threads or across processes.
* can use a single port. A major request was also to get the ability
* to submit user-defined events to a port. The idea of the
* user-defined events is to use the event ports for communication between
* in a port with the same priority as other event types.
*
* for events with the "highest priority" (priority here is related to the
* internal strategy to wakeup waiting threads) will retrieve the event,
* the requirement to have events which should be submitted immediately
* to all "waiting" threads. That is the main task of the alert event.
* The alert event is submitted by the application to a port. The port
* changes from a standard mode to the alert mode. Now all waiting threads
* will be awaken immediately and they will return with the alert event.
* Threads trying to retrieve events from a port in alert mode will
* return immediately with the alert event.
*
*
* An event port is like a kernel queue, which accept events submitted from
* user level as well as events submitted from kernel sub-systems. Sub-systems
* able to submit events to a port are the so-called "event sources".
* Current event sources:
* PORT_SOURCE_AIO : events submitted per transaction completion from
* POSIX-I/O framework.
* PORT_SOURCE_TIMER : events submitted when a timer fires
* (see timer_create(3RT)).
* PORT_SOURCE_FD : events submitted per file descriptor (see poll(2)).
* PORT_SOURCE_ALERT : events submitted from user. This is not really a
* single event, this is actually a port mode
* (see port_alert(3c)).
* PORT_SOURCE_USER : events submitted by applications with
* port_send(3c) or port_sendn(3c).
*
* There is a user API implemented in the libc library as well as a
* kernel API implemented in port_subr.c in genunix.
* The available user API functions are:
* port_create() : create a port as a file descriptor of portfs file system
* The standard close(2) function closes a port.
* port_associate() : associate a file descriptor with a port to be able to
* retrieve events from that file descriptor.
* port_dissociate(): remove the association of a file descriptor with a port.
* port_send() : send an event of type PORT_SOURCE_USER to a port
* port_sendn() : send an event of type PORT_SOURCE_USER to a list of ports
* port_get() : retrieve a single event from a port
* port_getn() : retrieve a list of events from a port
*
* The available kernel API functions are:
* port_init_event() : set event data in the event structure
* port_send_event() : send event to a port
* port_associate_ksource(): associate a kernel event source with a port
* port_dissociate_ksource(): dissociate a kernel event source from a port
*
* The libc implementation consists of small functions which pass the
* arguments to the kernel using the "portfs" system call. It means, all the
* synchronisation work is being done in the kernel. The "portfs" system
* call loads the portfs file system into the kernel.
*
* PORT CREATION
* The first function to be used is port_create() which internally creates
* a vnode and a portfs node. The portfs node is represented by the port_t
* structure, which again includes all the data necessary to control a port.
* port_create() returns a file descriptor, which needs to be used in almost
* all other event port functions.
* The maximum number of ports per system is controlled by the resource
* control: project:port-max-ids.
*
* EVENT GENERATION
* The second step is the triggering of events, which could be sent to a port.
* Every event source implements an own method to generate events for a port:
* PORT_SOURCE_AIO:
* The sigevent structure of the standard POSIX-IO functions
* was extended by an additional notification type.
* Standard notification types:
* SIGEV_NONE, SIGEV_SIGNAL and SIGEV_THREAD
* Event ports introduced now SIGEV_PORT.
* The notification type SIGEV_PORT specifies that a structure
* of type port_notify_t has to be attached to the sigev_value.
* The port_notify_t structure contains the event port file
* descriptor and a user-defined pointer.
* Internally the AIO implementation will use the kernel API
* functions to allocate an event port slot per transaction (aiocb)
* and sent the event to the port as soon as the transaction completes.
* All the events submitted per transaction are of type
* PORT_SOURCE_AIO.
* PORT_SOURCE_TIMER:
* The timer_create() function uses the same method as the
* PORT_SOURCE_AIO event source. It also uses the sigevent structure
* to deliver the port information.
* per timer and it will send the timer event as soon as the timer
* fires. If the timer-fired event is not delivered to the application
* before the next period elapsed, then an overrun counter will be
* incremented. The timer event source uses a callback function to
* detect the delivery of the event to the application. At that time
* the timer callback function will update the event overrun counter.
* PORT_SOURCE_FD:
* This event source uses the port_associate() function to allocate
* events argument of port_associate() the type of events which it is
* interested on.
* The internal pollwakeup() function is used by all the file
* systems --which are supporting the VOP_POLL() interface- to notify
* the upper layer (poll(2), devpoll(7d) and now event ports) about
* the event triggered (see valid events in poll(2)).
* The pollwakeup() function forwards the event to the layer registered
* to receive the current event.
* The port_dissociate() function can be used to free the allocated
* event slot from the port. Anyway, file descriptors deliver events
* only one time and remain deactivated until the application
* reactivates the association of a file descriptor with port_associate().
* If an associated file descriptor is closed then the file descriptor
* will be dissociated automatically from the port.
*
* PORT_SOURCE_ALERT:
* This event type is generated when the port was previously set in
* alert mode using the port_alert() function.
* A single alert event is delivered to every thread which tries to
* retrieve events from a port.
* PORT_SOURCE_USER:
* This type of event is generated from user level using the port_send()
* function to send a user event to a port or the port_sendn() function
* to send an event to a list of ports.
*
* EVENT DELIVERY / RETRIEVING EVENTS
* Events remain in the port queue until:
* - the application uses port_get() or port_getn() to retrieve events,
* - the event source cancel the event,
* - the event port is closed or
* - the process exits.
* The maximal number of events in a port queue is the maximal number
* of event slots/structures which can be allocated by event sources.
* The allocation of event slots/structures is controlled by the resource
* control: process.port-max-events.
* The port_get() function retrieves a single event and the port_getn()
* function retrieves a list of events.
* Events are classified as shareable and non-shareable events across processes.
* Non-shareable events are invisible for the port_get(n)() functions of
* processes other than the owner of the event.
* Shareable event types are:
* PORT_SOURCE_USER events
* This type of event is unconditionally shareable and without
* limitations. If the parent process sends a user event and closes
* the port afterwards, the event remains in the port and the child
* process will still be able to retrieve the user event.
* PORT_SOURCE_ALERT events
* This type of event is shareable between processes.
* Limitation: The alert mode of the port is removed if the owner
* (process which set the port in alert mode) of the
* alert event closes the port.
* PORT_SOURCE_FD events
* This type of event is conditional shareable between processes.
* After fork(2) all forked file descriptors are shareable between
* the processes. The child process is allowed to retrieve events
* from the associated file descriptors and it can also re-associate
* the fd with the port.
* Limitations: The child process is not allowed to dissociate
* the file descriptor from the port. Only the
* owner (process) of the association is allowed to
* dissociate the file descriptor from the port.
* If the owner of the association closes the port
* the association will be removed.
* PORT_SOURCE_AIO events
* This type of event is not shareable between processes.
* PORT_SOURCE_TIMER events
* This type of event is not shareable between processes.
*
* FORK BEHAVIOUR
* On fork(2) the child process inherits all opened file descriptors from
* the parent process. This is also valid for port file descriptors.
* Associated file descriptors with a port maintain the association across the
* fork(2). It means, the child process gets full access to the port and
* it can retrieve events from all common associated file descriptors.
* Events of file descriptors created and associated with a port after the
* fork(2) are non-shareable and can only be retrieved by the same process.
*
* If the parent or the child process closes an exported port (using fork(2)
* or I_SENDFD) all the file descriptors associated with the port by the
* process will be dissociated from the port. Events of dissociated file
* descriptors as well as all non-shareable events will be discarded.
* The other process can continue working with the port as usual.
*
* CLOSING A PORT
* close(2) has to be used to close a port. See FORK BEHAVIOUR for details.
*
* PORT EVENT STRUCTURES
* The global control structure of the event ports framework is port_control_t.
* port_control_t keeps track of the number of created ports in the system.
* The cache of the port event structures is also located in port_control_t.
*
* On port_create() the vnode and the portfs node is also created.
* The portfs node is represented by the port_t structure.
* The port_t structure manages all port specific tasks:
* - management of resource control values
* - port VOP_POLL interface
* - creation time
* - uid and gid of the port
*
* The port_t structure contains the port_queue_t structure.
* The port_queue_t structure contains all the data necessary for the
* queue management:
* - locking
* - condition variables
* - event counters
* - submitted events (represented by port_kevent_t structures)
* - threads waiting for event delivery (check portget_t structure)
* - PORT_SOURCE_FD cache (managed by the port_fdcache_t structure)
* - event source management (managed by the port_source_t structure)
* - alert mode management (check port_alert_t structure)
*
* EVENT MANAGEMENT
* The event port file system creates a kmem_cache for internal allocation of
* event port structures.
*
* 1. Event source association with a port:
* The first step to do for event sources is to get associated with a port
* using the port_associate_ksource() function or adding an entry to the
* port_ksource_tab[]. An event source can get dissociated from a port
* using the port_dissociate_ksource() function. An entry in the
* port_ksource_tab[] implies that the source will be associated
* automatically with every new created port.
* The event source can deliver a callback function, which is used by the
* port to notify the event source about close(2). The idea is that
* in such a case the event source should free all allocated resources
* and it must return to the port all allocated slots/structures.
* The port_close() function will wait until all allocated event
* structures/slots are returned to the port.
* The callback function is not necessary when the event source does not
* maintain local resources, a second condition is that the event source
* can guarantee that allocated event slots will be returned without
* delay to the port (it will not block and sleep somewhere).
*
* 2. Reservation of an event slot / event structure
* The event port reliability is based on the reservation of an event "slot"
* (allocation of an event structure) by the event source as part of the
* application call. If the maximal number of event slots is exhausted then
* the event source can return a corresponding error code to the application.
*
* The port_alloc_event() function has to be used by event sources to
* allocate an event slot (reserve an event structure). The port_alloc_event()
* doesn not block and it will return a 0 value on success or an error code
* if it fails.
* An argument of port_alloc_event() is a flag which determines the behavior
* of the event after it was delivered to the application:
* PORT_ALLOC_DEFAULT : event slot becomes free after delivery to the
* application.
* PORT_ALLOC_PRIVATE : event slot remains under the control of the event
* source. This kind of slots can not be used for
* event delivery and should only be used internally
* by the event source.
* PORT_KEV_CACHED : event slot remains under the control of an event
* port cache. It does not become free after delivery
* to the application.
* PORT_ALLOC_SCACHED : event slot remains under the control of the event
* source. The event source takes the control over
* the slot after the event is delivered to the
* application.
*
* 3. Delivery of events to the event port
* event data to the port. Event source has to use the function
* port_send_event(). The single argument is a pointer to the previously
* in two ways:
* 1. using the port_set_event() function, or
* 2. updating the portkev_events field out of the callback function:
* The event source can deliver a callback function to the port as an
* argument of port_init_event().
* One of the arguments of the callback function is a pointer to the
* events field, which will be delivered to the application.
* (see Delivery of events to the application).
* Event structures/slots can be delivered to the event port only one time,
* they remain blocked until the data is delivered to the application and the
* slot becomes free or it is delivered back to the event source
* (PORT_ALLOC_SCACHED). The activation of the callback function mentioned above
* is at the same time the indicator for the event source that the event
*
* 4. Delivery of events to the application
* The events structures/slots delivered by event sources remain in the
* port queue until they are retrieved by the application or the port
* is closed (exit(2) also closes all opened file descriptors)..
* The application uses port_get() or port_getn() to retrieve events from
* retrieves a list of event structures/slots.
* Both functions are able to poll for events and return immediately or they
* can specify a timeout value.
* Before the events are delivered to the application they are moved to a
* second temporary internal queue. The idea is to avoid lock collisions or
* contentions of the global queue lock.
* The global queue lock is used every time when an event source delivers
* new events to the port.
* The port_get() and port_getn() functions
* a) retrieve single events from the temporary queue,
* b) prepare the data to be passed to the application memory,
* c) activate the callback function of the event sources:
* - to get the latest event data,
* - the event source can free all allocated resources associated with the
* current event,
* - the event source can deny the delivery of the event to the application
* (e.g. because of the wrong process).
* d) put the event back to the temporary queue if the event delivery was denied
* e) repeat a) until d) as long as there are events in the queue and
* there is enough user space available.
*
* The loop described above could block for a very long time the global mutex,
* to avoid that a second mutex was introduced to synchronized concurrent
* threads accessing the temporary queue.
*/
static struct sysent port_sysent = {
6,
(int (*)())portfs,
};
};
#ifdef _SYSCALL32_IMPL
static int64_t
static struct sysent port_sysent32 = {
6,
(int (*)())portfs32,
};
"32-bit event ports syscalls",
};
#endif /* _SYSCALL32_IMPL */
static struct modlinkage modlinkage = {
&modlsys,
#ifdef _SYSCALL32_IMPL
#endif
};
{ "ports", KSTAT_DATA_UINT32 }
};
struct vnodeops *port_vnodeops;
extern rctl_hndl_t rc_process_portev;
extern rctl_hndl_t rc_project_portids;
extern void aio_close_port(void *, int, pid_t, int);
/*
* This table contains a list of event sources which need a static
* association with a port (every port).
* The last NULL entry in the table is required to detect "end of table".
*/
struct port_ksource port_ksource_tab[] = {
};
/* local functions */
port_gettimer_t *);
static int port_alert(port_t *, int, int, void *);
static int port_create(int *);
static int *port_errorn(int *, int, int, int);
static int port_noshare(void *, int *, pid_t, int, void *);
int);
static void port_remove_alert(port_queue_t *);
static void port_check_return_cond(port_queue_t *);
static void port_kstat_init(void);
#ifdef _SYSCALL32_IMPL
#endif
int
_init(void)
{
static const fs_operation_def_t port_vfsops_template[] = {
};
extern const fs_operation_def_t port_vnodeops_template[];
int error;
return (ENXIO);
/* Create a dummy vfs */
if (error) {
return (error);
}
if (error) {
return (error);
}
/* create kmem_cache for port event structures */
port_kstat_init(); /* init port kstats */
return (mod_install(&modlinkage));
}
int
{
}
/*
* System call wrapper for all port related system calls from 32-bit programs.
*/
#ifdef _SYSCALL32_IMPL
static int64_t
{
switch (opcode & PORT_CODE_MASK) {
case PORT_GET:
break;
case PORT_SENDN:
break;
default:
break;
}
return (error);
}
#endif /* _SYSCALL32_IMPL */
/*
* System entry point for port functions.
* a0 is a port file descriptor (except for PORT_SENDN and PORT_CREATE).
* The libc uses PORT_SYS_NOPORT in functions which do not deliver a
* port file descriptor as first argument.
*/
static int64_t
{
rval_t r;
int error = 0;
r.r_vals = 0;
if (opcode & PORT_SYS_NOPORT) {
opcode &= PORT_CODE_MASK;
if (opcode == PORT_SENDN) {
return (r.r_vals);
}
if (opcode == PORT_CREATE) {
if (error)
return (r.r_vals);
}
}
/* opcodes using port as first argument (a0) */
}
switch (opcode & PORT_CODE_MASK) {
case PORT_GET:
{
/* see PORT_GETN description */
port_timer.pgt_loop = 0;
} else {
}
do {
nget = 1;
break;
}
case PORT_GETN:
{
/*
* port_getn() can only retrieve own or shareable events from
* other processes. The port_getn() function remains in the
* kernel until own or shareable events are available or the
* timeout elapses.
*/
port_timer.pgt_flags = 0;
port_timer.pgt_loop = 0;
do {
return (r.r_vals);
}
case PORT_ASSOCIATE:
{
/* currently only PORT_SOURCE_FD is implemented */
if ((int)a1 != PORT_SOURCE_FD) {
break;
}
(void *)a4);
break;
}
case PORT_SEND:
{
/* user-defined events */
break;
}
case PORT_DISPATCH:
{
/*
* library events, blocking
* Only events of type PORT_SOURCE_AIO or PORT_SOURCE_MQ
* are currently allowed.
*/
break;
}
break;
}
case PORT_DISSOCIATE:
{
/* currently only PORT_SOURCE_FD is implemented */
if ((int)a1 != PORT_SOURCE_FD) {
break;
}
break;
}
case PORT_ALERT:
{
if ((int)a2) /* a2 = events */
else
break;
}
default:
break;
}
if (error)
return (r.r_vals);
}
/*
* System call to create a port.
*
* The port_create() function creates a vnode of type VPORT per port.
* The port control data is associated with the vnode as vnode private data.
* The port_create() function returns an event port file descriptor.
*/
static int
port_create(int *fdp)
{
/* initialize vnode and port private data */
/*
* Retrieve the maximal number of event ports allowed per system from
* the resource control: project.port-max-ids.
*/
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
return (EAGAIN);
}
/*
* Retrieve the maximal number of events allowed per port from
* the resource control: process.port-max-events.
*/
p->p_rctls, p);
mutex_exit(&p->p_lock);
/* allocate a new user file descriptor and a file structure */
/*
* If the file table is full, free allocated resources.
*/
return (EMFILE);
}
p->p_portcnt++;
/* initializes port private data */
/* set user file pointer */
return (0);
}
/*
* port_init() initializes event port specific data
*/
static void
{
/*
* If it is not enough memory available to satisfy a user
* request using a single port_getn() call then port_getn()
* will reduce the size of the list to PORT_MAX_LIST.
*/
/* Set timestamp entries required for fstat(2) requests */
/* initialize port queue structs */
portq->portq_flags = 0;
/* Allocate cache skeleton for PORT_SOURCE_FD events */
/*
* Allocate cache skeleton for association of event sources.
*/
/*
* pre-associate some kernel sources with this port.
* The pre-association is required to create port_source_t
* structures for object association.
* Some sources can not get associated with a port before the first
* object association is requested. Another reason to pre_associate
* a particular source with a port is because of performance.
*/
}
/*
* The port_add_ksource_local() function is being used to associate
* event sources with every new port.
* The event sources need to be added to port_ksource_tab[].
*/
static void
{
port_source_t **ps;
break;
}
/* associate new source with the port */
}
}
/*
* The port_send() function sends an event of type "source" to a
* port. This function is non-blocking. An event can be sent to
* a port as long as the number of events per port does not achieve the
* maximal allowed number of events. The max. number of events per port is
* defined by the resource control process.max-port-events.
* This function is used by the port library function port_send()
* and port_dispatch(). The port_send(3c) function is part of the
* event ports API and submits events of type PORT_SOURCE_USER. The
* port_dispatch() function is project private and it is used by library
* functions to submit events of other types than PORT_SOURCE_USER
* (e.g. PORT_SOURCE_AIO).
*/
static int
{
int error;
if (error)
return (error);
pev->portkev_object = 0;
pev->portkev_flags = 0;
return (0);
}
/*
* The port_noshare() function returns 0 if the current event was generated
* by the same process. Otherwise is returns a value other than 0 and the
* event should not be delivered to the current processe.
* The port_noshare() function is normally used by the port_dispatch()
* function. The port_dispatch() function is project private and can only be
* used within the event port project.
* Currently the libaio uses the port_dispatch() function to deliver events
* of types PORT_SOURCE_AIO.
*/
/* ARGSUSED */
static int
{
return (1);
return (0);
}
/*
* The port_dispatch_event() function is project private and it is used by
* libraries involved in the project to deliver events to the port.
* port_dispatch will sleep and wait for enough resources to satisfy the
* request, if necessary.
* The library can specify if the delivered event is shareable with other
* processes (see PORT_SYS_NOSHARE flag).
*/
static int
{
int error;
if (error)
return (error);
if (opcode & PORT_SYS_NOSHARE) {
} else {
pev->portkev_flags = 0;
}
return (0);
}
/*
* The port_sendn() function is the kernel implementation of the event
* port API function port_sendn(3c).
* This function is able to send an event to a list of event ports.
*/
static int
{
int errorcnt = 0;
int error = 0;
int count;
int port;
int *plist;
return (EINVAL);
return (EFAULT);
}
/*
* Scan the list for event port file descriptors and send the
* attached user event data embedded in a event of type
* PORT_SOURCE_USER to every event port in the list.
* If a list entry is not a valid event port then the corresponding
* error code will be stored in the errors[] list with the same
* list offset as in the ports[] list.
*/
errorcnt++;
continue;
}
errorcnt++;
continue;
}
if (error) {
errorcnt++;
continue;
}
pev->portkev_object = 0;
pev->portkev_flags = 0;
}
if (errorcnt) {
}
return (error);
}
static int *
{
return (elist);
}
/*
* port_alert()
* The port_alert() funcion is a high priority event and it is always set
* on top of the queue. It is also delivered as single event.
* flags:
* - SET :overwrite current alert data
* - UPDATE:set alert data or return EBUSY if alert mode is already set
*
* - set the ALERT flag
* - wakeup all sleeping threads
*/
static int
{
return (EINVAL);
/* check alert conditions */
if (flags == PORT_ALERT_UPDATE) {
return (EBUSY);
}
}
/*
* Store alert data in the port to be delivered to threads
* which are using port_get(n) to retrieve events.
*/
/* alert and deliver alert data to waiting threads */
/* no threads waiting for events */
return (0);
}
/*
* Set waiting threads in alert mode (PORTGET_ALERT)..
* Every thread waiting for events already allocated a portget_t
* structure to sleep on.
* The port alert arguments are stored in the portget_t structure.
* The PORTGET_ALERT flag is set to indicate the thread to return
* immediately with the alert event.
*/
do {
pa->portal_object = 0;
}
return (0);
}
/*
* Clear alert state of the port
*/
static void
{
}
/*
* The port_getn() function is used to retrieve events from a port.
*
* The port_getn() function returns immediately if there are enough events
* available in the port to satisfy the request or if the port is in alert
* mode (see port_alert(3c)).
* The timeout argument of port_getn(3c) -which is embedded in the
* port_gettimer_t structure- specifies if the system call should block or if it
* should return immediately depending on the number of events available.
* This function is internally used by port_getn(3c) as well as by
* port_get(3c).
*/
static int
{
int error = 0;
int rval;
int blocking = -1;
int timecheck;
int flag;
void *results;
return (EINVAL);
if (max == 0) {
/*
* Return number of objects with events.
* The port_block() call is required to synchronize this
* thread with another possible thread, which could be
* retrieving events from the port queue.
*/
/*
* Check if a second thread is currently retrieving events
* and it is using the temporary event queue.
*/
if (portq->portq_tnent) {
/* put remaining events back to the port queue */
}
return (0);
}
return (EFAULT);
}
if (*nget == 0) { /* no events required */
return (0);
}
/* port is being closed ... */
return (EBADFD);
}
/* return immediately if port in alert mode */
if (error == 0)
*nget = 1;
return (error);
}
portq->portq_thrcnt++;
/*
* Now check if the completed events satisfy the
* "wait" requirements of the current thread:
*/
/*
* loop entry of same thread
* pgt_loop is set when the current thread returns
* prematurely from this function. That could happen
* when a port is being shared between processes and
* this thread could not find events to return.
* It is not allowed to a thread to retrieve non-shareable
* events generated in other processes.
* PORTQ_WAIT_EVENTS is set when a thread already
* checked the current event queue and no new events
* are added to the queue.
*/
/* some new events arrived ...check them */
goto portnowait;
}
} else {
/* check if enough events are available ... */
goto portnowait;
/*
* There are not enough events available to satisfy
* the request, check timeout value and wait for
* incoming events.
*/
if (error) {
return (error);
}
if (blocking == 0) /* don't block, check fired events */
goto portnowait;
gethrestime(&now);
}
}
/* enqueue thread in the list of waiting threads */
/* Wait here until return conditions met */
for (;;) {
/* reap alert event and return */
if (error)
*nget = 0;
else
*nget = 1;
portq->portq_thrcnt--;
return (error);
}
/*
* Check if some other thread is already retrieving
* events (portq_getn > 0).
*/
if ((portq->portq_getn == 0) &&
break;
break;
}
if (rval <= 0) {
break;
}
}
/* take thread out of the wait queue */
/* return without events */
return (error);
}
/*
* Move port event queue to a temporary event queue .
* New incoming events will be continue be posted to the event queue
* and they will not be considered by the current thread.
* of the port queue mutex. The contention and performance degradation
* could happen because:
* a) incoming events use the port queue mutex to enqueue new events and
* b) before the event can be delivered to the application it is
* necessary to notify the event sources about the event delivery.
* Sometimes the event sources can require a long time to return and
* the queue mutex would block incoming events.
* During this time incoming events (port_send_event()) do not need
* to awake threads waiting for events. Before the current thread
* returns it will check the conditions to awake other waiting threads.
*/
if (portq->portq_tnent) {
/*
* Move remaining events from previous thread back to the
* port event queue.
*/
}
/* move port event queue to a temporary queue */
if (model == DATAMODEL_NATIVE) {
eventsz = sizeof (port_event_t);
}
break;
/* Just discard event */
if (PORT_FREE_EVENT(pev))
port_free_event_local(pev, 0);
tnent--;
continue;
}
/* move event data to copyout list */
/*
* Event can not be delivered to the
* current process.
*/
else
} else {
nevents++; /* # of events ready */
}
}
#ifdef _SYSCALL32_IMPL
} else {
eventsz = sizeof (port_event32_t);
}
break;
/* Just discard event */
if (PORT_FREE_EVENT(pev))
port_free_event_local(pev, 0);
tnent--;
continue;
}
/* move event data to copyout list */
/*
* Event can not be delivered to the
* current process.
*/
else
} else {
nevents++; /* # of events ready */
}
}
#endif /* _SYSCALL32_IMPL */
}
/*
* Remember number of remaining events in the temporary event queue.
*/
/*
* Work to do before return :
* - push list of remaining events back to the top of the standard
* port queue.
* - if this is the last thread calling port_get(n) then wakeup the
* thread waiting on close(2).
* - check for a deferred cv_signal from port_send_event() and wakeup
* the sleeping thread.
*/
if (portq->portq_tnent) {
/*
* move remaining events in the temporary event queue back
* to the port event queue
*/
}
/* Last thread => check close(2) conditions ... */
/* do not copyout events */
*nget = 0;
return (EBADFD);
}
} else if (portq->portq_getn == 0) {
/*
* no other threads retrieving events ...
* check wakeup conditions of sleeping threads
*/
}
/*
* Check PORTQ_POLLIN here because the current thread set temporarily
* the number of events in the queue to zero.
*/
} else {
}
/* now copyout list of user event structures to user space */
if (nevents) {
}
/* no events retrieved: check loop conditions */
if (blocking == -1) {
/* no timeout checked */
if (error) {
return (error);
}
gethrestime(&now);
}
} else {
/* timeout already checked -> remember values */
}
}
if (blocking)
/* timeout remaining */
}
/* set number of user event structures completed */
return (error);
}
/*
* 1. copy kernel event structure to user event structure.
* 2. PORT_KEV_WIRED event structures will be reused by the "source"
* 3. Remove PORT_KEV_DONEQ flag (event removed from the event queue)
* 4. Other types of event structures can be delivered back to the port cache
* (port_free_event_local()).
* 5. The event source callback function is the last opportunity for the
* event source to update events, to free local resources associated with
* the event or to deny the delivery of the event.
*/
static int
{
int free_event = 0;
int flags;
int error;
/* remove event from the queue */
/*
* Events of type PORT_KEV_WIRED remain allocated by the
* event source.
*/
else
free_event = 1;
if (pkevp->portkev_callback) {
if (error) {
/*
* Event can not be delivered.
* Caller must reinsert the event into the queue.
*/
return (error);
}
}
if (free_event)
return (0);
}
#ifdef _SYSCALL32_IMPL
/*
* 1. copy kernel event structure to user event structure.
* 2. PORT_KEV_WIRED event structures will be reused by the "source"
* 3. Remove PORT_KEV_DONEQ flag (event removed from the event queue)
* 4. Other types of event structures can be delivered back to the port cache
* (port_free_event_local()).
* 5. The event source callback function is the last opportunity for the
* event source to update events, to free local resources associated with
* the event or to deny the delivery of the event.
*/
static int
{
int free_event = 0;
int error;
int flags;
/* remove event from the queue */
/*
* Events if type PORT_KEV_WIRED remain allocated by the
* sub-system (source).
*/
else
free_event = 1;
if (error) {
/*
* Event can not be delivered.
* Caller must reinsert the event into the queue.
*/
return (error);
}
}
if (free_event)
return (0);
}
#endif /* _SYSCALL32_IMPL */
/*
* copyout alert event.
*/
static int
{
/* copyout alert event structures to user space */
if (model == DATAMODEL_NATIVE) {
return (EFAULT);
#ifdef _SYSCALL32_IMPL
} else {
return (EFAULT);
#endif /* _SYSCALL32_IMPL */
}
return (0);
}
/*
* Check return conditions :
* - pending port close(2)
* - threads waiting for events
*/
static void
{
portq->portq_thrcnt--;
if (portq->portq_thrcnt == 0)
else
}
}
/*
* The port_get_kevent() function returns
* - the event located at the head of the queue if 'last' pointer is NULL
* - the next event after the event pointed by 'last'
* The caller of this function is responsible for the integrity of the queue
* in use:
* - port_getn() is using a temporary queue protected with port_block().
* - port_close_events() is working on the global event queue and protects
* the queue with portq->portq_mutex.
*/
{
else
}
/*
* The port_get_timeout() function gets the timeout data from user space
* and converts that info into a corresponding internal representation.
* The kerneldata flag means that the timeout data is already loaded.
*/
static int
int *blocking, int kerneldata)
{
*blocking = 1;
return (0);
}
if (kerneldata) {
} else {
if (model == DATAMODEL_NATIVE) {
return (EFAULT);
#ifdef _SYSCALL32_IMPL
} else {
sizeof (wait_time_32)))
return (EFAULT);
#endif /* _SYSCALL32_IMPL */
}
}
*blocking = 0;
return (0);
}
return (EINVAL);
*blocking = 1;
return (0);
}
/*
* port_queue_thread()
* Threads requiring more events than available will be put in a wait queue.
* There is a "thread wait queue" per port.
* Threads requiring less events get a higher priority than others and they
* will be awoken first.
*/
static portget_t *
{
/* first waiting thread */
return (pgetp);
}
/*
* thread waiting for less events will be set on top of the queue.
*/
for (;;) {
break;
break; /* last event */
}
/* add thread to the queue */
return (pgetp);
}
/*
* Take thread out of the queue.
*/
static void
{
/* last (single) waiting thread */
portq->portq_nget = 0;
} else {
}
}
/*
* Set up event port kstats.
*/
static void
{
if (ksp) {
}
}