Discussion:
AHardwareBuffer_lock not working
(too old to reply)
Shahriar Vaghar
2018-12-03 06:45:54 UTC
Permalink
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
--
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 android-***@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/c50fa7b9-ce79-4507-82c2-12db28916e58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Glenn Kasten
2018-12-04 16:10:12 UTC
Permalink
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:
https://source.android.com/setup/contribute/report-bugs
and be sure to include a complete (but minimal) test case that
includes the threads and relative timing.
Thanks!
Post by Shahriar Vaghar
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
--
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 android-***@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.
Shahriar Vaghar
2018-12-04 20:10:19 UTC
Permalink
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
Post by Glenn Kasten
The code below does not appear to be complete; it does not show the
threads and relative timing.
https://source.android.com/setup/contribute/report-bugs
and be sure to include a complete (but minimal) test case that
includes the threads and relative timing.
Thanks!
Post by Shahriar Vaghar
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
--
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
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
<https://groups.google.com/d/msgid/android-ndk/01fdec2e-c30b-4bd8-b5fc-b6718e227275%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 android-***@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/CAPwkQFFeAnxZVcLZfTsvAsiA0UsRxKhy3h6-w6bOVtPCZZ4Faw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...