/*
* 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"
/*
* A synchronized FIFO queue for inter-thread producer-consumer semantics.
* This queue will handle multiple writers and readers simultaneously.
*
* The following operations are provided:
* slp_new_queue: create a new queue
* slp_enqueue: place a message at the end of the queue
* slp_enqueue_at_head: place a message the the start of the queue
* slp_dequeue: remove and return the next message on the queue
* (waits indefinately)
* slp_dequeue_timed: remove and return the next message on the queue
* (waits only for a specified time)
* slp_flush_queue: flushes and frees all messages on a queue
* slp_destroy_queue: frees an empty queue.
*/
#include <stdio.h>
#include <stdlib.h>
#include <thread.h>
#include <synch.h>
#include <syslog.h>
#include <slp.h>
#include <slp-internal.h>
/* Private implementation details */
struct queue_entry {
void *msg;
};
struct queue {
int count;
};
/*
* Creates, initializes, and returns a new queue.
* If an initialization error occured, returns NULL and sets err to
* the appropriate SLP error code.
* queues can operate in one of two modes: timed-wait, and infinite
* wait. The timeout parameter specifies which of these modes should
* be enabled for the new queue.
*/
struct queue *q;
/* initialize new mutex and semaphore */
return (NULL);
}
/* intialize condition vars */
return (NULL);
}
/* create the queue */
return (NULL);
}
q->count = 0;
return (q);
}
/*
* Adds msg to the tail of queue q.
* Returns an SLP error code: SLP_OK for no error, or SLP_MEMORY_ALLOC_FAILED
* if it couldn't allocate memory.
*/
return (SLP_MEMORY_ALLOC_FAILED);
}
(void) mutex_lock(q->lock);
} else { /* queue is empty */
}
q->count++;
(void) mutex_unlock(q->lock);
(void) cond_signal(q->wait);
return (SLP_OK);
}
/*
* Inserts a message at the head of the queue. This is useful for inserting
* things like cancel messages.
*/
return (SLP_MEMORY_ALLOC_FAILED);
}
(void) mutex_lock(q->lock);
q->count++;
(void) mutex_unlock(q->lock);
(void) cond_signal(q->wait);
return (SLP_OK);
}
/*
* The core functionality for dequeue.
*/
void *msg;
if (!qe)
return (NULL); /* shouldn't get here */
else
q->count--;
return (msg);
}
/*
* Returns the first message waiting or arriving in the queue, or if no
* message is available after waiting the amount of time specified in
* 'to', returns NULL, and sets 'etimed' to true. If an error occured,
* returns NULL and sets 'etimed' to false.
*/
int err;
void *ans;
if (etimed)
(void) mutex_lock(q->lock);
if (q->count > 0) {
/* something's in the q, so no need to wait */
goto msg_available;
}
/* else wait */
while (q->count == 0) {
if (to) {
} else {
}
(void) mutex_unlock(q->lock);
return (NULL);
}
}
ans = dequeue_nolock(q);
(void) mutex_unlock(q->lock);
return (ans);
}
/*
* Removes the first message from the queue and returns it.
* Returns NULL only on internal error.
*/
}
/*
* Flushes the queue, using the caller-specified free function to
* free each message in the queue.
*/
free_f(p);
}
}
/*
* Frees a queue.
* The queue must be empty before it can be destroyed; slp_flush_queue
* can be used to empty a queue.
*/
(void) mutex_destroy(q->lock);
(void) cond_destroy(q->wait);
free(q);
}