AHardwareBuffer_lock not working

263 views
Skip to first unread message

Shahriar Vaghar

unread,
Dec 3, 2018, 6:32:08 AM12/3/18
to android-ndk
Hello,

In the code below, while consumer has grabbed the lock, once in a while, producer gets the lock and writes to shared buffer!

What am I doing wrong? My flags are wrong? I need only one of the functions have access to the shared buffer at one time.

producer and consumer are running in two separate processes.


static AHardwareBuffer* hardware_buffer;

void producer() {
    void* shared_buffer = NULL;

    int ret = AHardwareBuffer_lock(hardware_buffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, NULL, &shared_buffer);
    if (ret != 0) {
        ALOGE("%s: Failed to AHardwareBuffer_lock", __func__);
       return;
    }

    // do work - write data to shared_buffer

    ret = AHardwareBuffer_unlock(adev->h_buffer, NULL);
    if (ret != 0) {
        ALOGE("%s: Failed to AHardwareBuffer_unlock", __func__);
    }
}


void consumer() {
    void* shared_buffer = NULL;

    int ret = AHardwareBuffer_lock(hardware_buffer, AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, -1, NULL, &shared_buffer);
    if (ret != 0) {
        LOG("Failed to AHardwareBuffer_lock %d", ret);
        return;
    }

    // do work - read data from shared_buffer

    ret = AHardwareBuffer_unlock(hardware_buffer, NULL);
    if (ret != 0) {
        LOG("failed to AHardwareBuffer_unlock %d", ret);
    }
}


Thanks, Shahriar

Glenn Kasten

unread,
Dec 4, 2018, 11:10:12 AM12/4/18
to android-ndk
The code below does not appear to be complete; it does not show the threads and relative timing.
If you still believe it is a bug, please file a bug here:
and be sure to include a complete (but minimal) test case that
includes the threads and relative timing.
Thanks!

Shahriar Vaghar

unread,
Dec 4, 2018, 4:35:22 PM12/4/18
to andro...@googlegroups.com
Hello,

Thanks for your reply.

The consumer is called by a timer that fires every 4 millisecond.
In the actual implementation, the producer is the audioservice (audio hal), which is streaming audio packets at a rate of 4 msecs. But you could replace that with another timer.

Based on my code instrumentation and logs, timer fires at various rates. So lock is not grabbed at a constant intervals.

You should be able to repo this easily, For example, you could have to int counters in shared memory. One counter is incremented by the consumer and the other by producer. print the counters after lock and before unlock.

int create_timer() {
    timer_t timerid;
    struct sigevent se;
    memset(&se, 0, sizeof(struct sigevent));
    se.sigev_notify_function = consumer;
    se.sigev_notify = SIGEV_THREAD;
    se.sigev_value.sival_ptr = &timerid;
    se.sigev_notify_attributes = NULL;

    int status = timer_create(CLOCK_BOOTTIME, &se, &timerid);
    if (status != 0) {
      LOG("failed to create timer: %s", strerror(errno));
      return status;
    }

    struct itimerspec ts;
    ts.it_value.tv_sec = 0;
    ts.it_value.tv_nsec = 9000000; // 9 milli seconds
    ts.it_interval.tv_sec = 0;
    ts.it_interval.tv_nsec = 4000000; // 4 milli seconds
    status = timer_settime(timerid, 0, &ts, NULL);
    if (status == -1) {
      LOG("failed to settimer, %s", strerror(errno));
    }

    return status;
}

Thanks, Shahriar

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/01fdec2e-c30b-4bd8-b5fc-b6718e227275%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages