diff options
Diffstat (limited to 'mysys/mf_iocache.c')
-rw-r--r-- | mysys/mf_iocache.c | 82 |
1 files changed, 54 insertions, 28 deletions
diff --git a/mysys/mf_iocache.c b/mysys/mf_iocache.c index 1a761fa5ed8..6e0a267165b 100644 --- a/mysys/mf_iocache.c +++ b/mysys/mf_iocache.c @@ -424,32 +424,62 @@ int _my_b_read(register IO_CACHE *info, byte *Buffer, uint Count) DBUG_RETURN(0); } + #ifdef THREAD -int init_io_cache_share(IO_CACHE *info, IO_CACHE_SHARE *s, uint num_threads) + +/* Initialzie multi-thread usage of the IO cache */ + +void init_io_cache_share(IO_CACHE *info, IO_CACHE_SHARE *s, uint num_threads) { DBUG_ASSERT(info->type == READ_CACHE); - pthread_mutex_init(& s->mutex, MY_MUTEX_INIT_FAST); - pthread_cond_init (& s->cond, 0); + pthread_mutex_init(&s->mutex, MY_MUTEX_INIT_FAST); + pthread_cond_init(&s->cond, 0); s->count=num_threads; s->active=0; /* to catch errors */ info->share=s; info->read_function=_my_b_read_r; } -int remove_io_thread(IO_CACHE *info) +void remove_io_thread(IO_CACHE *info) { - if (errno=pthread_mutex_lock(& info->share->mutex)) - return -1; + pthread_mutex_lock(&info->share->mutex); if (! info->share->count--) - pthread_cond_signal(& info->share->cond); - pthread_mutex_unlock(& info->share->mutex); + pthread_cond_signal(&info->share->cond); + pthread_mutex_unlock(&info->share->mutex); +} + + +int lock_io_cache(IO_CACHE *info) +{ + pthread_mutex_lock(&info->share->mutex); + if (!info->share->count) + return 1; + info->share->count--; + pthread_cond_wait(&((info)->share->cond), &((info)->share->mutex)); + if (!++info->share->count) + return 1; + pthread_mutex_unlock(&info->share->mutex); return 0; } +void unlock_io_cache(IO_CACHE *info) +{ + pthread_cond_broadcast(&info->share->cond); + pthread_mutex_unlock(&info->share->mutex); +} + +/* + Read from the io cache in a thread safe manner +*/ + +#define IO_ROUND_UP(X) (((X)+IO_SIZE-1) & ~(IO_SIZE-1)) +#define IO_ROUND_DN(X) ( (X) & ~(IO_SIZE-1)) + + int _my_b_read_r(register IO_CACHE *info, byte *Buffer, uint Count) { my_off_t pos_in_file; - int length,diff_length,read_len; + uint length,diff_length,read_len; DBUG_ENTER("_my_b_read_r"); if ((read_len=(uint) (info->read_end-info->read_pos))) @@ -459,26 +489,23 @@ int _my_b_read_r(register IO_CACHE *info, byte *Buffer, uint Count) Buffer+=read_len; Count-=read_len; } - -#define IO_ROUND_UP(X) (((X)+IO_SIZE-1) & ~(IO_SIZE-1)) -#define IO_ROUND_DN(X) ( (X) & ~(IO_SIZE-1)) - - while (Count) { - int cnt, len; + while (Count) + { + uint cnt, len; pos_in_file= info->pos_in_file + (uint)(info->read_end - info->buffer); - diff_length= pos_in_file & (IO_SIZE-1); + diff_length= (uint) (pos_in_file & (IO_SIZE-1)); length=IO_ROUND_UP(Count+diff_length)-diff_length; - length=(length <= info->read_length) ? - length + IO_ROUND_DN(info->read_length - length) : - length - IO_ROUND_UP(length - info->read_length) ; + length= ((length <= info->read_length) ? + length + IO_ROUND_DN(info->read_length - length) : + length - IO_ROUND_UP(length - info->read_length)) ; if (lock_io_cache(info)) { info->share->active=info; if (info->seek_not_done) /* File touched, do seek */ VOID(my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0))); len=my_read(info->file,info->buffer, length, info->myflags); - info->read_end=info->buffer + (len == -1 ? 0 : len); + info->read_end=info->buffer + (len == (uint) -1 ? 0 : len); info->error=(len == length ? 0 : len); info->pos_in_file=pos_in_file; unlock_io_cache(info); @@ -494,7 +521,7 @@ int _my_b_read_r(register IO_CACHE *info, byte *Buffer, uint Count) info->seek_not_done=0; if (info->error) { - info->error=read_len; + info->error=(int) read_len; DBUG_RETURN(1); } cnt=(len > Count) ? Count : len; @@ -508,6 +535,7 @@ int _my_b_read_r(register IO_CACHE *info, byte *Buffer, uint Count) } #endif + /* Do sequential read from the SEQ_READ_APPEND cache we do this in three stages: @@ -1072,13 +1100,11 @@ int end_io_cache(IO_CACHE *info) #ifdef THREAD /* simple protection against multi-close: destroying share first */ if (info->share) - if (pthread_cond_destroy (& info->share->cond) | - pthread_mutex_destroy(& info->share->mutex)) - { - DBUG_RETURN(1); - } - else - info->share=0; + { + pthread_cond_destroy(&info->share->cond); + pthread_mutex_destroy(&info->share->mutex); + info->share=0; + } #endif if ((pre_close=info->pre_close)) |