1c72fcc34Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2c72fcc34Sopenharmony_ci//
3c72fcc34Sopenharmony_ci// waiter-poll.c - Waiter for event notification by poll(2).
4c72fcc34Sopenharmony_ci//
5c72fcc34Sopenharmony_ci// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6c72fcc34Sopenharmony_ci//
7c72fcc34Sopenharmony_ci// Licensed under the terms of the GNU General Public License, version 2.
8c72fcc34Sopenharmony_ci
9c72fcc34Sopenharmony_ci#include "waiter.h"
10c72fcc34Sopenharmony_ci#include "misc.h"
11c72fcc34Sopenharmony_ci
12c72fcc34Sopenharmony_ci#include <stdlib.h>
13c72fcc34Sopenharmony_ci#include <errno.h>
14c72fcc34Sopenharmony_ci#include <poll.h>
15c72fcc34Sopenharmony_ci
16c72fcc34Sopenharmony_cistatic int poll_prepare(struct waiter_context *waiter ATTRIBUTE_UNUSED)
17c72fcc34Sopenharmony_ci{
18c72fcc34Sopenharmony_ci	// Nothing to do because an instance of waiter has required data.
19c72fcc34Sopenharmony_ci	return 0;
20c72fcc34Sopenharmony_ci}
21c72fcc34Sopenharmony_ci
22c72fcc34Sopenharmony_cistatic int poll_wait_event(struct waiter_context *waiter, int timeout_msec)
23c72fcc34Sopenharmony_ci{
24c72fcc34Sopenharmony_ci	int err;
25c72fcc34Sopenharmony_ci
26c72fcc34Sopenharmony_ci	err = poll(waiter->pfds, waiter->pfd_count, timeout_msec);
27c72fcc34Sopenharmony_ci	if (err < 0)
28c72fcc34Sopenharmony_ci		return -errno;
29c72fcc34Sopenharmony_ci
30c72fcc34Sopenharmony_ci	return err;
31c72fcc34Sopenharmony_ci}
32c72fcc34Sopenharmony_ci
33c72fcc34Sopenharmony_cistatic void poll_release(struct waiter_context *waiter ATTRIBUTE_UNUSED)
34c72fcc34Sopenharmony_ci{
35c72fcc34Sopenharmony_ci	// Nothing to do because an instance of waiter has required data.
36c72fcc34Sopenharmony_ci	return;
37c72fcc34Sopenharmony_ci}
38c72fcc34Sopenharmony_ci
39c72fcc34Sopenharmony_ciconst struct waiter_data waiter_poll = {
40c72fcc34Sopenharmony_ci	.ops = {
41c72fcc34Sopenharmony_ci		.prepare	= poll_prepare,
42c72fcc34Sopenharmony_ci		.wait_event	= poll_wait_event,
43c72fcc34Sopenharmony_ci		.release	= poll_release,
44c72fcc34Sopenharmony_ci	},
45c72fcc34Sopenharmony_ci};
46