Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_poll.cpp Source File

mbed_poll.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "mbed_poll.h"
00018 #include "FileHandle.h"
00019 #include "mbed_thread.h"
00020 
00021 namespace mbed {
00022 
00023 // timeout -1 forever, or milliseconds
00024 int poll(pollfh fhs[], unsigned nfhs, int timeout)
00025 {
00026     /*
00027      * TODO Proper wake-up mechanism.
00028      * In order to correctly detect availability of read/write a FileHandle, we needed
00029      * a select or poll mechanisms. We opted for poll as POSIX defines in
00030      * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently,
00031      * mbed::poll() just spins and scans filehandles looking for any events we are
00032      * interested in. In future, his spinning behaviour will be replaced with
00033      * condition variables.
00034      */
00035     uint64_t start_time = 0;
00036     if (timeout > 0) {
00037         start_time = get_ms_count();
00038     }
00039 
00040     int count = 0;
00041     for (;;) {
00042         /* Scan the file handles */
00043         for (unsigned n = 0; n < nfhs; n++) {
00044             FileHandle *fh = fhs[n].fh;
00045             short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL;
00046             if (fh) {
00047                 fhs[n].revents = fh->poll(mask) & mask;
00048             } else {
00049                 fhs[n].revents = POLLNVAL;
00050             }
00051             if (fhs[n].revents) {
00052                 count++;
00053             }
00054         }
00055 
00056         if (count) {
00057             break;
00058         }
00059 
00060         /* Nothing selected - this is where timeout handling would be needed */
00061         if (timeout == 0 || (timeout > 0 && int64_t(get_ms_count() - start_time) > timeout)) {
00062             break;
00063         }
00064         // TODO - proper blocking
00065         // wait for condition variable, wait queue whatever here
00066         thread_sleep_for(1);
00067     }
00068     return count;
00069 }
00070 
00071 } // namespace mbed