Home | History | Annotate | Download | only in mtp
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <android-base/logging.h>
     18 #include <condition_variable>
     19 #include <memory>
     20 #include <mutex>
     21 #include <queue>
     22 #include <unistd.h>
     23 
     24 #include "PosixAsyncIO.h"
     25 
     26 namespace {
     27 
     28 void read_func(struct aiocb *aiocbp) {
     29     aiocbp->ret = TEMP_FAILURE_RETRY(pread(aiocbp->aio_fildes,
     30                 aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
     31     if (aiocbp->ret == -1) aiocbp->error = errno;
     32 }
     33 
     34 void write_func(struct aiocb *aiocbp) {
     35     aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
     36                 aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
     37     if (aiocbp->ret == -1) aiocbp->error = errno;
     38 }
     39 
     40 } // end anonymous namespace
     41 
     42 aiocb::~aiocb() {
     43     CHECK(!thread.joinable());
     44 }
     45 
     46 int aio_read(struct aiocb *aiocbp) {
     47     aiocbp->thread = std::thread(read_func, aiocbp);
     48     return 0;
     49 }
     50 
     51 int aio_write(struct aiocb *aiocbp) {
     52     aiocbp->thread = std::thread(write_func, aiocbp);
     53     return 0;
     54 }
     55 
     56 int aio_error(const struct aiocb *aiocbp) {
     57     return aiocbp->error;
     58 }
     59 
     60 ssize_t aio_return(struct aiocb *aiocbp) {
     61     return aiocbp->ret;
     62 }
     63 
     64 int aio_suspend(struct aiocb *aiocbp[], int n,
     65         const struct timespec *) {
     66     for (int i = 0; i < n; i++) {
     67         aiocbp[i]->thread.join();
     68     }
     69     return 0;
     70 }
     71 
     72 void aio_prepare(struct aiocb *aiocbp, void* buf, size_t count, off_t offset) {
     73     aiocbp->aio_buf = buf;
     74     aiocbp->aio_offset = offset;
     75     aiocbp->aio_nbytes = count;
     76 }
     77