~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_sys.h

Merged iocache.h addition.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2003 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 MySQL
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
15
20
 
16
21
#ifndef _my_sys_h
17
22
#define _my_sys_h
18
23
 
19
 
#ifdef HAVE_AIOWAIT
20
 
#include <sys/asynch.h>                 /* Used by record-cache */
21
 
typedef struct my_aio_result {
22
 
  aio_result_t result;
23
 
  int          pending;
24
 
} my_aio_result;
25
 
#endif
26
 
 
27
24
#include <errno.h>
28
25
#define my_errno (errno)
29
26
 
32
29
#include <mystrings/m_ctype.h>                    /* for CHARSET_INFO */
33
30
#include <stdarg.h>
34
31
#include <mysys/typelib.h>
 
32
#include <mysys/aio_result.h>
35
33
 
36
34
#define MY_INIT(name);          { my_progname= name; my_init(); }
37
35
 
279
277
  pthread_mutex_t mutex;
280
278
} MY_TMPDIR;
281
279
 
282
 
struct st_io_cache;
283
 
typedef int (*IO_CACHE_CALLBACK)(struct st_io_cache*);
284
 
 
285
 
typedef struct st_io_cache_share
286
 
{
287
 
  pthread_mutex_t       mutex;           /* To sync on reads into buffer. */
288
 
  pthread_cond_t        cond;            /* To wait for signals. */
289
 
  pthread_cond_t        cond_writer;     /* For a synchronized writer. */
290
 
  /* Offset in file corresponding to the first byte of buffer. */
291
 
  my_off_t              pos_in_file;
292
 
  /* If a synchronized write cache is the source of the data. */
293
 
  struct st_io_cache    *source_cache;
294
 
  unsigned char                 *buffer;         /* The read buffer. */
295
 
  unsigned char                 *read_end;       /* Behind last valid byte of buffer. */
296
 
  int                   running_threads; /* threads not in lock. */
297
 
  int                   total_threads;   /* threads sharing the cache. */
298
 
  int                   error;           /* Last error. */
299
 
#ifdef NOT_YET_IMPLEMENTED
300
 
  /* whether the structure should be free'd */
301
 
  bool alloced;
302
 
#endif
303
 
} IO_CACHE_SHARE;
304
 
 
305
 
typedef struct st_io_cache              /* Used when cacheing files */
306
 
{
307
 
  /* Offset in file corresponding to the first byte of unsigned char* buffer. */
308
 
  my_off_t pos_in_file;
309
 
  /*
310
 
    The offset of end of file for READ_CACHE and WRITE_CACHE.
311
 
    For SEQ_READ_APPEND it the maximum of the actual end of file and
312
 
    the position represented by read_end.
313
 
  */
314
 
  my_off_t end_of_file;
315
 
  /* Points to current read position in the buffer */
316
 
  unsigned char *read_pos;
317
 
  /* the non-inclusive boundary in the buffer for the currently valid read */
318
 
  unsigned char  *read_end;
319
 
  unsigned char  *buffer;                               /* The read buffer */
320
 
  /* Used in ASYNC_IO */
321
 
  unsigned char  *request_pos;
322
 
 
323
 
  /* Only used in WRITE caches and in SEQ_READ_APPEND to buffer writes */
324
 
  unsigned char  *write_buffer;
325
 
  /*
326
 
    Only used in SEQ_READ_APPEND, and points to the current read position
327
 
    in the write buffer. Note that reads in SEQ_READ_APPEND caches can
328
 
    happen from both read buffer (unsigned char* buffer) and write buffer
329
 
    (unsigned char* write_buffer).
330
 
  */
331
 
  unsigned char *append_read_pos;
332
 
  /* Points to current write position in the write buffer */
333
 
  unsigned char *write_pos;
334
 
  /* The non-inclusive boundary of the valid write area */
335
 
  unsigned char *write_end;
336
 
 
337
 
  /*
338
 
    Current_pos and current_end are convenience variables used by
339
 
    my_b_tell() and other routines that need to know the current offset
340
 
    current_pos points to &write_pos, and current_end to &write_end in a
341
 
    WRITE_CACHE, and &read_pos and &read_end respectively otherwise
342
 
  */
343
 
  unsigned char  **current_pos, **current_end;
344
 
  /*
345
 
    The lock is for append buffer used in SEQ_READ_APPEND cache
346
 
    need mutex copying from append buffer to read buffer.
347
 
  */
348
 
  pthread_mutex_t append_buffer_lock;
349
 
  /*
350
 
    The following is used when several threads are reading the
351
 
    same file in parallel. They are synchronized on disk
352
 
    accesses reading the cached part of the file asynchronously.
353
 
    It should be set to NULL to disable the feature.  Only
354
 
    READ_CACHE mode is supported.
355
 
  */
356
 
  IO_CACHE_SHARE *share;
357
 
  /*
358
 
    A caller will use my_b_read() macro to read from the cache
359
 
    if the data is already in cache, it will be simply copied with
360
 
    memcpy() and internal variables will be accordinging updated with
361
 
    no functions invoked. However, if the data is not fully in the cache,
362
 
    my_b_read() will call read_function to fetch the data. read_function
363
 
    must never be invoked directly.
364
 
  */
365
 
  int (*read_function)(struct st_io_cache *,unsigned char *,size_t);
366
 
  /*
367
 
    Same idea as in the case of read_function, except my_b_write() needs to
368
 
    be replaced with my_b_append() for a SEQ_READ_APPEND cache
369
 
  */
370
 
  int (*write_function)(struct st_io_cache *,const unsigned char *,size_t);
371
 
  /*
372
 
    Specifies the type of the cache. Depending on the type of the cache
373
 
    certain operations might not be available and yield unpredicatable
374
 
    results. Details to be documented later
375
 
  */
376
 
  enum cache_type type;
377
 
  /*
378
 
    Callbacks when the actual read I/O happens. These were added and
379
 
    are currently used for binary logging of LOAD DATA INFILE - when a
380
 
    block is read from the file, we create a block create/append event, and
381
 
    when IO_CACHE is closed, we create an end event. These functions could,
382
 
    of course be used for other things
383
 
  */
384
 
  IO_CACHE_CALLBACK pre_read;
385
 
  IO_CACHE_CALLBACK post_read;
386
 
  IO_CACHE_CALLBACK pre_close;
387
 
  /*
388
 
    Counts the number of times, when we were forced to use disk. We use it to
389
 
    increase the binlog_cache_disk_use status variable.
390
 
  */
391
 
  uint32_t disk_writes;
392
 
  void* arg;                            /* for use by pre/post_read */
393
 
  char *file_name;                      /* if used with 'open_cached_file' */
394
 
  char *dir,*prefix;
395
 
  File file; /* file descriptor */
396
 
  /*
397
 
    seek_not_done is set by my_b_seek() to inform the upcoming read/write
398
 
    operation that a seek needs to be preformed prior to the actual I/O
399
 
    error is 0 if the cache operation was successful, -1 if there was a
400
 
    "hard" error, and the actual number of I/O-ed bytes if the read/write was
401
 
    partial.
402
 
  */
403
 
  int   seek_not_done,error;
404
 
  /* buffer_length is memory size allocated for buffer or write_buffer */
405
 
  size_t        buffer_length;
406
 
  /* read_length is the same as buffer_length except when we use async io */
407
 
  size_t  read_length;
408
 
  myf   myflags;                        /* Flags used to my_read/my_write */
409
 
  /*
410
 
    alloced_buffer is 1 if the buffer was allocated by init_io_cache() and
411
 
    0 if it was supplied by the user.
412
 
    Currently READ_NET is the only one that will use a buffer allocated
413
 
    somewhere else
414
 
  */
415
 
  bool alloced_buffer;
416
 
#ifdef HAVE_AIOWAIT
417
 
  /*
418
 
    As inidicated by ifdef, this is for async I/O, which is not currently
419
 
    used (because it's not reliable on all systems)
420
 
  */
421
 
  uint32_t inited;
422
 
  my_off_t aio_read_pos;
423
 
  my_aio_result aio_result;
424
 
#endif
425
 
} IO_CACHE;
426
280
 
