| 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 <sys/times.h> |
| 35 |
#include <limits.h> |
| 36 |
#include <sys/time.h> |
| 37 |
#include <stdlib.h> /* needed for getenv() */ |
| 38 |
#include <pthread.h> /* needed for pthread_atfork() */ |
| 39 |
#include <signal.h> /* needed for thread setup */ |
| 40 |
|
| 41 |
#include "dapl_test_data.h" /* for alloc_count */ |
| 42 |
|
| 43 |
|
| 44 |
/* |
| 45 |
* Machine dependant initialization |
| 46 |
*/ |
| 47 |
|
| 48 |
void |
| 49 |
DT_Mdep_Init (void) |
| 50 |
{ |
| 51 |
/* nothing */ |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* Machine dependant deinitialization |
| 56 |
*/ |
| 57 |
|
| 58 |
void |
| 59 |
DT_Mdep_End (void) |
| 60 |
{ |
| 61 |
/* nothing */ |
| 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 |
struct timespec t; |
| 83 |
t.tv_sec = msec / 1000; /* Whole seconds */ |
| 84 |
t.tv_nsec = (msec % 1000) * 1000 * 1000; |
| 85 |
nanosleep (&t, 0); |
| 86 |
} |
| 87 |
|
| 88 |
void |
| 89 |
DT_Mdep_Schedule (void) |
| 90 |
{ |
| 91 |
/* void */ |
| 92 |
} |
| 93 |
/* |
| 94 |
* Get system statistics including uptime and idle time |
| 95 |
*/ |
| 96 |
|
| 97 |
bool |
| 98 |
DT_Mdep_GetCpuStat ( |
| 99 |
DT_CpuStat *cpu_stat ) |
| 100 |
{ |
| 101 |
/* FIXME not implemented */ |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* Get current time in milliseconds (relative to some fixed point) |
| 107 |
*/ |
| 108 |
unsigned long |
| 109 |
DT_Mdep_GetTime (void) |
| 110 |
{ |
| 111 |
struct tms ts; |
| 112 |
clock_t t = times (&ts); |
| 113 |
return (unsigned long) ((DAT_UINT64) t * 1000 / CLK_TCK); |
| 114 |
} |
| 115 |
|
| 116 |
double |
| 117 |
DT_Mdep_GetCpuMhz ( |
| 118 |
void ) |
| 119 |
{ |
| 120 |
#error "Undefined Platform" |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
unsigned long |
| 125 |
DT_Mdep_GetContextSwitchNum (void ) |
| 126 |
{ |
| 127 |
#error "Undefined Platform" |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
* Memory allocate and free routines for control blocks (objects) - regular |
| 132 |
* memory, always zeroed. |
| 133 |
*/ |
| 134 |
void * |
| 135 |
DT_Mdep_Malloc (size_t l_) |
| 136 |
{ |
| 137 |
void *rval; |
| 138 |
|
| 139 |
/* |
| 140 |
* check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count++; |
| 141 |
* DT_Mdep_Unlock(&Alloc_Count_Lock); |
| 142 |
*/ |
| 143 |
|
| 144 |
rval = malloc (l_); |
| 145 |
|
| 146 |
if (rval) |
| 147 |
{ |
| 148 |
memset (rval, 0, l_); |
| 149 |
} |
| 150 |
return ( rval ); |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
DT_Mdep_Free (void *a_) |
| 155 |
{ |
| 156 |
/* |
| 157 |
* check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count--; |
| 158 |
* DT_Mdep_Unlock(&Alloc_Count_Lock); |
| 159 |
*/ |
| 160 |
|
| 161 |
free (a_); |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* Lock support |
| 166 |
* |
| 167 |
* Lock object constructor |
| 168 |
*/ |
| 169 |
bool |
| 170 |
DT_Mdep_LockInit (DT_Mdep_LockType * lock_ptr) |
| 171 |
{ |
| 172 |
return pthread_mutex_init (lock_ptr, 0) ? false : true; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* Lock object destructor |
| 177 |
*/ |
| 178 |
void |
| 179 |
DT_Mdep_LockDestroy (DT_Mdep_LockType * lock_ptr) |
| 180 |
{ |
| 181 |
pthread_mutex_destroy (lock_ptr); |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* Lock |
| 186 |
*/ |
| 187 |
void |
| 188 |
DT_Mdep_Lock (DT_Mdep_LockType * lock_ptr) |
| 189 |
{ |
| 190 |
pthread_mutex_lock (lock_ptr); |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
* unlock |
| 195 |
*/ |
| 196 |
void |
| 197 |
DT_Mdep_Unlock (DT_Mdep_LockType * lock_ptr) |
| 198 |
{ |
| 199 |
pthread_mutex_unlock (lock_ptr); |
| 200 |
} |
| 201 |
|
| 202 |
/* |
| 203 |
* Init Thread Attributes |
| 204 |
*/ |
| 205 |
void |
| 206 |
DT_Mdep_Thread_Init_Attributes (Thread * thread_ptr) |
| 207 |
{ |
| 208 |
pthread_attr_init (&thread_ptr->attr); |
| 209 |
pthread_attr_setstacksize (&thread_ptr->attr, thread_ptr->stacksize); |
| 210 |
/* Create thread in detached state to free resources on termination; |
| 211 |
* this precludes doing a pthread_join, but we don't do it |
| 212 |
*/ |
| 213 |
pthread_attr_setdetachstate (&thread_ptr->attr, PTHREAD_CREATE_DETACHED); |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
* Destroy Thread Attributes |
| 218 |
*/ |
| 219 |
void |
| 220 |
DT_Mdep_Thread_Destroy_Attributes (Thread * thread_ptr) |
| 221 |
{ |
| 222 |
pthread_attr_destroy (&thread_ptr->attr); |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* Start the thread |
| 227 |
*/ |
| 228 |
bool |
| 229 |
DT_Mdep_Thread_Start (Thread * thread_ptr) |
| 230 |
{ |
| 231 |
return pthread_create (&thread_ptr->thread_handle, |
| 232 |
&thread_ptr->attr, |
| 233 |
DT_Mdep_Thread_Start_Routine, |
| 234 |
thread_ptr) == 0; |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* Thread execution entry point function |
| 239 |
*/ |
| 240 |
DT_Mdep_Thread_Start_Routine_Return_Type |
| 241 |
DT_Mdep_Thread_Start_Routine (void *thread_handle) |
| 242 |
{ |
| 243 |
Thread *thread_ptr; |
| 244 |
thread_ptr = (Thread *) thread_handle; |
| 245 |
|
| 246 |
thread_ptr->function (thread_ptr->param); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* Thread detach routine. Allows the pthreads |
| 252 |
* interface to clean up resources properly at |
| 253 |
* thread's end. |
| 254 |
*/ |
| 255 |
void DT_Mdep_Thread_Detach ( int thread_id ) /* AMM */ |
| 256 |
{ |
| 257 |
pthread_detach ( thread_id); |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* Allows a thread to get its own ID so it |
| 262 |
* can pass it to routines wanting to act |
| 263 |
* upon themselves. |
| 264 |
*/ |
| 265 |
|
| 266 |
int DT_Mdep_Thread_SELF (void) /* AMM */ |
| 267 |
{ |
| 268 |
|
| 269 |
return (pthread_self ()); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
/* |
| 274 |
* Allow a thread to exit and cleanup resources. |
| 275 |
*/ |
| 276 |
|
| 277 |
void DT_Mdep_Thread_EXIT ( void * thread_handle ) /* AMM */ |
| 278 |
{ |
| 279 |
pthread_exit ( thread_handle ); |
| 280 |
} |
| 281 |
|
| 282 |
/* |
| 283 |
* DT_Mdep_wait_object_init |
| 284 |
* |
| 285 |
* Initialize a wait object |
| 286 |
* |
| 287 |
* Input: |
| 288 |
* wait_obj |
| 289 |
* |
| 290 |
* Returns: |
| 291 |
* 0 if successful |
| 292 |
* -1 if unsuccessful |
| 293 |
*/ |
| 294 |
int |
| 295 |
DT_Mdep_wait_object_init ( |
| 296 |
IN DT_WAIT_OBJECT *wait_obj) |
| 297 |
{ |
| 298 |
|
| 299 |
wait_obj->signaled = DAT_FALSE; |
| 300 |
if ( 0 != pthread_cond_init ( &wait_obj->cv, NULL ) ) |
| 301 |
{ |
| 302 |
return (-1); |
| 303 |
} |
| 304 |
|
| 305 |
/* Always returns 0. */ |
| 306 |
pthread_mutex_init ( &wait_obj->lock, NULL ); |
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
/* Wait on the supplied wait object, up to the specified time_out. |
| 312 |
* A timeout of DAT_TIMEOUT_INFINITE will wait indefinitely. |
| 313 |
* Timeout should be specified in micro seconds. |
| 314 |
* |
| 315 |
* Functional returns: |
| 316 |
* 0 -- another thread invoked dapl_os_wait object_wakeup |
| 317 |
* -1 -- someone else is already waiting in this wait |
| 318 |
* object. |
| 319 |
* only one waiter is allowed at a time. |
| 320 |
* -1 -- another thread invoked dapl_os_wait_object_destroy |
| 321 |
* -1 -- the specified time limit was reached. |
| 322 |
*/ |
| 323 |
|
| 324 |
int |
| 325 |
DT_Mdep_wait_object_wait ( |
| 326 |
IN DT_WAIT_OBJECT *wait_obj, |
| 327 |
IN int timeout_val) |
| 328 |
{ |
| 329 |
|
| 330 |
int dat_status; |
| 331 |
int pthread_status; |
| 332 |
struct timespec future; |
| 333 |
|
| 334 |
dat_status = 0; |
| 335 |
pthread_status = 0; |
| 336 |
|
| 337 |
if ( timeout_val != DAT_TIMEOUT_INFINITE ) |
| 338 |
{ |
| 339 |
struct timeval now; |
| 340 |
struct timezone tz; |
| 341 |
unsigned int microsecs; |
| 342 |
|
| 343 |
gettimeofday (&now, &tz); |
| 344 |
microsecs = now.tv_usec + (timeout_val % 1000000); |
| 345 |
if (microsecs > 1000000) |
| 346 |
{ |
| 347 |
now.tv_sec = now.tv_sec + timeout_val / 1000000 + 1; |
| 348 |
now.tv_usec = microsecs - 1000000; |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
now.tv_sec = now.tv_sec + timeout_val / 1000000; |
| 353 |
now.tv_usec = microsecs; |
| 354 |
} |
| 355 |
|
| 356 |
/* Convert timeval to timespec */ |
| 357 |
future.tv_sec = now.tv_sec; |
| 358 |
future.tv_nsec = now.tv_usec * 1000; |
| 359 |
|
| 360 |
pthread_mutex_lock (&wait_obj->lock); |
| 361 |
while ( wait_obj->signaled == DAT_FALSE && pthread_status == 0) |
| 362 |
{ |
| 363 |
pthread_status = pthread_cond_timedwait ( |
| 364 |
&wait_obj->cv, &wait_obj->lock, &future ); |
| 365 |
|
| 366 |
/* |
| 367 |
* No need to reset &future if we go around the loop; |
| 368 |
* It's an absolute time. |
| 369 |
*/ |
| 370 |
} |
| 371 |
/* Reset the signaled status if we were woken up. */ |
| 372 |
if (pthread_status == 0) |
| 373 |
{ |
| 374 |
wait_obj->signaled = false; |
| 375 |
} |
| 376 |
pthread_mutex_unlock (&wait_obj->lock); |
| 377 |
} |
| 378 |
else |
| 379 |
{ |
| 380 |
pthread_mutex_lock (&wait_obj->lock); |
| 381 |
while ( wait_obj->signaled == DAT_FALSE && pthread_status == 0) |
| 382 |
{ |
| 383 |
pthread_status = pthread_cond_wait ( |
| 384 |
&wait_obj->cv, &wait_obj->lock ); |
| 385 |
} |
| 386 |
/* Reset the signaled status if we were woken up. */ |
| 387 |
if (pthread_status == 0) |
| 388 |
{ |
| 389 |
wait_obj->signaled = false; |
| 390 |
} |
| 391 |
pthread_mutex_unlock (&wait_obj->lock); |
| 392 |
} |
| 393 |
|
| 394 |
if (ETIMEDOUT == pthread_status) |
| 395 |
{ |
| 396 |
return (-1); |
| 397 |
} |
| 398 |
else if ( 0 != pthread_status) |
| 399 |
{ |
| 400 |
return (-1); |
| 401 |
} |
| 402 |
|
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
/* |
| 408 |
* DT_Mdep_wait_object_wakeup |
| 409 |
* |
| 410 |
* Wakeup a thread waiting on 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_wakeup ( |
| 421 |
DT_WAIT_OBJECT *wait_obj ) |
| 422 |
{ |
| 423 |
pthread_mutex_lock ( &wait_obj->lock ); |
| 424 |
wait_obj->signaled = true; |
| 425 |
pthread_mutex_unlock ( &wait_obj->lock ); |
| 426 |
if ( 0 != pthread_cond_signal ( &wait_obj->cv ) ) |
| 427 |
{ |
| 428 |
return (-1); |
| 429 |
} |
| 430 |
|
| 431 |
return 0; |
| 432 |
|
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
/* |
| 437 |
* DT_Mdep_wait_object_destroy |
| 438 |
* |
| 439 |
* Destroy a wait object |
| 440 |
* |
| 441 |
* Input: |
| 442 |
* wait_obj |
| 443 |
* |
| 444 |
* Returns: |
| 445 |
* 0 if successful |
| 446 |
* -1 if not successful |
| 447 |
*/ |
| 448 |
int |
| 449 |
DT_Mdep_wait_object_destroy ( |
| 450 |
IN DT_WAIT_OBJECT *wait_obj) |
| 451 |
{ |
| 452 |
|
| 453 |
if ( 0 != pthread_cond_destroy ( &wait_obj->cv ) ) |
| 454 |
{ |
| 455 |
return (-1); |
| 456 |
} |
| 457 |
if ( 0 != pthread_mutex_destroy ( &wait_obj->lock ) ) |
| 458 |
{ |
| 459 |
return (-1); |
| 460 |
} |
| 461 |
|
| 462 |
return 0; |
| 463 |
|
| 464 |
|
| 465 |
} |
| 466 |
|