dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / isns / isnsd / msgq.c
blob6c5bf396d1d8f29accc1810c9df626c932bee009
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <pthread.h>
31 #include <synch.h>
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"
39 msg_queue_t *
40 queue_calloc(
43 msg_queue_t *q;
45 q = (msg_queue_t *)calloc(1, sizeof (msg_queue_t));
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;
55 return (q);
58 int
59 queue_msg_set(
60 msg_queue_t *q,
61 msg_id_t id,
62 void *data
65 msg_text_t *msg;
67 msg = (msg_text_t *)calloc(1, sizeof (msg_text_t));
69 if (!msg) {
70 return (1);
73 msg->id = id;
74 msg->data = data;
76 (void) pthread_mutex_lock(&q->q_mutex);
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;
89 (void) pthread_mutex_unlock(&q->q_mutex);
91 (void) sema_post(&q->q_sema);
93 return (0);
96 msg_text_t *
97 queue_msg_get(
98 msg_queue_t *q
101 msg_text_t *msg;
103 while (sema_wait(&q->q_sema)) {
104 (void) sleep(1);
107 (void) pthread_mutex_lock(&q->q_mutex);
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;
116 (void) pthread_mutex_unlock(&q->q_mutex);
118 return (msg);
121 void
122 queue_msg_free(
123 msg_text_t *msg
126 free(msg);