427
281
typedef int (*qsort2_cmp)(const void *, const void *, const void *);
428
282
 
467
321
  (char*) my_b_get_buffer_start(info)
468
322
#define my_b_get_pos_in_file(info) (info)->pos_in_file
469
323
 
470
 
/* tell write offset in the SEQ_APPEND cache */
471
 
int      my_b_copy_to_file(IO_CACHE *cache, FILE *file);
472
 
my_off_t my_b_append_tell(IO_CACHE* info);
473
 
my_off_t my_b_safe_tell(IO_CACHE* info); /* picks the correct tell() */
474
324
 
475
325
#define my_b_bytes_in_cache(info) (size_t) (*(info)->current_end - \
476
326
                                          *(info)->current_pos)
631
481
extern qsort2_cmp get_ptr_compare(size_t);
632
482
void my_store_ptr(unsigned char *buff, size_t pack_length, my_off_t pos);
633
483
my_off_t my_get_ptr(unsigned char *ptr, size_t pack_length);
634
 
extern int init_io_cache(IO_CACHE *info,File file,size_t cachesize,
635
 
                         enum cache_type type,my_off_t seek_offset,
636
 
                         bool use_async_io, myf cache_myflags);
637
 
extern bool reinit_io_cache(IO_CACHE *info,enum cache_type type,
638
 
                               my_off_t seek_offset,bool use_async_io,
639
 
                               bool clear_cache);
640
 
extern void setup_io_cache(IO_CACHE* info);
641
 
extern int _my_b_read(IO_CACHE *info,unsigned char *Buffer,size_t Count);
642
 
extern int _my_b_read_r(IO_CACHE *info,unsigned char *Buffer,size_t Count);
643
 
extern void init_io_cache_share(IO_CACHE *read_cache, IO_CACHE_SHARE *cshare,
644
 
                                IO_CACHE *write_cache, uint32_t num_threads);
645
 
extern void remove_io_thread(IO_CACHE *info);
646
 
extern int _my_b_seq_read(IO_CACHE *info,unsigned char *Buffer,size_t Count);
647
 
extern int _my_b_net_read(IO_CACHE *info,unsigned char *Buffer,size_t Count);
648
 
extern int _my_b_get(IO_CACHE *info);
649
 
extern int _my_b_async_read(IO_CACHE *info,unsigned char *Buffer,size_t Count);
650
 
extern int _my_b_write(IO_CACHE *info,const unsigned char *Buffer,size_t Count);
651
 
extern int my_b_append(IO_CACHE *info,const unsigned char *Buffer,size_t Count);
652
 
extern int my_b_safe_write(IO_CACHE *info,const unsigned char *Buffer,size_t Count);
653
 
 
654
 
extern int my_block_write(IO_CACHE *info, const unsigned char *Buffer,
655
 
                          size_t Count, my_off_t pos);
656
 
extern int my_b_flush_io_cache(IO_CACHE *info, int need_append_buffer_lock);
657
 
 
658
 
#define flush_io_cache(info) my_b_flush_io_cache((info),1)
659
 
 
660
 
extern int end_io_cache(IO_CACHE *info);
661
 
extern size_t my_b_fill(IO_CACHE *info);
662
 
extern void my_b_seek(IO_CACHE *info,my_off_t pos);
663
 
extern size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length);
664
 
extern my_off_t my_b_filelength(IO_CACHE *info);
665
 
extern size_t my_b_printf(IO_CACHE *info, const char* fmt, ...);
666
 
extern size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list ap);
667
 
extern bool open_cached_file(IO_CACHE *cache,const char *dir,
668
 
                                 const char *prefix, size_t cache_size,
669
 
                                 myf cache_myflags);
670
 
extern bool real_open_cached_file(IO_CACHE *cache);
671
 
extern void close_cached_file(IO_CACHE *cache);
672
484
File create_temp_file(char *to, const char *dir, const char *pfx,
673
485
                      int mode, myf MyFlags);
674
486
#define my_init_dynamic_array(A,B,C,D) init_dynamic_array2(A,B,NULL,C,D CALLER_INFO)