xref: /illumos-gate/usr/src/cmd/isns/isnsd/msgq.c (revision fcf3ce44)
1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte 
22*fcf3ce44SJohn Forte /*
23*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*fcf3ce44SJohn Forte  * Use is subject to license terms.
25*fcf3ce44SJohn Forte  */
26*fcf3ce44SJohn Forte 
27*fcf3ce44SJohn Forte #include <stdio.h>
28*fcf3ce44SJohn Forte #include <stdlib.h>
29*fcf3ce44SJohn Forte #include <unistd.h>
30*fcf3ce44SJohn Forte #include <pthread.h>
31*fcf3ce44SJohn Forte #include <synch.h>
32*fcf3ce44SJohn Forte 
33*fcf3ce44SJohn Forte #include "isns_server.h"
34*fcf3ce44SJohn Forte #include "isns_msgq.h"
35*fcf3ce44SJohn Forte #include "isns_cache.h"
36*fcf3ce44SJohn Forte #include "isns_obj.h"
37*fcf3ce44SJohn Forte #include "isns_log.h"
38*fcf3ce44SJohn Forte 
39*fcf3ce44SJohn Forte msg_queue_t *
queue_calloc()40*fcf3ce44SJohn Forte queue_calloc(
41*fcf3ce44SJohn Forte )
42*fcf3ce44SJohn Forte {
43*fcf3ce44SJohn Forte 	msg_queue_t *q;
44*fcf3ce44SJohn Forte 
45*fcf3ce44SJohn Forte 	q = (msg_queue_t *)calloc(1, sizeof (msg_queue_t));
46*fcf3ce44SJohn Forte 
47*fcf3ce44SJohn Forte 	if (q) {
48*fcf3ce44SJohn Forte 		if (sema_init(&q->q_sema, 0, USYNC_THREAD, NULL) ||
49*fcf3ce44SJohn Forte 		    pthread_mutex_init(&q->q_mutex, NULL)) {
50*fcf3ce44SJohn Forte 			free(q);
51*fcf3ce44SJohn Forte 			q = NULL;
52*fcf3ce44SJohn Forte 		}
53*fcf3ce44SJohn Forte 	}
54*fcf3ce44SJohn Forte 
55*fcf3ce44SJohn Forte 	return (q);
56*fcf3ce44SJohn Forte }
57*fcf3ce44SJohn Forte 
58*fcf3ce44SJohn Forte int
queue_msg_set(msg_queue_t * q,msg_id_t id,void * data)59*fcf3ce44SJohn Forte queue_msg_set(
60*fcf3ce44SJohn Forte 	msg_queue_t *q,
61*fcf3ce44SJohn Forte 	msg_id_t id,
62*fcf3ce44SJohn Forte 	void *data
63*fcf3ce44SJohn Forte )
64*fcf3ce44SJohn Forte {
65*fcf3ce44SJohn Forte 	msg_text_t *msg;
66*fcf3ce44SJohn Forte 
67*fcf3ce44SJohn Forte 	msg = (msg_text_t *)calloc(1, sizeof (msg_text_t));
68*fcf3ce44SJohn Forte 
69*fcf3ce44SJohn Forte 	if (!msg) {
70*fcf3ce44SJohn Forte 		return (1);
71*fcf3ce44SJohn Forte 	}
72*fcf3ce44SJohn Forte 
73*fcf3ce44SJohn Forte 	msg->id = id;
74*fcf3ce44SJohn Forte 	msg->data = data;
75*fcf3ce44SJohn Forte 
76*fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&q->q_mutex);
77*fcf3ce44SJohn Forte 
78*fcf3ce44SJohn Forte 	if (q->q_head == NULL) {
79*fcf3ce44SJohn Forte 		ASSERT(!q->q_tail);
80*fcf3ce44SJohn Forte 		q->q_head = msg;
81*fcf3ce44SJohn Forte 		q->q_tail = msg;
82*fcf3ce44SJohn Forte 	} else {
83*fcf3ce44SJohn Forte 		ASSERT(q->q_tail);
84*fcf3ce44SJohn Forte 		q->q_tail->next = msg;
85*fcf3ce44SJohn Forte 		msg->prev = q->q_tail;
86*fcf3ce44SJohn Forte 		q->q_tail = msg;
87*fcf3ce44SJohn Forte 	}
88*fcf3ce44SJohn Forte 
89*fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&q->q_mutex);
90*fcf3ce44SJohn Forte 
91*fcf3ce44SJohn Forte 	(void) sema_post(&q->q_sema);
92*fcf3ce44SJohn Forte 
93*fcf3ce44SJohn Forte 	return (0);
94*fcf3ce44SJohn Forte }
95*fcf3ce44SJohn Forte 
96*fcf3ce44SJohn Forte msg_text_t *
queue_msg_get(msg_queue_t * q)97*fcf3ce44SJohn Forte queue_msg_get(
98*fcf3ce44SJohn Forte 	msg_queue_t *q
99*fcf3ce44SJohn Forte )
100*fcf3ce44SJohn Forte {
101*fcf3ce44SJohn Forte 	msg_text_t *msg;
102*fcf3ce44SJohn Forte 
103*fcf3ce44SJohn Forte 	while (sema_wait(&q->q_sema)) {
104*fcf3ce44SJohn Forte 		(void) sleep(1);
105*fcf3ce44SJohn Forte 	}
106*fcf3ce44SJohn Forte 
107*fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&q->q_mutex);
108*fcf3ce44SJohn Forte 
109*fcf3ce44SJohn Forte 	msg = q->q_head;
110*fcf3ce44SJohn Forte 	ASSERT(msg);
111*fcf3ce44SJohn Forte 	q->q_head = msg->next;
112*fcf3ce44SJohn Forte 	if (q->q_head == NULL) {
113*fcf3ce44SJohn Forte 		q->q_tail = NULL;
114*fcf3ce44SJohn Forte 	}
115*fcf3ce44SJohn Forte 
116*fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&q->q_mutex);
117*fcf3ce44SJohn Forte 
118*fcf3ce44SJohn Forte 	return (msg);
119*fcf3ce44SJohn Forte }
120*fcf3ce44SJohn Forte 
121*fcf3ce44SJohn Forte void
queue_msg_free(msg_text_t * msg)122*fcf3ce44SJohn Forte queue_msg_free(
123*fcf3ce44SJohn Forte 	msg_text_t *msg
124*fcf3ce44SJohn Forte )
125*fcf3ce44SJohn Forte {
126*fcf3ce44SJohn Forte 	free(msg);
127*fcf3ce44SJohn Forte }