| 1 |
/* |
| 2 |
* Component of the D-ITG 2.8.1 (r1023) platform (http://traffic.comics.unina.it/software/ITG) |
| 3 |
* |
| 4 |
* Copyright : (C) 2004-2013 by Alessio Botta, Walter de Donato, Alberto Dainotti, |
| 5 |
* Stefano Avallone, Antonio Pescape' (PI) |
| 6 |
* COMICS (COMputer for Interaction and CommunicationS) Group |
| 7 |
* Department of Electrical Engineering and Information Technologies |
| 8 |
* University of Napoli "Federico II". |
| 9 |
* email : [email protected], [email protected], [email protected], |
| 10 |
* [email protected], [email protected] |
| 11 |
* |
| 12 |
* This program is free software: you can redistribute it and/or modify |
| 13 |
* it under the terms of the GNU General Public License as published by |
| 14 |
* the Free Software Foundation, either version 3 of the License, or |
| 15 |
* (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 |
* |
| 25 |
* For commercial use please refer to D-ITG Professional. |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
#ifdef WIN32 |
| 30 |
int num = 0; |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "thread.h" |
| 34 |
#include <stdio.h> |
| 35 |
#include "debug.h" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
int createThread(void *argument, void *(nameFunction) (void *), void *attrib, pthread_t &idThread, bool detach) |
| 41 |
{ |
| 42 |
|
| 43 |
int ret = 0; |
| 44 |
#ifdef WIN32 |
| 45 |
|
| 46 |
LPDWORD pid = 0; |
| 47 |
|
| 48 |
idThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) nameFunction, argument, |
| 49 |
(DWORD) NULL, pid); |
| 50 |
PRINTD(3,"createThread: Return value CreateThread (WIN32) %lu\n",(unsigned long int)idThread); |
| 51 |
if (idThread == NULL) ret = -1; |
| 52 |
PRINTD(3,"createThread: Return value CreateThread: %d\n",ret); |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef UNIX |
| 56 |
|
| 57 |
|
| 58 |
ret = pthread_create(&idThread, NULL, nameFunction, argument); |
| 59 |
if (ret != 0) ret = -1; |
| 60 |
if (detach) pthread_detach(idThread); |
| 61 |
PRINTD(3,"createThread: Return value pthread_create (LINUX) %lu\n",(unsigned long int)idThread); |
| 62 |
PRINTD(3,"createThread: Return value createThread : %d\n",ret); |
| 63 |
#endif |
| 64 |
return ret; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
int terminateThread(pthread_t idThread) |
| 69 |
{ |
| 70 |
|
| 71 |
int ret = 0; |
| 72 |
#ifdef WIN32 |
| 73 |
|
| 74 |
if (idThread!=0) |
| 75 |
ret = TerminateThread((HANDLE) idThread, 0); |
| 76 |
|
| 77 |
if (ret == 0) ret = -1; |
| 78 |
else ret = 0; |
| 79 |
#endif |
| 80 |
#ifdef UNIX |
| 81 |
|
| 82 |
if (idThread != 0) |
| 83 |
ret = pthread_cancel(idThread); |
| 84 |
|
| 85 |
if (ret != 0) |
| 86 |
ret = -1; |
| 87 |
#endif |
| 88 |
PRINTD(3,"CreateThread: Return value terminateThread : %d\n",ret); |
| 89 |
return ret; |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
void exitThread() |
| 94 |
{ |
| 95 |
#ifdef WIN32 |
| 96 |
|
| 97 |
ExitThread(0); |
| 98 |
#endif |
| 99 |
#ifdef UNIX |
| 100 |
|
| 101 |
pthread_exit(0); |
| 102 |
#endif |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
int joinThread(int numFlow, pthread_t hThr[]) |
| 107 |
{ |
| 108 |
int ret = 0; |
| 109 |
#ifdef WIN32 |
| 110 |
DWORD temp = 0; |
| 111 |
|
| 112 |
temp = WaitForMultipleObjects(numFlow, (const HANDLE *) hThr, TRUE, INFINITE); |
| 113 |
PRINTD(3,"joinThread: Return value WaitForMultipleObjects(WIN32) : %d\n",(int)temp); |
| 114 |
if (temp == WAIT_FAILED) ret = -1; |
| 115 |
#endif |
| 116 |
#ifdef UNIX |
| 117 |
|
| 118 |
int ret2 = 0; |
| 119 |
for(int i = 0; i < numFlow; i++) { |
| 120 |
ret2 = pthread_join(hThr[i], NULL); |
| 121 |
if (ret2 != 0) |
| 122 |
ret = -1; |
| 123 |
} |
| 124 |
#endif |
| 125 |
PRINTD(3,"joinThread: Return value joinThread : %d\n",ret); |
| 126 |
return ret; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#ifdef WIN32 |
| 132 |
|
| 133 |
int mutexThreadInit(HANDLE &mutex) |
| 134 |
{ |
| 135 |
int ret = 0; |
| 136 |
|
| 137 |
char nameMutex[30] = ""; |
| 138 |
|
| 139 |
|
| 140 |
snprintf(nameMutex, 30, "%s%d", nameProgram, ++num); |
| 141 |
|
| 142 |
mutex = CreateMutex(NULL, FALSE, nameMutex); |
| 143 |
|
| 144 |
PRINTD(3,"mutexThreadInit: Return value CreateMutex (WIN32) : %d \n",(int) mutex); |
| 145 |
PRINTD(3,"mutexThreadInit: Name mutex %s \n", nameMutex); |
| 146 |
|
| 147 |
if (mutex == NULL) ret = -1; |
| 148 |
PRINTD(3,"mutexThreadInit: Return value mutexThreadInit : %d \n",ret); |
| 149 |
return ret; |
| 150 |
} |
| 151 |
#endif |
| 152 |
|
| 153 |
#ifdef UNIX |
| 154 |
|
| 155 |
int mutexThreadInit(void* mutex) |
| 156 |
{ |
| 157 |
int ret = 0; |
| 158 |
|
| 159 |
ret = pthread_mutex_init((pthread_mutex_t *)mutex, NULL); |
| 160 |
PRINTD(3,"mutexThreadInit: Return value pthread_mutex_init (LINUX) : %p \n", mutex); |
| 161 |
PRINTD(3,"mutexThreadInit: Return value mutexThreadInit : %d \n",ret); |
| 162 |
return ret; |
| 163 |
} |
| 164 |
#endif |
| 165 |
|
| 166 |
|
| 167 |
int mutexThreadLock(void *mutex) |
| 168 |
{ |
| 169 |
int ret = 0; |
| 170 |
#ifdef UNIX |
| 171 |
|
| 172 |
ret = pthread_mutex_lock((pthread_mutex_t *) mutex); |
| 173 |
if (ret != 0 ) ret = -1; |
| 174 |
#endif |
| 175 |
#ifdef WIN32 |
| 176 |
DWORD temp = 0; |
| 177 |
|
| 178 |
temp = WaitForSingleObject((HANDLE)mutex,INFINITE); |
| 179 |
PRINTD(3,"mutexThreadLock: Return value WaitForSingleObject(WIN32) : %d\n",(int)temp); |
| 180 |
if (temp == WAIT_FAILED) ret = -1; |
| 181 |
#endif |
| 182 |
PRINTD(3,"mutexThreadLock: Return value mutexThreadLock : %d\n",ret); |
| 183 |
return ret; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
int mutexThreadUnlock(void *mutex) |
| 188 |
{ |
| 189 |
int ret = 0; |
| 190 |
#ifdef UNIX |
| 191 |
|
| 192 |
ret = pthread_mutex_unlock((pthread_mutex_t *) mutex); |
| 193 |
if (ret != 0 ) ret = -1; |
| 194 |
#endif |
| 195 |
#ifdef WIN32 |
| 196 |
BOOL flag = 0; |
| 197 |
|
| 198 |
flag = ReleaseMutex((HANDLE)mutex); |
| 199 |
PRINTD(3,"mutexThreadUnlock: Return value ReleaseMutex(WIN32) in mutexThreadUnlock : %d\n",flag); |
| 200 |
if (flag == 0) ret = -1; |
| 201 |
#endif |
| 202 |
PRINTD(3,"mutexThreadUnlock: Return value mutexThreadUnlock : %d\n",ret); |
| 203 |
return ret; |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
int mutexThreadRelease(void* mutex) |
| 209 |
{ |
| 210 |
int ret = 0; |
| 211 |
#ifdef UNIX |
| 212 |
|
| 213 |
ret = pthread_mutex_destroy((pthread_mutex_t *)mutex); |
| 214 |
if (ret != 0) ret = -1; |
| 215 |
#endif |
| 216 |
#ifdef WIN32 |
| 217 |
BOOL flag = 0; |
| 218 |
|
| 219 |
flag = CloseHandle((HANDLE)mutex); |
| 220 |
if (flag == 0) ret = -1; |
| 221 |
#endif |
| 222 |
PRINTD(3,"mutexThreadRelease: Return value mutexThreadRelease : %d\n",ret); |
| 223 |
return ret; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
int closeSock(int socket) |
| 231 |
{ |
| 232 |
int ret = 0; |
| 233 |
#if defined UNIX |
| 234 |
|
| 235 |
ret = close(socket); |
| 236 |
#endif |
| 237 |
#ifdef WIN32 |
| 238 |
|
| 239 |
ret = closesocket(socket); |
| 240 |
if (ret == SOCKET_ERROR) ret = -1; |
| 241 |
#endif |
| 242 |
PRINTD(3,"closeSock: Return value closeSock : %d\n",ret); |
| 243 |
return ret; |
| 244 |
}; |