Skip to content

Commit d08f097

Browse files
authored
Fix 'fdatasync' task and change interfaces name. (#1792)
1 parent 5246a41 commit d08f097

9 files changed

Lines changed: 32 additions & 29 deletions

docs/en/tutorial-09-http_file_server.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public:
116116
~~~
117117

118118
Both pread and pwrite return WFFileIOTask. We do not distinguish between sort and psort, and we do not distinguish between client and server task. They all follow the same principle.
119-
In addition to these two interfaces, preadv and pwritev return WFFileVIOTask; fsync and fdsync return WFFileSyncTask. You can see the details in the header file.
119+
In addition to these two interfaces, preadv and pwritev return WFFileVIOTask; fsync and fdatasync return WFFileSyncTask. You can see the details in the header file.
120120
The example uses the user\_data field of the task to save the global data of the service. For larger services, we recommend to use series context. You can see the [proxy examples](/tutorial/tutorial-05-http_proxy.cc) for details.
121121

122122
# Handling file reading results
@@ -199,4 +199,4 @@ Linux operating system supports a set of asynchronous IO system calls with high
199199
We have implemented a set of posix aio interfaces to support other UNIX systems, and used the sigevent notification method of threads, but it is no longer in use because of its low efficiency.
200200
Currently, for non-Linux systems, asynchronous IO is always simulated by multi-threading. When an IO task arrives, a thread is created in real time to execute IO tasks, and then a callback is used to return to the handler thread pool.
201201
Multi-threaded IO is also the only choice in macOS, because macOS does not have good sigevent support and posix aio will not work in macOS.
202-
Some UNIX systems do not support fdatasync. In this case, an fdsync task is equivalent to an fsync task.
202+
Some UNIX systems do not support fdatasync. In this case, an fdatasync task is equivalent to an fsync task.

docs/tutorial-09-http_file_server.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public:
110110
};
111111
~~~
112112
无论是pread还是pwrite,返回的都是WFFileIOTask。这与不区分sort或psort,不区分client或server task是一个道理。
113-
除这两个接口还有preadv和pwritev,返回WFFileVIOTask,以及fsync,fdsync,返回WFFileSyncTask。可以在头文件里查看。
113+
除这两个接口还有preadv和pwritev,返回WFFileVIOTask,以及fsync,fdatasync,返回WFFileSyncTask。可以在头文件里查看。
114114
示例用了task的user_data域保存服务的全局数据。但对于大服务,我们推荐使用series context。可以参考前面的[proxy示例](../tutorial/tutorial-05-http_proxy.cc)
115115

116116
# 处理读文件结果
@@ -195,5 +195,5 @@ Linux操作系统支持一套效率很高,CPU占用非常少的异步IO系统
195195
我们曾经实现过一套posix aio接口用于支持其它UNIX系统,并使用线程的sigevent通知方式,但由于其效率太低,已经不再使用了。
196196
目前,对于非Linux系统,异步IO一律是用多线程实现,在IO任务到达时,实时创建线程执行IO任务,callback回到handler线程池。
197197
多线程IO也是macOS下的唯一选择,因为macOS没有良好的sigevent支持,posix aio行不通。
198-
某些UNIX系统不支持fdatasync调用,这种情况下,fdsync任务将等价于fsync任务
198+
某些UNIX系统不支持fdatasync调用,这种情况下,fdatasync任务将等价于fsync任务
199199

src/factory/FileTaskImpl.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ class WFFilefsyncTask : public WFFileSyncTask
128128
}
129129
};
130130

131-
class WFFilefdsyncTask : public WFFileSyncTask
131+
class WFFilefdatasyncTask : public WFFileSyncTask
132132
{
133133
public:
134-
WFFilefdsyncTask(int fd, IOService *service, fsync_callback_t&& cb) :
134+
WFFilefdatasyncTask(int fd, IOService *service, fsync_callback_t&& cb) :
135135
WFFileSyncTask(service, std::move(cb))
136136
{
137137
this->args.fd = fd;
@@ -140,7 +140,7 @@ class WFFilefdsyncTask : public WFFileSyncTask
140140
protected:
141141
virtual int prepare()
142142
{
143-
this->prep_fdsync(this->args.fd);
143+
this->prep_fdatasync(this->args.fd);
144144
return 0;
145145
}
146146
};
@@ -344,12 +344,12 @@ WFFileSyncTask *WFTaskFactory::create_fsync_task(int fd,
344344
std::move(callback));
345345
}
346346

347-
WFFileSyncTask *WFTaskFactory::create_fdsync_task(int fd,
348-
fsync_callback_t callback)
347+
WFFileSyncTask *WFTaskFactory::create_fdatasync_task(int fd,
348+
fsync_callback_t callback)
349349
{
350-
return new WFFilefdsyncTask(fd,
351-
WFGlobal::get_io_service(),
352-
std::move(callback));
350+
return new WFFilefdatasyncTask(fd,
351+
WFGlobal::get_io_service(),
352+
std::move(callback));
353353
}
354354

355355
/* Factory functions with path name. */

src/factory/WFTaskFactory.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,8 @@ class WFTaskFactory
184184
static WFFileSyncTask *create_fsync_task(int fd,
185185
fsync_callback_t callback);
186186

187-
/* On systems that do not support fdatasync(), like macOS,
188-
* fdsync task is equal to fsync task. */
189-
static WFFileSyncTask *create_fdsync_task(int fd,
190-
fsync_callback_t callback);
187+
static WFFileSyncTask *create_fdatasync_task(int fd,
188+
fsync_callback_t callback);
191189

192190
/* File tasks with path name. */
193191
public:

src/kernel/IOService_linux.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void IOSession::prep_fsync(int fd)
235235
iocb->aio_lio_opcode = IO_CMD_FSYNC;
236236
}
237237

238-
void IOSession::prep_fdsync(int fd)
238+
void IOSession::prep_fdatasync(int fd)
239239
{
240240
struct iocb *iocb = (struct iocb *)this->iocb_buf;
241241

src/kernel/IOService_linux.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class IOSession
4343
void prep_pwritev(int fd, const struct iovec *iov, int iovcnt,
4444
long long offset);
4545
void prep_fsync(int fd);
46-
void prep_fdsync(int fd);
46+
void prep_fdatasync(int fd);
4747

4848
protected:
4949
long get_res() const { return this->res; }

src/kernel/IOService_thread.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void IOSession::prep_fsync(int fd)
8080
this->op = IO_CMD_FSYNC;
8181
}
8282

83-
void IOSession::prep_fdsync(int fd)
83+
void IOSession::prep_fdatasync(int fd)
8484
{
8585
this->fd = fd;
8686
this->op = IO_CMD_FDSYNC;
@@ -104,6 +104,12 @@ int IOService::init(int maxevents)
104104
return -1;
105105
}
106106

107+
p = dlsym(RTLD_DEFAULT, "fdatasync");
108+
if (p)
109+
this->fdatasync = (int (*)(int))p;
110+
else
111+
this->fdatasync = fsync;
112+
107113
p = dlsym(RTLD_DEFAULT, "preadv");
108114
if (p)
109115
this->preadv = (ssize_t (*)(int, const struct iovec *, int, off_t))p;
@@ -216,15 +222,11 @@ void *IOService::io_routine(void *arg)
216222
case IO_CMD_PWRITE:
217223
ret = pwrite(fd, session->buf, session->count, session->offset);
218224
break;
225+
case IO_CMD_FSYNC:
219226
ret = fsync(fd);
220227
break;
221228
case IO_CMD_FDSYNC:
222-
#if _POSIX_SYNCHRONIZED_IO > 0
223-
ret = fdatasync(fd);
224-
break;
225-
#endif
226-
case IO_CMD_FSYNC:
227-
ret = fsync(fd);
229+
ret = service->fdatasync(fd);
228230
break;
229231
case IO_CMD_PREADV:
230232
ret = service->preadv(fd, (const struct iovec *)session->buf,

src/kernel/IOService_thread.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class IOSession
4343
void prep_pwritev(int fd, const struct iovec *iov, int iovcnt,
4444
long long offset);
4545
void prep_fsync(int fd);
46-
void prep_fdsync(int fd);
46+
void prep_fdatasync(int fd);
4747

4848
protected:
4949
long get_res() const { return this->res; }
@@ -105,13 +105,16 @@ class IOService
105105
static void *io_routine(void *arg);
106106
static void *aio_finish(void *ptr, void *context);
107107

108+
private:
109+
int (*fdatasync)(int);
110+
ssize_t (*preadv)(int, const struct iovec *, int, off_t);
111+
ssize_t (*pwritev)(int, const struct iovec *, int, off_t);
112+
108113
private:
109114
static ssize_t preadv_emul(int fd, const struct iovec *iov, int iovcnt,
110115
off_t offset);
111116
static ssize_t pwritev_emul(int fd, const struct iovec *iov, int iovcnt,
112117
off_t offset);
113-
ssize_t (*preadv)(int, const struct iovec *, int, off_t);
114-
ssize_t (*pwritev)(int, const struct iovec *, int, off_t);
115118

116119
public:
117120
virtual ~IOService() { }

src/manager/WFFacilities.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ inline WFFuture<int> WFFacilities::async_fdatasync(int fd)
131131
{
132132
auto *pr = new WFPromise<int>();
133133
auto fr = pr->get_future();
134-
auto *task = WFTaskFactory::create_fdsync_task(fd, __fsync_future_callback);
134+
auto *task = WFTaskFactory::create_fdatasync_task(fd, __fsync_future_callback);
135135

136136
task->user_data = pr;
137137
task->start();

0 commit comments

Comments
 (0)