| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _UST_WAIT_H |
| 8 | #define _UST_WAIT_H |
| 9 | |
| 10 | #include <poll.h> |
| 11 | |
| 12 | /* |
| 13 | * Wait until "cond" gets true or timeout (in ms). |
| 14 | */ |
| 15 | #define wait_cond_interruptible_timeout(_cond, _timeout) \ |
| 16 | ({ \ |
| 17 | int __ret = 0, __pollret; \ |
| 18 | int __timeout = _timeout; \ |
| 19 | \ |
| 20 | for (;;) { \ |
| 21 | if (_cond) \ |
| 22 | break; \ |
| 23 | if (__timeout <= 0) { \ |
| 24 | __ret = -ETIMEDOUT; \ |
| 25 | break; \ |
| 26 | } \ |
| 27 | __pollret = poll(NULL, 0, 10); /* wait 10ms */ \ |
| 28 | if (__pollret < 0) { \ |
| 29 | __ret = -errno; \ |
| 30 | break; \ |
| 31 | } \ |
| 32 | __timeout -= 10; \ |
| 33 | } \ |
| 34 | __ret; \ |
| 35 | }) |
| 36 | |
| 37 | |
| 38 | #endif /* _UST_WAIT_H */ |