diff options
Diffstat (limited to 'mysys')
-rw-r--r-- | mysys/Makefile.am | 1 | ||||
-rw-r--r-- | mysys/base64.c | 2 | ||||
-rw-r--r-- | mysys/my_vle.c | 113 | ||||
-rw-r--r-- | mysys/thr_mutex.c | 77 |
4 files changed, 192 insertions, 1 deletions
diff --git a/mysys/Makefile.am b/mysys/Makefile.am index a54bc550228..4d6c0d1e189 100644 --- a/mysys/Makefile.am +++ b/mysys/Makefile.am @@ -35,6 +35,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c my_mmap.c \ mf_tempdir.c my_lock.c mf_brkhant.c my_alarm.c \ my_malloc.c my_realloc.c my_once.c mulalloc.c \ my_alloc.c safemalloc.c my_new.cc \ + my_vle.c \ my_fopen.c my_fstream.c my_getsystime.c \ my_error.c errors.c my_div.c my_messnc.c \ mf_format.c mf_same.c mf_dirname.c mf_fn_ext.c \ diff --git a/mysys/base64.c b/mysys/base64.c index b29c8ff8360..60218993c42 100644 --- a/mysys/base64.c +++ b/mysys/base64.c @@ -16,6 +16,7 @@ #include <base64.h> #include <m_string.h> /* strchr() */ +#include <m_ctype.h> /* my_isspace() */ #ifndef MAIN @@ -115,7 +116,6 @@ pos(unsigned char c) } \ if (i == size) \ { \ - i= size + 1; \ break; \ } \ } diff --git a/mysys/my_vle.c b/mysys/my_vle.c new file mode 100644 index 00000000000..3ddc1e4c6e0 --- /dev/null +++ b/mysys/my_vle.c @@ -0,0 +1,113 @@ +/* + Copyright (C) 2005 MySQL AB + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA +*/ + +/* + Variable length encoding. + + A method to store an arbitrary-size non-negative integer. We let the + most significant bit of the number indicate that the next byte + should be contatenated to form the real number. +*/ + +#include "my_vle.h" + +/* + Function to encode an unsigned long as VLE. The bytes for the VLE + will be written to the location pointed to by 'out'. The maximum + number of bytes written will be 'max'. + + PARAMETERS + + out Pointer to beginning of where to store VLE bytes. + max Maximum number of bytes to write. + n Number to encode. + + RETURN VALUE + On success, one past the end of the array containing the VLE + bytes. On failure, the 'out' pointer is returned. +*/ + +byte* +my_vle_encode(byte* out, my_size_t max, ulong n) +{ + byte buf[my_vle_sizeof(n)]; + byte *ptr= buf; + my_size_t len; + + do + { + *ptr++= (n & 0x7F); + n>>= 7; + } + while (n > 0); + + len= ptr - buf; + + if (len <= max) + { + /* + The bytes are stored in reverse order in 'buf'. Let's write them + in correct order to the output buffer and set the MSB at the + same time. + */ + while (ptr-- > buf) + { + byte v= *ptr; + if (ptr > buf) + v|= 0x80; + *out++= v; + } + } + + return out; +} + +/* + Function to decode a VLE representation of an integral value. + + + PARAMETERS + + result_ptr Pointer to an unsigned long where the value will be written. + vle Pointer to the VLE bytes. + + RETURN VALUE + + One-past the end of the VLE bytes. The routine will never read + more than sizeof(*result_ptr) + 1 bytes. +*/ + +byte const* +my_vle_decode(ulong *result_ptr, byte const *vle) +{ + ulong result= 0; + my_size_t cnt= 1; + + do + { + result<<= 7; + result|= (*vle & 0x7F); + } + while ((*vle++ & 0x80) && ++cnt <= sizeof(*result_ptr) + 1); + + if (cnt <= sizeof(*result_ptr) + 1) + *result_ptr= result; + + return vle; +} diff --git a/mysys/thr_mutex.c b/mysys/thr_mutex.c index 3326068d164..3dde1fd5bfa 100644 --- a/mysys/thr_mutex.c +++ b/mysys/thr_mutex.c @@ -356,3 +356,80 @@ void safe_mutex_end(FILE *file __attribute__((unused))) } #endif /* THREAD && SAFE_MUTEX */ + +#if defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) + +#include "mysys_priv.h" +#include "my_static.h" +#include <m_string.h> + +#include <m_ctype.h> +#include <hash.h> +#include <myisampack.h> +#include <mysys_err.h> +#include <my_sys.h> + +#undef pthread_mutex_t +#undef pthread_mutex_init +#undef pthread_mutex_lock +#undef pthread_mutex_trylock +#undef pthread_mutex_unlock +#undef pthread_mutex_destroy +#undef pthread_cond_wait +#undef pthread_cond_timedwait + +ulong mutex_delay(ulong delayloops) +{ + ulong i; + volatile ulong j; + + j = 0; + + for (i = 0; i < delayloops * 50; i++) + j += i; + + return(j); +} + +#define MY_PTHREAD_FASTMUTEX_SPINS 8 +#define MY_PTHREAD_FASTMUTEX_DELAY 4 + +int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp, + 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)) + mp->spins= MY_PTHREAD_FASTMUTEX_SPINS; + else + mp->spins= 0; + return pthread_mutex_init(&mp->mutex, attr); +} + +int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp) +{ + int res; + uint i; + uint maxdelay= MY_PTHREAD_FASTMUTEX_DELAY; + + for (i= 0; i < mp->spins; i++) + { + res= pthread_mutex_trylock(&mp->mutex); + + if (res == 0) + return 0; + + if (res != EBUSY) + return res; + + mutex_delay(maxdelay); + maxdelay += ((double) random() / (double) RAND_MAX) * + MY_PTHREAD_FASTMUTEX_DELAY + 1; + } + return pthread_mutex_lock(&mp->mutex); +} +#endif /* defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */ |