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