| 1 |
/* |
| 2 |
* Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This Software is licensed under one of the following licenses: |
| 5 |
* |
| 6 |
* 1) under the terms of the "Common Public License 1.0" a copy of which is |
| 7 |
* in the file LICENSE.txt in the root directory. The license is also |
| 8 |
* available from the Open Source Initiative, see |
| 9 |
* http://www.opensource.org/licenses/cpl.php. |
| 10 |
* |
| 11 |
* 2) under the terms of the "The BSD License" a copy of which is in the file |
| 12 |
* LICENSE2.txt in the root directory. The license is also available from |
| 13 |
* the Open Source Initiative, see |
| 14 |
* http://www.opensource.org/licenses/bsd-license.php. |
| 15 |
* |
| 16 |
* 3) under the terms of the "GNU General Public License (GPL) Version 2" a |
| 17 |
* copy of which is in the file LICENSE3.txt in the root directory. The |
| 18 |
* license is also available from the Open Source Initiative, see |
| 19 |
* http://www.opensource.org/licenses/gpl-license.php. |
| 20 |
* |
| 21 |
* Licensee has the right to choose one of the above licenses. |
| 22 |
* |
| 23 |
* Redistributions of source code must retain the above copyright |
| 24 |
* notice and one of the license notices. |
| 25 |
* |
| 26 |
* Redistributions in binary form must reproduce both the above copyright |
| 27 |
* notice, one of the license notices in the documentation |
| 28 |
* and/or other materials provided with the distribution. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "dapl_mdep.h" |
| 32 |
#include "dapl_proto.h" |
| 33 |
|
| 34 |
#include <process.h> |
| 35 |
|
| 36 |
#include "dapl_test_data.h" /* for alloc_count */ |
| 37 |
|
| 38 |
/* |
| 39 |
* Machine dependant initialization |
| 40 |
*/ |
| 41 |
|
| 42 |
void |
| 43 |
DT_Mdep_Init (void) |
| 44 |
{ |
| 45 |
/* Initialize winsic2 */ |
| 46 |
/* |
| 47 |
* Needs to be done in main(), it's too late here. |
| 48 |
*/ |
| 49 |
// WORD wv = MAKEWORD (1, 1); |
| 50 |
// WSADATA wsaData; |
| 51 |
// WSAStartup (wv, &wsaData); |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* Machine dependant deinitialization |
| 56 |
*/ |
| 57 |
|
| 58 |
void |
| 59 |
DT_Mdep_End (void) |
| 60 |
{ |
| 61 |
WSACleanup (); |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* Generate name of IB device |
| 66 |
*/ |
| 67 |
|
| 68 |
bool |
| 69 |
DT_Mdep_GetDefaultDeviceName (char *dapl_name) |
| 70 |
{ |
| 71 |
strcpy (dapl_name, DT_MdepDeviceName); |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* Sleep specified number of milliseconds |
| 77 |
*/ |
| 78 |
|
| 79 |
void |
| 80 |
DT_Mdep_Sleep (int msec) |
| 81 |
{ |
| 82 |
Sleep (msec); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Get system statistics including uptime and idle time |
| 87 |
*/ |
| 88 |
void |
| 89 |
DT_Mdep_Schedule (void) |
| 90 |
{ |
| 91 |
/* nothing here */ |
| 92 |
} |
| 93 |
|
| 94 |
bool |
| 95 |
DT_Mdep_GetCpuStat ( |
| 96 |
DT_CpuStat *cpu_stat ) |
| 97 |
{ |
| 98 |
/* FIXME not implemented */ |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
* Get current time in milliseconds (relative to some fixed point) |
| 104 |
*/ |
| 105 |
unsigned long |
| 106 |
DT_Mdep_GetTime (void) |
| 107 |
{ |
| 108 |
return GetTickCount (); |
| 109 |
} |
| 110 |
|
| 111 |
double |
| 112 |
DT_Mdep_GetCpuMhz ( |
| 113 |
void ) |
| 114 |
{ |
| 115 |
LONG retVal; |
| 116 |
HKEY hKey; |
| 117 |
DWORD cpuSpeed = 0; |
| 118 |
DWORD dataSize = sizeof (DWORD); |
| 119 |
|
| 120 |
/* For windows need to query the registry to get the CPU |
| 121 |
* Information...-SVSV */ |
| 122 |
retVal = RegOpenKeyEx (HKEY_LOCAL_MACHINE, |
| 123 |
TEXT ("Hardware\\Description\\System\\CentralProcessor\\0"), |
| 124 |
0, |
| 125 |
KEY_QUERY_VALUE, |
| 126 |
&hKey); |
| 127 |
|
| 128 |
if (retVal == ERROR_SUCCESS) |
| 129 |
{ |
| 130 |
retVal = RegQueryValueEx (hKey, |
| 131 |
TEXT ("~MHz"), NULL, NULL, |
| 132 |
(LPBYTE)&cpuSpeed, &dataSize); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
RegCloseKey (hKey); |
| 137 |
|
| 138 |
return cpuSpeed; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
unsigned long |
| 143 |
DT_Mdep_GetContextSwitchNum (void) |
| 144 |
{ |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* Memory allocate and free routines for control blocks (objects) - regular |
| 150 |
* memory, always zeroed. |
| 151 |
*/ |
| 152 |
void * |
| 153 |
DT_Mdep_Malloc (size_t l_) |
| 154 |
{ |
| 155 |
void *rval; |
| 156 |
|
| 157 |
/* |
| 158 |
* check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count++; |
| 159 |
* DT_Mdep_Unlock(&Alloc_Count_Lock); |
| 160 |
*/ |
| 161 |
|
| 162 |
rval = malloc (l_); |
| 163 |
|
| 164 |
if (rval) |
| 165 |
{ |
| 166 |
memset (rval, 0, l_); |
| 167 |
} |
| 168 |
return ( rval ); |
| 169 |
} |
| 170 |
|
| 171 |
void |
| 172 |
DT_Mdep_Free (void *a_) |
| 173 |
{ |
| 174 |
/* |
| 175 |
* check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count--; |
| 176 |
* DT_Mdep_Unlock(&Alloc_Count_Lock); |
| 177 |
*/ |
| 178 |
|
| 179 |
free (a_); |
| 180 |
} |
| 181 |
|
| 182 |
/* |
| 183 |
* Lock support |
| 184 |
* |
| 185 |
* Lock object constructor |
| 186 |
*/ |
| 187 |
bool |
| 188 |
DT_Mdep_LockInit (DT_Mdep_LockType * lock_ptr) |
| 189 |
{ |
| 190 |
*lock_ptr = CreateMutex (0, FALSE, 0); |
| 191 |
return *lock_ptr ? true : false; |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* Lock object destructor |
| 196 |
*/ |
| 197 |
void |
| 198 |
DT_Mdep_LockDestroy (DT_Mdep_LockType * lock_ptr) |
| 199 |
{ |
| 200 |
CloseHandle (*lock_ptr); |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
* Lock |
| 205 |
*/ |
| 206 |
void |
| 207 |
DT_Mdep_Lock (DT_Mdep_LockType * lock_ptr) |
| 208 |
{ |
| 209 |
WaitForSingleObject (*lock_ptr, INFINITE); |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
* unlock |
| 214 |
*/ |
| 215 |
void |
| 216 |
DT_Mdep_Unlock (DT_Mdep_LockType * lock_ptr) |
| 217 |
{ |
| 218 |
ReleaseMutex (*lock_ptr); |
| 219 |
} |
| 220 |
|
| 221 |
/* |
| 222 |
* Init Thread Attributes |
| 223 |
*/ |
| 224 |
void |
| 225 |
DT_Mdep_Thread_Init_Attributes (Thread * thread_ptr) |
| 226 |
{ |
| 227 |
/* nothing */ |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
* Destroy Thread Attributes |
| 232 |
*/ |
| 233 |
void |
| 234 |
DT_Mdep_Thread_Destroy_Attributes (Thread * thread_ptr) |
| 235 |
{ |
| 236 |
/* nothing */ |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
* Start the thread |
| 241 |
*/ |
| 242 |
bool |
| 243 |
DT_Mdep_Thread_Start (Thread * thread_ptr) |
| 244 |
{ |
| 245 |
thread_ptr->thread_handle = |
| 246 |
CreateThread (NULL, |
| 247 |
0, |
| 248 |
(LPTHREAD_START_ROUTINE)DT_Mdep_Thread_Start_Routine, |
| 249 |
thread_ptr, |
| 250 |
0, |
| 251 |
NULL); |
| 252 |
if (thread_ptr->thread_handle == NULL) |
| 253 |
{ |
| 254 |
return false; |
| 255 |
} |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* Thread execution entry point function |
| 261 |
*/ |
| 262 |
DT_Mdep_Thread_Start_Routine_Return_Type |
| 263 |
DT_Mdep_Thread_Start_Routine (void *thread_handle) |
| 264 |
{ |
| 265 |
Thread *thread_ptr; |
| 266 |
thread_ptr = (Thread *) thread_handle; |
| 267 |
|
| 268 |
thread_ptr->function (thread_ptr->param); |
| 269 |
} |
| 270 |
|
| 271 |
/* |
| 272 |
* Thread detach routine. Allows the pthreads |
| 273 |
* interface to clean up resources properly at |
| 274 |
* thread's end. |
| 275 |
*/ |
| 276 |
void DT_Mdep_Thread_Detach (DT_Mdep_ThreadHandleType thread_id ) /* AMM */ |
| 277 |
{ |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Allows a thread to get its own ID so it |
| 282 |
* can pass it to routines wanting to act |
| 283 |
* upon themselves. |
| 284 |
*/ |
| 285 |
|
| 286 |
DT_Mdep_ThreadHandleType DT_Mdep_Thread_SELF (void) /* AMM */ |
| 287 |
{ |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
/* |
| 293 |
* Allow a thread to exit and cleanup resources. |
| 294 |
*/ |
| 295 |
|
| 296 |
void DT_Mdep_Thread_EXIT ( void * thread_handle ) /* AMM */ |
| 297 |
{ |
| 298 |
|
| 299 |
/* nothing */ |
| 300 |
} |
| 301 |
|
| 302 |
/* |
| 303 |
* DT_Mdep_wait_object_init |
| 304 |
* |
| 305 |
* Initialize a wait object |
| 306 |
* |
| 307 |
* Input: |
| 308 |
* wait_obj |
| 309 |
* |
| 310 |
* Returns: |
| 311 |
* 0 if successful |
| 312 |
* -1 if unsuccessful |
| 313 |
*/ |
| 314 |
int |
| 315 |
DT_Mdep_wait_object_init ( |
| 316 |
IN DT_WAIT_OBJECT *wait_obj) |
| 317 |
{ |
| 318 |
|
| 319 |
*wait_obj = CreateEvent (NULL, FALSE, FALSE, NULL); |
| 320 |
|
| 321 |
if ( *wait_obj == NULL ) |
| 322 |
{ |
| 323 |
return -1; |
| 324 |
} |
| 325 |
|
| 326 |
return 0; |
| 327 |
|
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
/* Wait on the supplied wait object, up to the specified time_out. |
| 333 |
* A timeout of DAT_TIMEOUT_INFINITE will wait indefinitely. |
| 334 |
* Timeout should be specified in micro seconds. |
| 335 |
* |
| 336 |
* Functional returns: |
| 337 |
* 0 -- another thread invoked dapl_os_wait object_wakeup |
| 338 |
* -1 -- someone else is already waiting in this wait |
| 339 |
* object. |
| 340 |
* only one waiter is allowed at a time. |
| 341 |
* -1 -- another thread invoked dapl_os_wait_object_destroy |
| 342 |
* -1 -- the specified time limit was reached. |
| 343 |
*/ |
| 344 |
|
| 345 |
int |
| 346 |
DT_Mdep_wait_object_wait ( |
| 347 |
IN DT_WAIT_OBJECT *wait_obj, |
| 348 |
IN int timeout_val) |
| 349 |
{ |
| 350 |
|
| 351 |
DAT_RETURN status; |
| 352 |
DWORD op_status; |
| 353 |
|
| 354 |
status = DAT_SUCCESS; |
| 355 |
|
| 356 |
if ( DAT_TIMEOUT_INFINITE == timeout_val ) |
| 357 |
{ |
| 358 |
op_status = WaitForSingleObject (*wait_obj, INFINITE); |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
/* convert to milliseconds */ |
| 363 |
op_status = WaitForSingleObject (*wait_obj, timeout_val/1000); |
| 364 |
} |
| 365 |
|
| 366 |
if (op_status == WAIT_TIMEOUT) |
| 367 |
{ |
| 368 |
status = DAT_CLASS_ERROR | DAT_TIMEOUT_EXPIRED; |
| 369 |
} |
| 370 |
else if ( op_status == WAIT_FAILED) |
| 371 |
{ |
| 372 |
status = DAT_CLASS_ERROR | DAT_INTERNAL_ERROR; |
| 373 |
} |
| 374 |
|
| 375 |
return status; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
/* |
| 380 |
* DT_Mdep_wait_object_wakeup |
| 381 |
* |
| 382 |
* Wakeup a thread waiting on a wait object |
| 383 |
* |
| 384 |
* Input: |
| 385 |
* wait_obj |
| 386 |
* |
| 387 |
* Returns: |
| 388 |
* 0 if successful |
| 389 |
* -1 if not successful |
| 390 |
*/ |
| 391 |
int |
| 392 |
DT_Mdep_wait_object_wakeup ( |
| 393 |
DT_WAIT_OBJECT *wait_obj ) |
| 394 |
{ |
| 395 |
DWORD op_status; |
| 396 |
|
| 397 |
op_status = SetEvent (*wait_obj); |
| 398 |
if ( op_status == 0 ) |
| 399 |
{ |
| 400 |
return DAT_CLASS_ERROR | DAT_INTERNAL_ERROR; |
| 401 |
} |
| 402 |
|
| 403 |
return DAT_SUCCESS; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
/* |
| 408 |
* DT_Mdep_wait_object_destroy |
| 409 |
* |
| 410 |
* Destroy a wait object |
| 411 |
* |
| 412 |
* Input: |
| 413 |
* wait_obj |
| 414 |
* |
| 415 |
* Returns: |
| 416 |
* 0 if successful |
| 417 |
* -1 if not successful |
| 418 |
*/ |
| 419 |
int |
| 420 |
DT_Mdep_wait_object_destroy ( |
| 421 |
IN DT_WAIT_OBJECT *wait_obj) |
| 422 |
{ |
| 423 |
|
| 424 |
DWORD op_status; |
| 425 |
DAT_RETURN status = DAT_SUCCESS; |
| 426 |
|
| 427 |
op_status = CloseHandle (*wait_obj); |
| 428 |
|
| 429 |
if ( op_status == 0 ) |
| 430 |
{ |
| 431 |
status = DAT_CLASS_ERROR | DAT_INTERNAL_ERROR; |
| 432 |
} |
| 433 |
|
| 434 |
return status; |
| 435 |
} |
| 436 |
|