00001
00007 #include <fcntl.h>
00008 #include <unistd.h>
00009 #include <errno.h>
00010 #include <string.h>
00011 #include <stdlib.h>
00012 #include <misc/debug.h>
00013 #include <misc/errno.h>
00014 #include <core/tx.h>
00015 #include <core/config.h>
00016 #include <core/koa.h>
00017 #include <core/buffer.h>
00018 #include <core/stats.h>
00019 #include <libc/syscalls.h>
00020 #include <core/txdesc.h>
00021 #include <xcalls/xcalls.h>
00022
00023
00024 typedef struct x_read_undo_args_s x_read_undo_args_t;
00025
00026 struct x_read_undo_args_s {
00027 int fd;
00028 int nbyte;
00029 };
00030
00031
00032 static
00033 void
00034 x_read_undo(void *args, int *result)
00035 {
00036 x_read_undo_args_t *args_undo = (x_read_undo_args_t *) args;
00037 int local_result = 0;
00038
00039 if (txc_libc_lseek(args_undo->fd,
00040 -args_undo->nbyte, SEEK_CUR) < 0)
00041 {
00042 local_result = errno;
00043 }
00044 if (result) {
00045 *result = local_result;
00046 }
00047 }
00048
00049
00064 ssize_t
00065 XCALL_DEF(x_read)(int fd, void *buf, size_t nbyte, int *result)
00066 {
00067 txc_tx_t *txd;
00068 txc_koamgr_t *koamgr = txc_g_koamgr;
00069 txc_koa_t *koa;
00070 txc_sentinel_t *sentinel;
00071 txc_result_t xret;
00072 int ret;
00073 x_read_undo_args_t *args_undo;
00074 int local_result;
00075
00076
00077 txd = txc_tx_get_txd();
00078
00079 switch(txc_tx_get_xactstate(txd)) {
00080 case TXC_XACTSTATE_TRANSACTIONAL_IRREVOCABLE:
00081 case TXC_XACTSTATE_NONTRANSACTIONAL:
00082 if ((ret = txc_libc_read(fd, buf, nbyte)) < 0) {
00083 local_result = errno;
00084 } else {
00085 local_result = 0;
00086 }
00087 goto done;
00088
00089 case TXC_XACTSTATE_TRANSACTIONAL_RETRYABLE:
00090 txc_koa_lock_fd(koamgr, fd);
00091 xret = txc_koa_lookup_fd2koa(koamgr, fd, &koa);
00092 if (xret == TXC_R_FAILURE) {
00093
00094
00095
00096
00097 txc_koa_unlock_fd(koamgr, fd);
00098 local_result = EBADF;
00099 ret = -1;
00100 goto done;
00101 }
00102 sentinel = txc_koa_get_sentinel(koa);
00103 xret = txc_sentinel_tryacquire(txd, sentinel,
00104 TXC_SENTINEL_ACQUIREONRETRY);
00105 txc_koa_unlock_fd(koamgr, fd);
00106 if (xret == TXC_R_BUSYSENTINEL) {
00107 txc_tx_abort_transaction(txd, TXC_ABORTREASON_BUSYSENTINEL);
00108 TXC_INTERNALERROR("Never gets here. Transaction abort failed.\n");
00109 }
00110
00111
00112
00113 if ((args_undo = (x_read_undo_args_t *)
00114 txc_buffer_linear_malloc(txd->buffer_linear,
00115 sizeof(x_read_undo_args_t)))
00116 == NULL)
00117 {
00118 local_result = ENOMEM;
00119 ret = -1;
00120 goto error_handler_0;
00121 }
00122 args_undo->fd = fd;
00123 if ((ret = txc_libc_read(fd, buf, nbyte)) < 0) {
00124 local_result = errno;
00125 goto error_handler_1;
00126 }
00127 args_undo->nbyte = ret;
00128 txc_tx_register_undo_action(txd, x_read_undo,
00129 (void *) args_undo, result,
00130 TXC_TX_REGULAR_UNDO_ACTION_ORDER);
00131 local_result = 0;
00132 txc_stats_txstat_increment(txd, XCALL, x_read, 1);
00133 goto done;
00134
00135 default:
00136 TXC_INTERNALERROR("Unknown transaction state\n");
00137 }
00138
00139 error_handler_1:
00140 txc_buffer_linear_free(txd->buffer_linear,
00141 sizeof(x_read_undo_args_t));
00142 error_handler_0:
00143 done:
00144 if (result) {
00145 *result = local_result;
00146 }
00147 return ret;
00148 }