object pool, freertos queue and concurrent memory access

braincat
Posts: 11
Joined: Tue Jun 15, 2021 9:54 pm

object pool, freertos queue and concurrent memory access

Postby braincat » Tue Jun 15, 2021 10:09 pm

Coming from programming on intel cpus, I was wondering if it is safe to do the following:

Task 1
  1. SomeStruct *s = object_pool.allocate();
  2. s->field = value;
  3. xQueueSend(xQueue, &s, 0);
  1. Task 2:
  2. SomeStruct *s = nullptr;
  3. xQueueReceive(xQueue, &s, 0)
  4. object_pool.release(s);
  5. // and use data pointed to by "s" pointer
allocate, release calls to object pool are protected by freertos semaphore.

If I were coding for x86-64, I would have to make sure that there is sufficient thread safety
in place, either using a queue with a mutex inside or synchronization using atomics, so that
there is a guarantee that the second task sees values in the memory pointed to by
the received pointer.

Is above safe to do on esp32? Thanks.

ESP_Sprite
Posts: 9020
Joined: Thu Nov 26, 2015 4:08 am

Re: object pool, freertos queue and concurrent memory access

Postby ESP_Sprite » Wed Jun 16, 2021 2:11 am

The queue already guarantees software thread safety. Fyi, some of our drivers use more-or-less this paradigm to shuttle buffers around from/to the interrupt routines.

Who is online

Users browsing this forum: No registered users and 183 guests