#ifndef _ASYNCIO_H
#define _ASYNCIO_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <thread.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <siginfo.h>
#include <aio.h>
#include <limits.h>
#include <ucontext.h>
#include <sys/asynch.h>
#include <sys/mman.h>
#if !defined(_LP64)
#define AIOSTKSIZE (64 * 1024)
#else
#define AIOSTKSIZE (128 * 1024)
#endif
#define SIGAIOCANCEL SIGLWP
#define AIO_WAITN_MAXIOCBS 32768
typedef struct aio_args aio_args_t;
typedef struct aio_lio aio_lio_t;
typedef struct notif_param notif_param_t;
typedef struct aio_req aio_req_t;
typedef struct aio_worker aio_worker_t;
typedef struct aio_hash aio_hash_t;
struct aio_args {
int fd;
caddr_t buf;
size_t bufsz;
offset_t offset;
};
struct aio_lio {
mutex_t lio_mutex;
cond_t lio_cond_cv;
aio_lio_t *lio_next;
char lio_mode;
char lio_canned;
char lio_largefile;
char lio_waiting;
int lio_nent;
int lio_refcnt;
int lio_event;
int lio_port;
int lio_signo;
union sigval lio_sigval;
uintptr_t lio_object;
struct sigevent *lio_sigevent;
};
struct notif_param {
int np_signo;
int np_port;
void *np_user;
int np_event;
uintptr_t np_object;
int np_lio_signo;
int np_lio_port;
void *np_lio_user;
int np_lio_event;
uintptr_t np_lio_object;
};
struct aio_req {
aio_req_t *req_link;
aio_req_t *req_next;
aio_req_t *req_prev;
char req_state;
char req_type;
char req_largefile;
char req_op;
aio_worker_t *req_worker;
aio_result_t *req_resultp;
aio_args_t req_args;
aio_lio_t *req_head;
struct sigevent req_sigevent;
void *req_aiocbp;
notif_param_t req_notify;
};
#define LIO_FSYNC LIO_WAIT+1
#define LIO_DESTROY LIO_FSYNC+1
#define LIO_FSYNC_CANCELED 0x1
#define AIO_REQ_QUEUED 1
#define AIO_REQ_INPROGRESS 2
#define AIO_REQ_CANCELED 3
#define AIO_REQ_DONE 4
#define AIO_REQ_FREE 5
#define AIO_REQ_DONEQ 6
#define AIO_NO_KAIO 0x0
#define AIO_KAIO 0x1
#define AIO_NO_DUPS 0x2
#define AIO_POSIX_REQ 0x1
#define CHECK 1
#define NOCHECK 2
#define CHECKED 3
#define USERAIO 4
#define USERAIO_DONE 5
#define AIO_WAIT_INPROGRESS 0x1
#define AIO_IO_WAITING 0x2
#define AIO_LIB_WAITN 0x4
#define AIO_LIB_WAITN_PENDING 0x8
#define MAX_KAIO_FDARRAY_SIZE 2048
#define KAIO_FDARRAY_ELEM_SIZE WORD_BIT
#define MAX_KAIO_FDS (MAX_KAIO_FDARRAY_SIZE * KAIO_FDARRAY_ELEM_SIZE)
#define VALID_FD(fdes) ((fdes) >= 0 && (fdes) < MAX_KAIO_FDS)
#define KAIO_SUPPORTED(fdes) \
(!VALID_FD(fdes) || \
((_kaio_supported[(fdes) / KAIO_FDARRAY_ELEM_SIZE] & \
(uint32_t)(1 << ((fdes) % KAIO_FDARRAY_ELEM_SIZE))) == 0))
#define SET_KAIO_NOT_SUPPORTED(fdes) \
if (VALID_FD(fdes)) \
_kaio_supported[(fdes) / KAIO_FDARRAY_ELEM_SIZE] |= \
(uint32_t)(1 << ((fdes) % KAIO_FDARRAY_ELEM_SIZE))
#define CLEAR_KAIO_SUPPORTED(fdes) \
if (VALID_FD(fdes)) \
_kaio_supported[(fdes) / KAIO_FDARRAY_ELEM_SIZE] &= \
~(uint32_t)(1 << ((fdes) % KAIO_FDARRAY_ELEM_SIZE))
struct aio_worker {
aio_worker_t *work_forw;
aio_worker_t *work_backw;
mutex_t work_qlock1;
cond_t work_idle_cv;
aio_req_t *work_head1;
aio_req_t *work_tail1;
aio_req_t *work_next1;
aio_req_t *work_prev1;
aio_req_t *work_req;
thread_t work_tid;
int work_count1;
int work_done1;
int work_minload1;
int work_idleflg;
sigjmp_buf work_jmp_buf;
};
struct aio_hash {
mutex_t hash_lock;
aio_req_t *hash_ptr;
#if !defined(_LP64)
void *hash_pad;
#endif
};
extern aio_hash_t *_aio_hash;
#define HASHSZ 2048
#define AIOHASH(resultp) ((((uintptr_t)(resultp) >> 17) ^ \
((uintptr_t)(resultp) >> 2)) & (HASHSZ - 1))
#define POSIX_AIO(x) ((x)->req_type == AIO_POSIX_REQ)
extern int __uaio_init(void);
extern void _kaio_init(void);
extern intptr_t _kaio(int, ...);
extern int _aiorw(int, caddr_t, int, offset_t, int, aio_result_t *, int);
extern int _aio_rw(aiocb_t *, aio_lio_t *, aio_worker_t **, int, int);
#if !defined(_LP64)
extern int _aio_rw64(aiocb64_t *, aio_lio_t *, aio_worker_t **, int, int);
#endif
extern int _aio_create_worker(aio_req_t *, int);
extern int _aio_cancel_req(aio_worker_t *, aio_req_t *, int *, int *);
extern int aiocancel_all(int);
extern void aio_panic(const char *) __NORETURN;
extern aio_req_t *_aio_hash_find(aio_result_t *);
extern aio_req_t *_aio_hash_del(aio_result_t *);
extern void _aio_req_mark_done(aio_req_t *);
extern void _aio_waitn_wakeup(void);
extern aio_worker_t *_aio_worker_alloc(void);
extern void _aio_worker_free(void *);
extern aio_req_t *_aio_req_alloc(void);
extern void _aio_req_free(aio_req_t *);
extern aio_lio_t *_aio_lio_alloc(void);
extern void _aio_lio_free(aio_lio_t *);
extern int _aio_idle(aio_worker_t *);
extern void *_aio_do_request(void *);
extern void *_aio_do_notify(void *);
extern void _lio_remove(aio_req_t *);
extern aio_req_t *_aio_req_remove(aio_req_t *);
extern int _aio_get_timedelta(timespec_t *, timespec_t *);
extern aio_result_t *_aio_req_done(void);
extern void _aio_set_result(aio_req_t *, ssize_t, int);
extern int _aio_sigev_thread_init(struct sigevent *);
extern int _aio_sigev_thread(aiocb_t *);
#if !defined(_LP64)
extern int _aio_sigev_thread64(aiocb64_t *);
#endif
extern aio_worker_t *_kaiowp;
extern aio_worker_t *__workers_rw;
extern aio_worker_t *__nextworker_rw;
extern int __rw_workerscnt;
extern aio_worker_t *__workers_no;
extern aio_worker_t *__nextworker_no;
extern int __no_workerscnt;
extern mutex_t __aio_initlock;
extern cond_t __aio_initcv;
extern int __aio_initbusy;
extern mutex_t __aio_mutex;
extern cond_t _aio_iowait_cv;
extern cond_t _aio_waitn_cv;
extern int _max_workers;
extern int _min_workers;
extern sigset_t _worker_set;
extern int _aio_worker_cnt;
extern int _sigio_enabled;
extern pid_t __pid;
extern int __uaio_ok;
extern int _kaio_ok;
extern pthread_key_t _aio_key;
extern aio_req_t *_aio_done_tail;
extern aio_req_t *_aio_done_head;
extern aio_req_t *_aio_doneq;
extern int _aio_freelist_cnt;
extern int _aio_allocated_cnt;
extern int _aio_donecnt;
extern int _aio_doneq_cnt;
extern int _aio_waitncnt;
extern int _aio_outstand_cnt;
extern int _kaio_outstand_cnt;
extern int _aio_req_done_cnt;
extern int _aio_kernel_suspend;
extern int _aio_suscv_cnt;
extern int _aiowait_flag;
extern int _aio_flags;
extern uint32_t *_kaio_supported;
extern const sigset_t maskset;
#ifdef __cplusplus
}
#endif
#endif