forked from DC-SWAT/DreamShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
198 lines (160 loc) · 4.5 KB
/
Copy pathqueue.c
File metadata and controls
198 lines (160 loc) · 4.5 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
** $Id: queue.c 10 2007-09-15 19:37:27Z danielq $
** Queue Management: Implementation
** SoongSoft, Argentina
** http://www.soongsoft.com mailto:dq@soongsoft.com
** Copyright (C) 2003-2006 Daniel Quintela. All rights reserved.
*/
#ifdef _WIN32
# include <windows.h>
#ifndef NATV_WIN32
#include <pthread.h>
#endif
#else
# include <unistd.h>
# include <strings.h>
# include <pthread.h>
#endif
#include <stdlib.h>
#include "syncos.h"
#include "queue.h"
#if !defined(_WIN32) && !defined(_arch_dreamcast)
# define QUEUE_PIPE_IN 0
# define QUEUE_PIPE_OUT 1
#endif
static int InsertMsgAtQueueTail( QUEUE *pQueue, void *pMsg) {
QMSG *pQMsg;
pQMsg = ( QMSG *) calloc( 1, sizeof( QMSG));
pQMsg->pMsg = pMsg;
if( pQueue->qMsgHead == NULL) {
pQueue->qMsgHead = pQMsg;
pQueue->qMsgTail = pQMsg;
} else {
pQueue->qMsgTail->next = pQMsg;
pQueue->qMsgTail = pQMsg;
}
pQueue->msgcount++;
return( 0);
}
static QMSG * RemoveMsgFromQueueHead( QUEUE *pQueue) {
QMSG *pQMsg;
pQMsg = pQueue->qMsgHead;
if( pQueue->qMsgHead == pQueue->qMsgTail) {
pQueue->qMsgTail = NULL;
}
pQueue->qMsgHead = pQueue->qMsgHead->next;
pQueue->msgcount--;
return( pQMsg);
}
int QueDestroy( QUEUE *pQueue) {
while( pQueue->qMsgHead != NULL) {
QMSG *pQMsg;
pQMsg = RemoveMsgFromQueueHead( pQueue);
free( pQMsg->pMsg);
free( pQMsg);
}
#ifdef _WIN32
CloseHandle( pQueue->qNotEmpty);
if( pQueue->qNotFull != NULL)
CloseHandle( pQueue->qNotFull);
CloseHandle( pQueue->qMutex);
#elif defined(_arch_dreamcast)
sem_destroy( pQueue->qNotEmpty);
if( pQueue->qNotFull != NULL)
sem_destroy( pQueue->qNotFull);
#else
close( pQueue->qNotEmpty[QUEUE_PIPE_IN]);
close( pQueue->qNotEmpty[QUEUE_PIPE_OUT]);
#endif
return( 0);
}
int _QueGet( QUEUE *pQueue, void **ppMsg) {
QMSG *pQMsg;
OsLockMutex( ( void *) pQueue->qMutex, INFINITE);
pQMsg = RemoveMsgFromQueueHead( pQueue);
OsUnlockMutex( ( void *) pQueue->qMutex);
if( pQueue->qMax != QUE_NO_LIMIT) {
#ifdef _WIN32
ReleaseSemaphore( pQueue->qNotFull, 1, NULL);
#elif defined(_arch_dreamcast)
sem_signal(pQueue->qNotFull);
#endif
}
#if !defined(_WIN32) && !defined(_arch_dreamcast)
{
char b;
read( pQueue->qNotEmpty[QUEUE_PIPE_IN], &b, 1);
}
#endif
*ppMsg = pQMsg->pMsg;
free( pQMsg);
return( 0);
}
int QueGet( QUEUE *pQueue, void **ppMsg) {
#ifdef _WIN32
WaitForSingleObject( pQueue->qNotEmpty, INFINITE);
#elif defined(_arch_dreamcast)
sem_wait(pQueue->qNotEmpty);
#endif
return( _QueGet( pQueue, ppMsg));
}
int QuePut( QUEUE *pQueue, void *pMsg) {
if( pQueue->qMax != QUE_NO_LIMIT) { /* bounded queue */
#ifdef _WIN32
WaitForSingleObject( pQueue->qNotFull, INFINITE);
#elif defined(_arch_dreamcast)
sem_wait(pQueue->qNotFull);
#endif
}
OsLockMutex( ( void *) pQueue->qMutex, INFINITE);
InsertMsgAtQueueTail( pQueue, pMsg);
OsUnlockMutex( ( void *) pQueue->qMutex);
#ifdef _WIN32
ReleaseSemaphore( pQueue->qNotEmpty, 1, NULL);
#elif defined(_arch_dreamcast)
sem_signal(pQueue->qNotEmpty);
#else
{
char b;
write( pQueue->qNotEmpty[QUEUE_PIPE_OUT], &b, 1);
}
#endif
return( 0);
}
int QueCreate( QUEUE *pQueue, int qLimit ) {
pQueue->qMutex = OsCreateMutex( NULL);
#ifdef _WIN32
pQueue->qNotEmpty = CreateSemaphore( NULL, 0, 0x7fffffff, NULL);
#elif defined(_arch_dreamcast)
pQueue->qNotEmpty = sem_create(0);
#else
if( pipe(pQueue->qNotEmpty)) {
return( -1);
}
#endif
if( qLimit != QUE_NO_LIMIT) {
#ifdef _WIN32
pQueue->qNotFull = CreateSemaphore( NULL, qLimit, 0x7fffffff, NULL);
} else
pQueue->qNotFull = NULL;
#elif defined(_arch_dreamcast)
pQueue->qNotFull = sem_create(qLimit);
} else
pQueue->qNotFull = NULL;
#else
} else
pQueue->qNotFull = 0;
#endif
pQueue->qMax = qLimit;
pQueue->qMsgHead = NULL;
pQueue->qMsgTail = NULL;
pQueue->msgcount = 0;
return( 0);
}
long GetQueNotEmptyHandle( QUEUE *pQueue) {
#if defined(_WIN32) || defined(_arch_dreamcast)
return( ( long) ( pQueue->qNotEmpty));
#else
return( ( long) ( pQueue->qNotEmpty[QUEUE_PIPE_IN]));
#endif
}