async-append: New library to allow asynchronous appending to a log file.
[sliver-openvswitch.git] / lib / async-append.h
1 /* Copyright (c) 2013 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef ASYNC_APPEND_H
17 #define ASYNC_APPEND_H 1
18
19 #include <stddef.h>
20
21 /* This module defines a simple, abstract interface to asynchronous file I/O.
22  * It is currently used only for logging.  Thus, for now the interface only
23  * supports appending to a file.  Multiple implementations are possible
24  * depending on the operating system's degree and form of support for
25  * asynchronous I/O.
26  *
27  * The comments below document the requirements on any implementation.
28  *
29  * Thread-safety
30  * =============
31  *
32  * Only a single thread may use a given 'struct async_append' at one time.
33  */
34
35 /* Enables using asynchronous I/O.  Some implementations may treat this as a
36  * no-op.
37  *
38  * Before this function is called, the POSIX aio implementation uses ordinary
39  * synchronous I/O because some POSIX aio libraries actually use threads
40  * internally, which has enough cost and robustness implications that it's
41  * better to use asynchronous I/O only when it has real expected benefits.
42  *
43  * Must be called while the process is still single-threaded.  May forbid the
44  * process from subsequently forking. */
45 void async_append_enable(void);
46
47 /* Creates and returns a new asynchronous appender for file descriptor 'fd',
48  * which the caller must have opened in append mode (O_APPEND).
49  *
50  * This function must always succeed.  If the system is for some reason unable
51  * to support asynchronous I/O on 'fd' then the library must fall back to
52  * syncrhonous I/O. */
53 struct async_append *async_append_create(int fd);
54
55 /* Destroys 'ap', without closing its underlying file descriptor. */
56 void async_append_destroy(struct async_append *ap);
57
58 /* Appends the 'size' bytes of 'data' to 'ap', asynchronously if possible. */
59 void async_append_write(struct async_append *ap,
60                         const void *data, size_t size);
61
62 /* Blocks until all data asynchronously written to 'ap' with
63  * async_append_write() has been committed to the point that it will be written
64  * to disk barring an operating system or hardware failure. */
65 void async_append_flush(struct async_append *ap);
66
67 #endif /* async-append.h */