src/mspstart.c

filter: Statementstmt 40.0% 4 / 10 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 40.0% 4 / 10 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/msptcp.c ↑ Summary src/mspclnt.c »
1 /****************************************************************************/
2 /* */
3 /* Program name: MQIsdp protocol C Language implementation */
4 /* */
5 /* Description: This source file contains the thread startup code for */
6 /* Windows and System V UNIX platforms. */
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/mspstart.c, SupportPacs, S000 1.3 03/11/28 16:44:01 */
15 /* */
16 /* Function: */
17 /* */
18 /* MQIsdp_StartTasks() starts the threads as appropriate for the Windows */
19 /* System V UNIX platforms. On these platforms it is appropriate to create */
20 /* the threads dynamically at runtime. On other platforms a different */
21 /* mechanism for starting the tasks may be required. */
22 /* */
23 /****************************************************************************/
24 /* */
25 /* Change history: */
26 /* */
27 /* V1.0 19-02-2003 IRH Initial release */
28 /* */
29 /*==========================================================================*/
30 /* Module Name: mspstart.c */
31 #include <mspsh.h>
32
33 #if defined(WIN32)
34 /* Prototypes for WIN32 send and receive tasks */
35 DWORD WINAPI mspWinStartSendTask( MQISDPTI *pTaskInfo );
36 DWORD WINAPI mspWinStartRcvTask( MQISDPTI *pTaskInfo );
37 static int mspWinStartTasks( MQISDPTI *pApiTaskInfo, MQISDPTI *pSendTaskInfo,
38 MQISDPTI *pRcvTaskInfo, char *pClientId );
39 #elif defined(UNIX)
40 /* Define a union required for semaphore initialisation */
41 union {
42 int val;
43 struct semid_ds *buf;
44 unsigned short *array;
45 }semctl_arg;
46
47 /* Prototypes for UNIX send and receive tasks */
48 void* mspUnixStartSendTask( void *pTaskInfo );
49 void* mspUnixStartRcvTask( void *pTaskInfo );
50 static int mspUnixStartTasks( MQISDPTI *pApiTaskInfo, MQISDPTI *pSendTaskInfo,
51 MQISDPTI *pRcvTaskInfo );
52 #endif
53
54 7 DllExport int MQIsdp_StartTasks( MQISDPTI *pApiTaskInfo, MQISDPTI *pSendTaskInfo,
55 MQISDPTI *pRcvTaskInfo, char *pClientId ) {
56 #if defined(WIN32)
57 return mspWinStartTasks( pApiTaskInfo, pSendTaskInfo, pRcvTaskInfo, pClientId );
58 #elif defined(UNIX)
59 7 return mspUnixStartTasks( pApiTaskInfo, pSendTaskInfo, pRcvTaskInfo );
60 #endif
61 }
62
63 #ifdef UNIX
64 7 static int mspUnixStartTasks( MQISDPTI *pApiTaskInfo, MQISDPTI *pSendTaskInfo,
65 MQISDPTI *pRcvTaskInfo ) {
66 #ifndef MSP_SINGLE_THREAD
67 pthread_t threadId;
68 pthread_attr_t threadArgs;
69 int apifd[2]; /* API thread pipe */
70 int sendfd[2]; /* SEND thread pipe */
71 int rcvfd[2]; /* RECEIVE thread pipe */
72 MTH mutexid;
73 MSH semHandle;
74
75 /* Create the server end of the mailboxes */
76 if ( pipe( apifd ) < 0 ) {
77 printf( "mspUnixStartTasks:API pipe error\n" );
78 }
79 if ( pipe( sendfd ) < 0 ) {
80 printf( "mspUnixStartTasks:SEND pipe error\n" );
81 }
82 if ( pipe( rcvfd ) < 0 ) {
83 printf( "mspUnixStartTasks:RCV pipe error\n" );
84 }
85
86 /* Create the mutex for coordinating the threads and initialise */
87 mutexid = semget( IPC_PRIVATE, 1, 0666 );
88 semctl_arg.val = 1;
89 semctl( mutexid, 0, SETVAL, semctl_arg );
90
91 /* Create the semaphore for coordinating blocking receives */
92 semHandle = (MSH)malloc( sizeof(MSH_S) );
93 pthread_mutex_init( &semHandle->semLock, NULL );
94 pthread_cond_init( &semHandle->msgSignal, NULL );
95 semHandle->msgAvailable = 0x00;
96
97 /* Create all the handles required by the api task */
98 /* API task requires: apiMailBox read */
99 /* sendMailbox write */
100 /* sendMutex */
101 /* receiveSemaphore */
102 pApiTaskInfo->apiMailbox = apifd[0];
103 pApiTaskInfo->sendMailbox = sendfd[1];
104 pApiTaskInfo->sendMutex = mutexid;
105 pApiTaskInfo->receiveSemaphore = semHandle;
106
107 /* Create all the mailbox handles required by send task */
108 /* Send task requires: apiMailBox write */
109 /* sendMailbox read */
110 /* receiveMailbox write */
111 /* sendMutex */
112 /* receiveSemaphore */
113 pSendTaskInfo->sendMailbox = sendfd[0];
114 pSendTaskInfo->apiMailbox = apifd[1];
115 pSendTaskInfo->receiveMailbox = rcvfd[1];
116 pSendTaskInfo->sendMutex = mutexid;
117 pSendTaskInfo->receiveSemaphore = semHandle;
118
119 /* Create all the handles required by the receive task */
120 /* Receive task requires: sendMailBox write */
121 /* receiveMailbox read */
122 /* sendMutex */
123 pRcvTaskInfo->receiveMailbox = rcvfd[0];
124 /* Duplicate the file descriptor as we have already used it for the api task */
125 pRcvTaskInfo->sendMailbox = dup( sendfd[1] );
126 pRcvTaskInfo->sendMutex = mutexid;
127
128 /* Fire up the receive task thread */
129 pthread_attr_init( &threadArgs );
130 pthread_attr_setdetachstate( &threadArgs, PTHREAD_CREATE_DETACHED );
131
132 if( pthread_create( &threadId, &threadArgs, mspUnixStartRcvTask, (void*)pRcvTaskInfo ) != 0 ) {
133 return 1;
134 }
135
136 /* Fire up the send task thread */
137 pthread_attr_init( &threadArgs );
138 pthread_attr_setdetachstate( &threadArgs, PTHREAD_CREATE_DETACHED );
139
140 if ( pthread_create( &threadId, &threadArgs, mspUnixStartSendTask, (void*)pSendTaskInfo ) != 0 ) {
141 return 1;
142 }
143
144 #endif
145 7 return 0;
146 }
147
148 0 void* mspUnixStartSendTask( void *pTaskInfo ) {
149 0 MQIsdp_SendTask( (MQISDPTI*)pTaskInfo );
150 0 return NULL;
151 }
152
153 0 void* mspUnixStartRcvTask( void *pTaskInfo ) {
154 0 MQIsdp_ReceiveTask( (MQISDPTI*)pTaskInfo );
155 0 return NULL;
156 }
157
158 #endif
159
160 #ifdef WIN32
161 static int mspWinStartTasks( MQISDPTI *pApiTaskInfo, MQISDPTI *pSendTaskInfo,
162 MQISDPTI *pRcvTaskInfo, char *pClientId ) {
163 #ifndef MSP_SINGLE_THREAD
164 HANDLE hThread;
165 DWORD threadId;
166 char *mailboxNameStem = "\\\\.\\mailslot\\MQISDP_";
167 int mailboxNameLen;
168 char *apiMailboxName = NULL;
169 char *sendMailboxName = NULL;
170 char *receiveMailboxName = NULL;
171 char *sendMutexName = NULL;
172 char *receiveSemName = NULL;
173
174 /* Use the clientId to make the various object names unique */
175 mailboxNameLen = strlen( mailboxNameStem ) + strlen( pClientId ) + 5;
176
177 apiMailboxName = (char*)malloc(mailboxNameLen);
178 sendMailboxName = (char*)malloc(mailboxNameLen);
179 receiveMailboxName = (char*)malloc(mailboxNameLen);
180 sendMutexName = (char*)malloc( strlen(pClientId) + 12 );
181 receiveSemName = (char*)malloc( strlen(pClientId) + 12 );
182
183 /* Set up the API mailbox name */
184 sprintf( apiMailboxName, "%sAPI_%s", mailboxNameStem, pClientId );
185
186 /* Set up the SEND mailbox name */
187 sprintf( sendMailboxName, "%sSND_%s", mailboxNameStem, pClientId );
188
189 /* Set up the RCV mailbox name */
190 sprintf( receiveMailboxName, "%sRCV_%s", mailboxNameStem, pClientId );
191
192 /* Set up the send mutex name */
193 sprintf( sendMutexName, "MQISDP_SND_%s", pClientId );
194
195 /* Set up the receive semaphore name */
196 sprintf( receiveSemName, "MQISDP_RCV_%s", pClientId );
197
198 /* Create the server end of the mailboxes */
199 pApiTaskInfo->apiMailbox = CreateMailslot( apiMailboxName , 40, 0, NULL );
200 pSendTaskInfo->sendMailbox = CreateMailslot( sendMailboxName , 40, 0, NULL );
201 pRcvTaskInfo->receiveMailbox = CreateMailslot( receiveMailboxName, 40, 0, NULL );
202
203 /* Create the mutex for coordinating the threads */
204 pSendTaskInfo->sendMutex = CreateMutex( NULL, FALSE, sendMutexName );
205
206 /* Create the semaphore for coordinating blocking receives */
207 pSendTaskInfo->receiveSemaphore = CreateSemaphore( NULL, 0, 1, receiveSemName );
208
209 /* Create all the handles required by the api task */
210 /* API task requires: apiMailBox */
211 /* sendMailbox */
212 /* sendMutex */
213 /* receiveSemaphore */
214 pApiTaskInfo->sendMailbox = CreateFile( sendMailboxName,
215 GENERIC_WRITE,
216 FILE_SHARE_READ | FILE_SHARE_WRITE,
217 NULL,
218 OPEN_EXISTING,
219 FILE_ATTRIBUTE_NORMAL,
220 NULL );
221
222 pApiTaskInfo->sendMutex = OpenMutex( MUTEX_ALL_ACCESS | SYNCHRONIZE, FALSE, sendMutexName );
223 pApiTaskInfo->receiveSemaphore = OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE, receiveSemName );
224
225 /* Create all the mailbox handles required by send task */
226 /* Send task requires: apiMailBox */
227 /* sendMailbox */
228 /* receiveMailbox */
229 /* sendMutex */
230 /* receiveSemaphore */
231 pSendTaskInfo->apiMailbox = CreateFile( apiMailboxName,
232 GENERIC_WRITE,
233 FILE_SHARE_READ | FILE_SHARE_WRITE,
234 NULL,
235 OPEN_EXISTING,
236 FILE_ATTRIBUTE_NORMAL,
237 NULL );
238
239 pSendTaskInfo->receiveMailbox = CreateFile( receiveMailboxName,
240 GENERIC_WRITE,
241 FILE_SHARE_READ | FILE_SHARE_WRITE,
242 NULL,
243 OPEN_EXISTING,
244 FILE_ATTRIBUTE_NORMAL,
245 NULL );
246
247 /* Create all the handles required by the receive task */
248 /* Receive task requires: sendMailBox */
249 /* receiveMailbox */
250 /* sendMutex */
251 pRcvTaskInfo->sendMailbox = CreateFile( sendMailboxName,
252 GENERIC_WRITE,
253 FILE_SHARE_READ | FILE_SHARE_WRITE,
254 NULL,
255 OPEN_EXISTING,
256 FILE_ATTRIBUTE_NORMAL,
257 NULL );
258
259 pRcvTaskInfo->sendMutex = OpenMutex( MUTEX_ALL_ACCESS | SYNCHRONIZE, FALSE, sendMutexName );
260
261 /* Fire up the receive task thread */
262 hThread = CreateThread( NULL, 0, mspWinStartRcvTask, pRcvTaskInfo, 0, &threadId );
263
264 if ( hThread == NULL ) {
265 return 1;
266 }
267
268 /* Fire up the send task thread */
269 hThread = CreateThread( NULL, 0, mspWinStartSendTask, pSendTaskInfo, 0, &threadId );
270
271 if ( hThread == NULL ) {
272 return 1;
273 }
274
275 #endif
276 return 0;
277 }
278
279 DWORD WINAPI mspWinStartSendTask( MQISDPTI *pTaskInfo ) {
280
281 MQIsdp_SendTask( pTaskInfo );
282
283 return 0;
284 }
285 DWORD WINAPI mspWinStartRcvTask( MQISDPTI *pTaskInfo ) {
286
287 MQIsdp_ReceiveTask( pTaskInfo );
288
289 return 0;
290 }
291 #endif
292
« src/msptcp.c ↑ Summary src/mspclnt.c »