-
Notifications
You must be signed in to change notification settings - Fork 7
/
msgqueue.c
169 lines (138 loc) · 4.59 KB
/
msgqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***************************************************************************
* Copyright (C) 2008 by John D. Robertson *
* john@rrci.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "ez_libpthread.h"
#include "msgqueue.h"
#include "util.h"
MSGQUEUE *
MSGQUEUE_constructor (MSGQUEUE * self, size_t msgSize, unsigned int queueLen)
{
if (!self)
return NULL;
pthread_mutex_init (&self->mtx, NULL);
/* Allocate the memory in which to store messages */
if (!(self->buff_ptr = (char *) malloc (msgSize * queueLen)))
return NULL;
/* Initialize the header segment */
self->numItems = 0;
self->head = 0;
self->tail = 0;
self->msgSize = msgSize;
self->maxItems = queueLen;
return self;
}
void*
MSGQUEUE_destructor (MSGQUEUE * self)
{
pthread_mutex_destroy (&self->mtx);
free (self->buff_ptr);
return self;
}
int
MSGQUEUE_submitMsg (MSGQUEUE * self, const void *msgBuf)
/*******************************************************************
* Submit a message to the message queue.
*/
{
int rtn = 0;
ez_pthread_mutex_lock (&self->mtx);
if (self->numItems == self->maxItems)
{
#ifdef DEBUG
eprintf("WARNING: %p queue full.", self);
#endif
rtn = EOF;
goto abort;
}
/* If this is not the first item to be added */
if (self->numItems)
self->tail = (self->tail + 1) % self->maxItems;
memcpy (self->buff_ptr + self->tail * self->msgSize, msgBuf, self->msgSize);
++self->numItems;
abort:
ez_pthread_mutex_unlock (&self->mtx);
return rtn;
}
int
MSGQUEUE_extractMsg (MSGQUEUE * self, void *msgBuf)
/*****************************************************************************
* Extract a message from the message queue. Returns EOF when queue is empty.
*/
{
int rtn = 0;
ez_pthread_mutex_lock (&self->mtx);
if (!self->numItems)
{
rtn = EOF;
goto abort;
}
self->numItems--;
memcpy (msgBuf, self->buff_ptr + self->head * self->msgSize, self->msgSize);
if (self->numItems)
self->head = (self->head + 1) % self->maxItems;
abort:
ez_pthread_mutex_unlock (&self->mtx);
return rtn;
}
int
MSGQUEUE_checkQueue (MSGQUEUE * self,
int (*check) (void *data_ptr, void *arg), void *arg)
/*****************************************************************************
* Runs through the message queuue calling check() until it returns non-zero,
* or the queue is fully traversed.
* Return: 0 -> check() always returned zero, or queue was empty.
* otherwise, return value of check().
*/
{
int rtn = 0;
unsigned int i;
void *ptr;
ez_pthread_mutex_lock (&self->mtx);
if (!self->numItems)
goto abort;
for (i = 0, ptr = self->buff_ptr + self->head * self->msgSize;
i < self->numItems;
++i, ptr =
self->buff_ptr + ((self->head + i) % self->maxItems) * self->msgSize)
{
if ((rtn = (*check) (ptr, arg)))
break;
}
abort:
ez_pthread_mutex_unlock (&self->mtx);
return rtn;
}
/************************************************************/
int _ez_MSGQUEUE_submitMsg(
const char *fileName,
int lineNo,
const char *funcName,
MSGQUEUE *self,
const void *msgBuf
)
{
int rtn= MSGQUEUE_submitMsg (self, msgBuf);
if(!rtn) return 0;
_eprintf(fileName, lineNo, funcName, "MSGQUEUE_submitMsg() failed");
abort();
}