Bug #28284: Test "mysqlslap" reports "out of memory"

When locking a "fast" mutex a static variable cpu_count 
was used as a flag to initialize itself on the first usage 
by calling sysconf() and setting non-zero value.
This is not thread and optimization safe on some 
platforms. That's why the global initialization needs 
to be done once in a designated function.
This will also speed up the usage (by a small bit) 
because it won't have to check if it's initialized on
every call.

Fixed by moving the fast mutexes initialization out of 
my_pthread_fastmutex_lock() to fastmutex_global_init()
and call it from my_init()
parent c4811d67
...@@ -538,6 +538,7 @@ typedef struct st_my_pthread_fastmutex_t ...@@ -538,6 +538,7 @@ typedef struct st_my_pthread_fastmutex_t
pthread_mutex_t mutex; pthread_mutex_t mutex;
uint spins; uint spins;
} my_pthread_fastmutex_t; } my_pthread_fastmutex_t;
void fastmutex_global_init(void);
int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp, int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
const pthread_mutexattr_t *attr); const pthread_mutexattr_t *attr);
......
...@@ -78,6 +78,9 @@ my_bool my_init(void) ...@@ -78,6 +78,9 @@ my_bool my_init(void)
my_umask_dir= 0700; /* Default umask for new directories */ my_umask_dir= 0700; /* Default umask for new directories */
#if defined(THREAD) && defined(SAFE_MUTEX) #if defined(THREAD) && defined(SAFE_MUTEX)
safe_mutex_global_init(); /* Must be called early */ safe_mutex_global_init(); /* Must be called early */
#endif
#if defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
fastmutex_global_init(); /* Must be called early */
#endif #endif
netware_init(); netware_init();
#ifdef THREAD #ifdef THREAD
......
...@@ -394,15 +394,11 @@ ulong mutex_delay(ulong delayloops) ...@@ -394,15 +394,11 @@ ulong mutex_delay(ulong delayloops)
#define MY_PTHREAD_FASTMUTEX_SPINS 8 #define MY_PTHREAD_FASTMUTEX_SPINS 8
#define MY_PTHREAD_FASTMUTEX_DELAY 4 #define MY_PTHREAD_FASTMUTEX_DELAY 4
static int cpu_count= 0;
int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp, int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
const pthread_mutexattr_t *attr) const pthread_mutexattr_t *attr)
{ {
static int cpu_count= 0;
#ifdef _SC_NPROCESSORS_CONF
if (!cpu_count && (attr == MY_MUTEX_INIT_FAST))
cpu_count= sysconf(_SC_NPROCESSORS_CONF);
#endif
if ((cpu_count > 1) && (attr == MY_MUTEX_INIT_FAST)) if ((cpu_count > 1) && (attr == MY_MUTEX_INIT_FAST))
mp->spins= MY_PTHREAD_FASTMUTEX_SPINS; mp->spins= MY_PTHREAD_FASTMUTEX_SPINS;
else else
...@@ -432,4 +428,13 @@ int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp) ...@@ -432,4 +428,13 @@ int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp)
} }
return pthread_mutex_lock(&mp->mutex); return pthread_mutex_lock(&mp->mutex);
} }
void fastmutex_global_init(void)
{
#ifdef _SC_NPROCESSORS_CONF
cpu_count= sysconf(_SC_NPROCESSORS_CONF);
#endif
}
#endif /* defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */ #endif /* defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment