Cring is a lightweight and efficient event loop library written in pure C, designed to simplify asynchronous programming
using the io-uring
interface. Leveraging the power of user-level context switching with ucontext
, It provides a clean and straightforward API for building scalable network applications.
By incorporating ucontext, Cring facilitates the seamless execution of asynchronous tasks, enhancing the overall efficiency of the event-driven paradigm.
Moreover, Cring is designed with a thread-per-core model in mind, emphasizing optimal resource utilization and parallelism.
- Pure C: Cring is written in pure C, making it easy to integrate into your C projects.
- Efficient IO: Utilizes the io-uring interface for high-performance asynchronous IO operations.
- Simple API: Provides a minimalistic and easy-to-use API for handling asynchronous tasks.
Here's a simple example of using Cring to create an echo server:
void client_handler(struct Executor *executor, void *data)
int fd = *(int *)data;
char buffer[PACKET_SIZE];
ssize_t r_len = async_read(executor, fd, (void *)buffer, PACKET_SIZE);
...
ssize_t w_len = async_write(executor, fd, (void *)buffer, r_len);
...
void echo_server(struct Executor *executor, void *data)
while (true) {
int fd = async_accept(executor, *(int *)data);
async_exec(executor, &client_handler, &fd);
}
int main()
struct Executor executor;
init_executor(&executor, 40, 1000);
async_exec(&executor, &pingpong_server, &server_fd);
run(&executor);
This example demonstrates the simplicity and elegance of using Cring to build an echo server that efficiently handles asynchronous IO operations.
For more usage examples, explore the examples folder.
Before building Cring, ensure that you have the following prerequisites installed on your system:
- Linux 5.6 or newer (support io_uring)
- CMake (version 3.16 or higher)
- A C compiler that supports C11 (e.g., GCC or Clang)
- liburing-dev
- Clone the Cring repository to your local machine:
git clone https://github.com/your-username/Cring.git
- pre-build
cmake -B Release .
- Optional: Customize the build by adding CMake options. For example, to enable benchmarking, tests, and examples, run:
cmake -B Release -DCRING_BENCHMARK=ON -DCRING_TESTS=ON -DCRING_EXAMPLES=ON .
- build
cmake --build Release
- To perform a code quality check using Clang-Tidy (if available), run:
cmake --build Release --target clang-tidy-check
Cring is designed for optimal performance, and we provide a comprehensive suite of benchmarks to evaluate its efficiency. For detailed benchmark results and instructions on running specific tests, please refer to the Benchmark folder.