Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed_poll.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "mbed_poll.h" 00017 #include "FileHandle.h" 00018 #if MBED_CONF_RTOS_PRESENT 00019 #include "rtos/Kernel.h" 00020 #include "rtos/Thread.h" 00021 using namespace rtos; 00022 #else 00023 #include "Timer.h" 00024 #include "LowPowerTimer.h" 00025 #endif 00026 00027 namespace mbed { 00028 00029 // timeout -1 forever, or milliseconds 00030 int poll(pollfh fhs[], unsigned nfhs, int timeout) 00031 { 00032 /** 00033 * TODO Proper wake-up mechanism. 00034 * In order to correctly detect availability of read/write a FileHandle, we needed 00035 * a select or poll mechanisms. We opted for poll as POSIX defines in 00036 * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently, 00037 * mbed::poll() just spins and scans filehandles looking for any events we are 00038 * interested in. In future, his spinning behaviour will be replaced with 00039 * condition variables. 00040 */ 00041 #if MBED_CONF_RTOS_PRESENT 00042 uint64_t start_time = 0; 00043 if (timeout > 0) { 00044 start_time = Kernel::get_ms_count(); 00045 } 00046 #define TIME_ELAPSED() int64_t(Kernel::get_ms_count() - start_time) 00047 #else 00048 #if MBED_CONF_PLATFORM_POLL_USE_LOWPOWER_TIMER 00049 LowPowerTimer timer; 00050 #else 00051 Timer timer; 00052 #endif 00053 if (timeout > 0) { 00054 timer.start(); 00055 } 00056 #define TIME_ELAPSED() timer.read_ms() 00057 #endif // MBED_CONF_RTOS_PRESENT 00058 00059 int count = 0; 00060 for (;;) { 00061 /* Scan the file handles */ 00062 for (unsigned n = 0; n < nfhs; n++) { 00063 FileHandle *fh = fhs[n].fh; 00064 short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL; 00065 if (fh) { 00066 fhs[n].revents = fh->poll(mask) & mask; 00067 } else { 00068 fhs[n].revents = POLLNVAL; 00069 } 00070 if (fhs[n].revents) { 00071 count++; 00072 } 00073 } 00074 00075 if (count) { 00076 break; 00077 } 00078 00079 /* Nothing selected - this is where timeout handling would be needed */ 00080 if (timeout == 0 || (timeout > 0 && TIME_ELAPSED() > timeout)) { 00081 break; 00082 } 00083 #ifdef MBED_CONF_RTOS_PRESENT 00084 // TODO - proper blocking 00085 // wait for condition variable, wait queue whatever here 00086 rtos::Thread::wait(1); 00087 #endif 00088 } 00089 return count; 00090 } 00091 00092 } // namespace mbed
Generated on Tue Jul 12 2022 12:45:29 by
