1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #include "mbed_poll.h"
- #include "FileHandle.h"
- #if MBED_CONF_RTOS_PRESENT
- #include "rtos/Kernel.h"
- #include "rtos/Thread.h"
- using namespace rtos;
- #else
- #include "Timer.h"
- #include "LowPowerTimer.h"
- #endif
- namespace mbed {
- int poll(pollfh fhs[], unsigned nfhs, int timeout)
- {
-
- #if MBED_CONF_RTOS_PRESENT
- uint64_t start_time = 0;
- if (timeout > 0) {
- start_time = Kernel::get_ms_count();
- }
- #define TIME_ELAPSED() int64_t(Kernel::get_ms_count() - start_time)
- #else
- #if MBED_CONF_PLATFORM_POLL_USE_LOWPOWER_TIMER
- LowPowerTimer timer;
- #else
- Timer timer;
- #endif
- if (timeout > 0) {
- timer.start();
- }
- #define TIME_ELAPSED() timer.read_ms()
- #endif
- int count = 0;
- for (;;) {
-
- for (unsigned n = 0; n < nfhs; n++) {
- FileHandle *fh = fhs[n].fh;
- short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL;
- if (fh) {
- fhs[n].revents = fh->poll(mask) & mask;
- } else {
- fhs[n].revents = POLLNVAL;
- }
- if (fhs[n].revents) {
- count++;
- }
- }
- if (count) {
- break;
- }
-
- if (timeout == 0 || (timeout > 0 && TIME_ELAPSED() > timeout)) {
- break;
- }
- #ifdef MBED_CONF_RTOS_PRESENT
-
-
- rtos::Thread::wait(1);
- #endif
- }
- return count;
- }
- }
|