~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/thr_mutex.c

  • Committer: Monty Taylor
  • Date: 2008-11-07 00:15:51 UTC
  • mto: This revision was merged to the branch mainline in revision 579.
  • Revision ID: monty@inaugust.com-20081107001551-8vxb6sf1ti0i5p09
Cleaned up some headers for PCH.

Show diffs side-by-side

added added

removed removed

Lines of Context:
381
381
 
382
382
#endif /* THREAD && SAFE_MUTEX */
383
383
 
384
 
#if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
385
 
 
386
 
#include "mysys_priv.h"
387
 
#include "my_static.h"
388
 
#include <m_string.h>
389
 
 
390
 
#include <m_ctype.h>
391
 
#include <myisampack.h>
392
 
#include <mysys_err.h>
393
 
#include <my_sys.h>
394
 
 
395
 
#undef pthread_mutex_t
396
 
#undef pthread_mutex_init
397
 
#undef pthread_mutex_lock
398
 
#undef pthread_mutex_trylock
399
 
#undef pthread_mutex_unlock
400
 
#undef pthread_mutex_destroy
401
 
#undef pthread_cond_wait
402
 
#undef pthread_cond_timedwait
403
 
 
404
 
uint32_t mutex_delay(uint32_t delayloops)
405
 
{
406
 
  uint32_t      i;
407
 
  volatile uint32_t j;
408
 
 
409
 
  j = 0;
410
 
 
411
 
  for (i = 0; i < delayloops * 50; i++)
412
 
    j += i;
413
 
 
414
 
  return(j); 
415
 
}       
416
 
 
417
 
#define MY_PTHREAD_FASTMUTEX_SPINS 8
418
 
#define MY_PTHREAD_FASTMUTEX_DELAY 4
419
 
 
420
 
static int cpu_count= 0;
421
 
 
422
 
int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
423
 
                              const pthread_mutexattr_t *attr)
424
 
{
425
 
  if ((cpu_count > 1) && (attr == MY_MUTEX_INIT_FAST))
426
 
    mp->spins= MY_PTHREAD_FASTMUTEX_SPINS; 
427
 
  else
428
 
    mp->spins= 0;
429
 
  return pthread_mutex_init(&mp->mutex, attr); 
430
 
}
431
 
 
432
 
int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp)
433
 
{
434
 
  int   res;
435
 
  uint32_t  i;
436
 
  uint32_t  maxdelay= MY_PTHREAD_FASTMUTEX_DELAY;
437
 
 
438
 
  for (i= 0; i < mp->spins; i++)
439
 
  {
440
 
    res= pthread_mutex_trylock(&mp->mutex);
441
 
 
442
 
    if (res == 0)
443
 
      return 0;
444
 
 
445
 
    if (res != EBUSY)
446
 
      return res;
447
 
 
448
 
    mutex_delay(maxdelay);
449
 
    maxdelay += ((double) random() / (double) RAND_MAX) * 
450
 
                MY_PTHREAD_FASTMUTEX_DELAY + 1;
451
 
  }
452
 
  return pthread_mutex_lock(&mp->mutex);
453
 
}
454
 
 
455
 
 
456
 
void fastmutex_global_init(void)
457
 
{
458
 
#ifdef _SC_NPROCESSORS_CONF
459
 
  cpu_count= sysconf(_SC_NPROCESSORS_CONF);
460
 
#endif
461
 
}
462
 
  
463
 
#endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */