queue.c (7c478bd95313f5f23a4c958a745db2134aa03244) | queue.c (0ea5e3a571e3da934507bdd32924d11659c70704) |
---|---|
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the | 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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. | 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. |
8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22/* | 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/* |
23 * Copyright 1996-2002 Sun Microsystems, Inc. All rights reserved. | 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
24 * Use is subject to license terms. 25 */ 26 27#pragma ident "%Z%%M% %I% %E% SMI" 28 29#include <pthread.h> 30#include <malloc.h> 31#include <memory.h> --- 12 unchanged lines hidden (view full) --- 44 45int 46dataq_init(dataq_t *ptr) 47{ 48 ptr->num_data = 0; 49 ptr->num_waiters = 0; 50 ll_init(&ptr->data); 51 ll_init(&ptr->waiters); | 23 * Use is subject to license terms. 24 */ 25 26#pragma ident "%Z%%M% %I% %E% SMI" 27 28#include <pthread.h> 29#include <malloc.h> 30#include <memory.h> --- 12 unchanged lines hidden (view full) --- 43 44int 45dataq_init(dataq_t *ptr) 46{ 47 ptr->num_data = 0; 48 ptr->num_waiters = 0; 49 ll_init(&ptr->data); 50 ll_init(&ptr->waiters); |
52 pthread_mutex_init(&ptr->lock, NULL); | 51 (void) pthread_mutex_init(&ptr->lock, NULL); |
53 assert((pthread_mutex_lock(&ptr->lock) == 0) && 54 (dataq_check(ptr) == 1) && 55 (pthread_mutex_unlock(&ptr->lock) == 0)); 56 return (0); 57} 58 59int 60dataq_enqueue(dataq_t *dataq, void *in) 61{ 62 dataq_data_t *ptr = (dataq_data_t *)malloc(sizeof (*ptr)); 63 dataq_waiter_t *sleeper; 64 65 if (ptr == NULL) 66 return (-1); 67 ptr->data = in; | 52 assert((pthread_mutex_lock(&ptr->lock) == 0) && 53 (dataq_check(ptr) == 1) && 54 (pthread_mutex_unlock(&ptr->lock) == 0)); 55 return (0); 56} 57 58int 59dataq_enqueue(dataq_t *dataq, void *in) 60{ 61 dataq_data_t *ptr = (dataq_data_t *)malloc(sizeof (*ptr)); 62 dataq_waiter_t *sleeper; 63 64 if (ptr == NULL) 65 return (-1); 66 ptr->data = in; |
68 pthread_mutex_lock(&dataq->lock); | 67 (void) pthread_mutex_lock(&dataq->lock); |
69 assert(dataq_check(dataq)); 70 ll_enqueue(&dataq->data, &ptr->list); 71 dataq->num_data++; 72 if (dataq->num_waiters) { 73 /*LINTED*/ 74 sleeper = (dataq_waiter_t *)ll_peek(&dataq->waiters); 75 sleeper->wakeup = 1; | 68 assert(dataq_check(dataq)); 69 ll_enqueue(&dataq->data, &ptr->list); 70 dataq->num_data++; 71 if (dataq->num_waiters) { 72 /*LINTED*/ 73 sleeper = (dataq_waiter_t *)ll_peek(&dataq->waiters); 74 sleeper->wakeup = 1; |
76 pthread_cond_signal(&sleeper->cv); | 75 (void) pthread_cond_signal(&sleeper->cv); |
77 } 78 assert(dataq_check(dataq)); | 76 } 77 assert(dataq_check(dataq)); |
79 pthread_mutex_unlock(&dataq->lock); | 78 (void) pthread_mutex_unlock(&dataq->lock); |
80 return (0); 81} 82 83int 84dataq_dequeue(dataq_t *dataq, void **outptr, int try) 85{ 86 dataq_data_t *dptr; 87 dataq_waiter_t *sleeper; 88 | 79 return (0); 80} 81 82int 83dataq_dequeue(dataq_t *dataq, void **outptr, int try) 84{ 85 dataq_data_t *dptr; 86 dataq_waiter_t *sleeper; 87 |
89 pthread_mutex_lock(&dataq->lock); | 88 (void) pthread_mutex_lock(&dataq->lock); |
90 if ((dataq->num_waiters > 0) || 91 ((dptr = (dataq_data_t *)ll_dequeue(&dataq->data)) == NULL)) { 92 dataq_waiter_t wait; 93 if (try) { | 89 if ((dataq->num_waiters > 0) || 90 ((dptr = (dataq_data_t *)ll_dequeue(&dataq->data)) == NULL)) { 91 dataq_waiter_t wait; 92 if (try) { |
94 pthread_mutex_unlock(&dataq->lock); | 93 (void) pthread_mutex_unlock(&dataq->lock); |
95 return (1); 96 } 97 wait.wakeup = 0; | 94 return (1); 95 } 96 wait.wakeup = 0; |
98 pthread_cond_init(&wait.cv, NULL); | 97 (void) pthread_cond_init(&wait.cv, NULL); |
99 dataq->num_waiters++; 100 ll_enqueue(&dataq->waiters, &wait.list); 101 while (wait.wakeup == 0) | 98 dataq->num_waiters++; 99 ll_enqueue(&dataq->waiters, &wait.list); 100 while (wait.wakeup == 0) |
102 pthread_cond_wait(&wait.cv, &dataq->lock); 103 ll_dequeue(&dataq->waiters); | 101 (void) pthread_cond_wait(&wait.cv, &dataq->lock); 102 (void) ll_dequeue(&dataq->waiters); |
104 dataq->num_waiters--; | 103 dataq->num_waiters--; |
105 pthread_cond_destroy(&wait.cv); | 104 (void) pthread_cond_destroy(&wait.cv); |
106 dptr = (dataq_data_t *)ll_dequeue(&dataq->data); 107 } 108 dataq->num_data--; 109 if (dataq->num_data && dataq->num_waiters) { 110 /*LINTED*/ 111 sleeper = (dataq_waiter_t *)ll_peek(&dataq->waiters); 112 sleeper->wakeup = 1; | 105 dptr = (dataq_data_t *)ll_dequeue(&dataq->data); 106 } 107 dataq->num_data--; 108 if (dataq->num_data && dataq->num_waiters) { 109 /*LINTED*/ 110 sleeper = (dataq_waiter_t *)ll_peek(&dataq->waiters); 111 sleeper->wakeup = 1; |
113 pthread_cond_signal(&sleeper->cv); | 112 (void) pthread_cond_signal(&sleeper->cv); |
114 } | 113 } |
115 pthread_mutex_unlock(&dataq->lock); | 114 (void) pthread_mutex_unlock(&dataq->lock); |
116 *outptr = dptr->data; 117 free(dptr); 118 return (0); 119} 120 121static void 122dataq_data_destroy(void * p) 123{ 124 dataq_data_t *d = (dataq_data_t *)p; 125 free(d->data); 126 free(d); 127} 128 129static void 130dataq_waiters_destroy(void * p) 131{ 132 dataq_waiter_t *d = (dataq_waiter_t *)p; | 115 *outptr = dptr->data; 116 free(dptr); 117 return (0); 118} 119 120static void 121dataq_data_destroy(void * p) 122{ 123 dataq_data_t *d = (dataq_data_t *)p; 124 free(d->data); 125 free(d); 126} 127 128static void 129dataq_waiters_destroy(void * p) 130{ 131 dataq_waiter_t *d = (dataq_waiter_t *)p; |
133 pthread_cond_destroy(&d->cv); | 132 (void) pthread_cond_destroy(&d->cv); |
134 free(d); 135} 136 137int 138dataq_destroy(dataq_t *dataq) 139{ | 133 free(d); 134} 135 136int 137dataq_destroy(dataq_t *dataq) 138{ |
140 pthread_mutex_destroy(&dataq->lock); | 139 (void) pthread_mutex_destroy(&dataq->lock); |
141 ll_mapf(&dataq->data, dataq_data_destroy); 142 ll_mapf(&dataq->waiters, dataq_waiters_destroy); 143 return (0); 144} | 140 ll_mapf(&dataq->data, dataq_data_destroy); 141 ll_mapf(&dataq->waiters, dataq_waiters_destroy); 142 return (0); 143} |