src/mspipc.c

filter: Statementstmt 68.0% 34 / 50 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.9% 40 / 58 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/ppersist.c ↑ Summary
1 /****************************************************************************/
2 /* */
3 /* Program name: MQIsdp protocol C Language implementation */
4 /* */
5 /* Description: This source file contains functions for handing all Inter */
6 /* Process Communication between the various threads. */
7 /* */
8 /* Statement: Licensed Materials - Property of IBM */
9 /* */
10 /* MQSeries SupportPac IA93 */
11 /* (C) Copyright IBM Corp. 2002 */
12 /* */
13 /****************************************************************************/
14 /* Version @(#) IA93/ship/mspipc.c, SupportPacs, S000 1.3 03/08/26 16:38:24 */
15 /* */
16 /* Function: */
17 /* */
18 /* Functions such as mspInitialiseIPC, mspWriteIPC and mspReadIPC are used */
19 /* for transferring data between threads. mspLockMutex and mspReleaseMutex */
20 /* control when data may be sent to a particular thread (a lock must be */
21 /* obtained before data can be written to a thread). mspWaitForSemaphore */
22 /* and mspSignalSemaphore are used when an application does a blocking wait */
23 /* for messages to arrive. */
24 /* */
25 /****************************************************************************/
26 /* */
27 /* Change history: */
28 /* */
29 /* V1.0 19-02-2003 IRH Initial release */
30 /* V1.1 18-08-2003 IRH mspReadIPC - pointer returned from mspRealloc was*/
31 /* not correctly assigned to *ppBuffer variable. */
32 /* */
33 /*==========================================================================*/
34 /* Module Name: mspipc.c */
35 #include <mspsh.h>
36
37 /* Initialise the IPC read timeout, if appropriate */
38 /* A timeout value of -1 will cause the mailbox to block until data is received */
39 0 DllExport int mspSetIPCTimeout( MBH mbHandle, long mSecs ) {
40 #ifndef MSP_SINGLE_THREAD
41
42 #if defined(WIN32)
43
44 if ( mSecs == -1 ) {
45 SetMailslotInfo( mbHandle, MSP_IPC_WAIT_FOREVER );
46 } else {
47 SetMailslotInfo( mbHandle, mSecs );
48 }
49
50 #endif
51
52 #endif
53
54 0 return 0;
55 }
56
57 /* Initialise the IPC, as appropriate */
58 /* A timeout value of -1 will cause the mailbox to block until data is received */
59 14 DllExport int mspInitialiseIPC( MBH mbHandle, IPCCB *pIpcCb ) {
60 #ifndef MSP_SINGLE_THREAD
61
62 #if defined(WIN32)
63 SetMailslotInfo( mbHandle, pIpcCb->readTimeout );
64 #elif defined(UNIX)
65 /* On UNIX we want to ignore the SIGPIPE signal */
66 signal( SIGPIPE, SIG_IGN );
67
68 #endif
69
70 #endif
71
72 14 return 0;
73 }
74
75 /* mspWriteIPC: Used by the tasks/threads to exchange data. */
76 /* Returns 1 on error, otherwise 0 */
77 114 DllExport int mspWriteIPC( MBH mbHandle, IPCCB *pIpcCb, char *ec, int retCode,
78 MQISDPMH hMsg, long numBytes, char *pBuffer ) {
79 114 int rc = 0;
80 CB_HEAD ipcHeader;
81 #ifndef MSP_SINGLE_THREAD
82 long bytesWritten = 0;
83 #endif /* MSP_SINGLE_THREAD */
84
85 /* Setup the IPC header */
86 114 memcpy( ipcHeader.eyeCatcher, ec, MSP_EC_LENGTH );
87 114 ipcHeader.returnCode = retCode;
88 114 ipcHeader.hMsg = hMsg;
89 114 ipcHeader.dataLength = numBytes;
90 114 ipcHeader.pData = pBuffer;
91
92 #ifndef MSP_SINGLE_THREAD
93
94 #if defined(WIN32)
95
96 if ( WriteFile( mbHandle, &ipcHeader, sizeof(CB_HEAD), &bytesWritten, NULL ) == 0 ) {
97 printf( "IPC: WriteFile error:%ld\n", GetLastError() );
98 rc = 1;
99 }
100 #elif defined(UNIX)
101
102 if ( write( mbHandle, &ipcHeader, sizeof(CB_HEAD) ) < 0 ) {
103 printf( "IPC: write error:%ld\n", errno );
104 rc = 1;
105 }
106
107 #endif
108
109 #else
110 114 memcpy( pIpcCb->pPseudoMailbox, &ipcHeader, sizeof(CB_HEAD) );
111 #endif
112
113 114 return rc;
114 }
115
116 /* mspReadIPC: Used by the daemon and client tasks to read data */
117 /* from the partner task (client or daemon respectively). */
118 /* Returns 1 on error, otherwise 0 */
119 /* NOTE: nBytesRead may be 0 even if data is read, because this */
120 /* only counts data received in addition to the CB_HEAD header. */
121 /* The function return code should be used to determine if data */
122 /* was received or not. */
123 114 DllExport int mspReadIPC( MBH mbHandle, IPCCB *pIpcCb, MSPCMN *comParms, long *nBytesRead,
124 long *bufSize, void **ppBuffer, char *ec ,int *retCode,
125 MQISDPMH *phMsg ) {
126 114 int rc = 0;
127 CB_HEAD ipcHeader;
128 114 long bytesRead = 0;
129
130 #ifndef MSP_SINGLE_THREAD
131
132 #if defined(WIN32)
133 if ( ReadFile( mbHandle, &ipcHeader, sizeof(CB_HEAD), &bytesRead, NULL ) == 0 ) {
134 if ( GetLastError() != ERROR_SEM_TIMEOUT) {
135 printf( "IPC: ReadFile error:%ld\n", GetLastError() );
136 }
137 bytesRead = 0;
138 rc = 1;
139 }
140
141 #elif defined(UNIX)
142 int selRc;
143
144 /* Wait for any IPC input down the pipe */
145 selRc = msp_select( mbHandle, pIpcCb->readTimeout );
146 if( selRc > 0 ) {
147 /* Bytes available */
148 bytesRead = read( mbHandle, &ipcHeader, sizeof(CB_HEAD) );
149 if ( bytesRead < 0 ) {
150 printf( "IPC: read error:%ld\n", errno );
151 bytesRead = 0;
152 rc = 1;
153 }
154 } else {
155 /* No data available - msp_select timeout (0) or an error (<0) */
156 if ( selRc < 0 ) {
157 printf( "IPC: read select error:%ld\n", errno );
158 }
159 bytesRead = 0;
160 rc = 1;
161 }
162
163 #endif
164
165 #else /* MSP_SINGLE_THREAD */
166 114 memcpy( &ipcHeader, pIpcCb->pPseudoMailbox, sizeof(CB_HEAD) );
167 114 bytesRead = sizeof(CB_HEAD);
168 #endif /* MSP_SINGLE_THREAD */
169
170 114 ▶ if ( bytesRead > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( bytesRead > 0 ) { 114 0 114
171 /* Copy the relevant information out of the IPC header */
172 114 *nBytesRead = ipcHeader.dataLength;
173 114 memcpy( ec, ipcHeader.eyeCatcher, MSP_EC_LENGTH );
174 114 *retCode = ipcHeader.returnCode;
175 114 ▶ if ( phMsg != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( phMsg != NULL ) { 60 54 114
176 60 *phMsg = ipcHeader.hMsg;
177 }
178
179 /* Now copy the received data into the receive buffer, extending the buffer if required */
180 114 ▶ if ( ipcHeader.dataLength > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ipcHeader.dataLength > 0 ) { 53 61 114
181 53 ▶ if ( ipcHeader.dataLength <= *bufSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ipcHeader.dataLength <= *bufSize ) { 53 0 53
182 53 memcpy( *ppBuffer, ipcHeader.pData, ipcHeader.dataLength );
183 } else {
184 0 *ppBuffer = mspRealloc( comParms, *ppBuffer, ipcHeader.dataLength, *bufSize );
185 0 *bufSize = ipcHeader.dataLength;
186 0 memcpy( *ppBuffer, ipcHeader.pData, ipcHeader.dataLength );
187 }
188 }
189 } else {
190 0 *nBytesRead = 0;
191 0 memset( ec, ' ', MSP_EC_LENGTH );
192 0 *retCode = MQISDP_FAILED;
193 }
194
195 114 return rc;
196 }
197
198 /* Waits for a mutex to become signalled */
199 /* Returns 0 if this task owns the mutex */
200 /* Returns 1 otherwise */
201 57 DllExport int mspLockMutex( MTH mutexHandle ) {
202 57 int rc = 0;
203
204 #ifndef MSP_SINGLE_THREAD
205
206 #if defined(WIN32)
207 /* Wait forever */
208 rc = WaitForSingleObject( mutexHandle, INFINITE );
209 if ( rc == WAIT_TIMEOUT ) {
210 rc = 1;
211 }
212
213 #elif defined(UNIX)
214 struct sembuf sb;
215
216 sb.sem_num = 0;
217 sb.sem_op = -1;
218 /* If the process fails we want the kernel to undo the semaphore */
219 sb.sem_flg = SEM_UNDO;
220
221 if ( semop( mutexHandle, &sb, 1 ) < 0 ) {
222 rc = 1;
223 }
224
225 #endif
226
227 #else /* MSP_SINGLE_THREAD */
228 57 rc = 0;
229 #endif /* MSP_SINGLE_THREAD */
230
231 57 return rc;
232 }
233
234 /* mspReleaseMutex: Release the mutex identified by MTH */
235 57 DllExport int mspReleaseMutex( MTH mutexHandle ) {
236 57 int rc = 1;
237
238 #ifndef MSP_SINGLE_THREAD
239
240 #if defined(WIN32)
241
242 if ( ReleaseMutex( mutexHandle ) > 0 ) {
243 /* Success */
244 rc = 0;
245 }
246
247 #elif defined(UNIX)
248 struct sembuf sb;
249
250 sb.sem_num = 0;
251 sb.sem_op = 1;
252 sb.sem_flg = SEM_UNDO;
253
254 if ( semop( mutexHandle, &sb, 1 ) < 0 ) {
255 rc = 1;
256 } else {
257 rc = 0;
258 }
259
260 #endif
261
262 #else /* MSP_SINGLE_THREAD */
263 57 rc = 0;
264 #endif /* MSP_SINGLE_THREAD */
265
266 57 return rc;
267 }
268
269 /* Waits for a semaphore to become signalled */
270 /* Returns 0 if this task waits successfully for the semaphore */
271 /* Returns 1 otherwise e.g. timeout */
272 0 DllExport int mspWaitForSemaphore( MSH semHandle, long msTimeout ) {
273 0 int rc = 1;
274
275 #ifndef MSP_SINGLE_THREAD
276
277 #if defined(WIN32)
278 if ( msTimeout == -1 ) {
279 /* Wait forever */
280 rc = WaitForSingleObject( semHandle, INFINITE );
281 } else {
282 rc = WaitForSingleObject( semHandle, msTimeout );
283 }
284 if ( rc == WAIT_TIMEOUT ) {
285 rc = 1;
286 } else {
287 rc = 0;
288 }
289 #elif defined(UNIX)
290 struct timespec to;
291
292 /* Lock the pthread mutex */
293 /* Check if a message is already available */
294 /* If there is no message then wait to be signalled */
295 /* either wait forever, or on a timer */
296 /* The message flag is checked first, as a message may have arrived */
297 /* whilst we were not listening for condition variable events. */
298 pthread_mutex_lock( &semHandle->semLock );
299 if ( semHandle->msgAvailable == 0x00 ) {
300 /* No message available, so wait */
301 if ( msTimeout == -1 ) {
302 /* Wait forever */
303 rc = pthread_cond_wait( &semHandle->msgSignal, &semHandle->semLock );
304 } else {
305
306 to.tv_sec = time(NULL) + (int)(msTimeout / 1000);
307 to.tv_nsec = 0;
308 /* to.tv_nsec = (int)(msTimeout % 1000); */
309 rc = pthread_cond_timedwait( &semHandle->msgSignal, &semHandle->semLock, &to );
310 }
311
312 if ( rc == ETIMEDOUT ) {
313 rc = 1;
314 } else {
315 rc = 0;
316 }
317 } else {
318 /* A message is available */
319 rc = 0;
320 }
321 /* Reset the message available flag */
322 semHandle->msgAvailable = 0x00;
323 pthread_mutex_unlock( &semHandle->semLock );
324
325 #endif
326
327 #else /* MSP_SINGLE_THREAD */
328 0 rc = 0;
329 #endif /* MSP_SINGLE_THREAD */
330
331 0 return rc;
332 }
333
334 /* mspSignalSemaphore: Release the semaphore to indicate that data is available */
335 /* to be received. */
336 0 DllExport int mspSignalSemaphore( MSH semHandle ) {
337 0 int rc = 1;
338
339 #ifndef MSP_SINGLE_THREAD
340
341 #if defined(WIN32)
342 if ( ReleaseSemaphore( semHandle, 1, NULL ) > 0 ) {
343 /* Success */
344 rc = 0;
345 }
346
347 #elif defined(UNIX)
348
349 /* Lock the pthread mutex */
350 /* Set the message available flag */
351 /* Signal any waiters */
352 /* A message flag is used as well as signalling because a signal is only */
353 /* caught if there is a thread waiting. The msgAvailable flag holds the */
354 /* correct state regardless of whether there is a thread waiting. */
355 pthread_mutex_lock( &semHandle->semLock );
356
357 /* Set the message available flag and signal any waiters */
358 semHandle->msgAvailable = 0x01;
359 pthread_cond_signal( &semHandle->msgSignal );
360
361 pthread_mutex_unlock( &semHandle->semLock );
362 rc = 0;
363
364 #endif
365
366 #else /* MSP_SINGLE_THREAD */
367 0 rc = 0;
368 #endif /* MSP_SINGLE_THREAD */
369
370 0 return rc;
371 }
372
373
« src/ppersist.c ↑ Summary