tools/thermal/lib/mainloop.c
Source file repositories/reference/linux-study-clean/tools/thermal/lib/mainloop.c
File Facts
- System
- Linux kernel
- Corpus path
tools/thermal/lib/mainloop.c- Extension
.c- Size
- 1521 bytes
- Lines
- 105
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdlib.herrno.hunistd.hsignal.hsys/epoll.hmainloop.hlog.h
Detected Declarations
struct mainloop_datafunction mainloopfunction mainloop_addfunction mainloop_delfunction mainloop_initfunction mainloop_exitfunction mainloop_fini
Annotated Snippet
struct mainloop_data {
mainloop_callback_t cb;
void *data;
int fd;
};
#define MAX_EVENTS 10
int mainloop(unsigned int timeout)
{
int i, nfds;
struct epoll_event events[MAX_EVENTS];
struct mainloop_data *md;
if (epfd < 0)
return -1;
for (;;) {
nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout);
if (exit_mainloop || !nfds)
return 0;
if (nfds < 0) {
if (errno == EINTR)
continue;
return -1;
}
for (i = 0; i < nfds; i++) {
md = events[i].data.ptr;
if (md->cb(md->fd, md->data) > 0)
return 0;
}
}
}
int mainloop_add(int fd, mainloop_callback_t cb, void *data)
{
struct epoll_event ev = {
.events = EPOLLIN,
};
struct mainloop_data *md;
md = malloc(sizeof(*md));
if (!md)
return -1;
md->data = data;
md->cb = cb;
md->fd = fd;
ev.data.ptr = md;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
free(md);
return -1;
}
return 0;
}
int mainloop_del(int fd)
{
if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0)
return -1;
return 0;
}
int mainloop_init(void)
{
epfd = epoll_create(2);
if (epfd < 0)
return -1;
return 0;
}
void mainloop_exit(void)
{
exit_mainloop = 1;
}
void mainloop_fini(void)
{
close(epfd);
Annotation
- Immediate include surface: `stdlib.h`, `errno.h`, `unistd.h`, `signal.h`, `sys/epoll.h`, `mainloop.h`, `log.h`.
- Detected declarations: `struct mainloop_data`, `function mainloop`, `function mainloop_add`, `function mainloop_del`, `function mainloop_init`, `function mainloop_exit`, `function mainloop_fini`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.