Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear documentation: Is malloc task/threadsafe? (IDFGH-1502) #3768

Closed
raldone01 opened this issue Jul 11, 2019 · 8 comments
Closed

Unclear documentation: Is malloc task/threadsafe? (IDFGH-1502) #3768

raldone01 opened this issue Jul 11, 2019 · 8 comments

Comments

@raldone01
Copy link
Contributor

I carefully read through https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/mem_alloc.html but I couldn't find anything regarding task/thread safety.

Do I have to guard malloc and free calls from concurrent access?
I would suggest to update the documentation and add that info.

@github-actions github-actions bot changed the title Unclear documentation: Is malloc task/threadsafe? Unclear documentation: Is malloc task/threadsafe? (IDFGH-1502) Jul 11, 2019
@PerMalmberg
Copy link
Contributor

PerMalmberg commented Jul 11, 2019

No, you do not. It's guaranteed by the language (C & C++), assuming you're using a modern version of it, (C11 ?). I have a hard time seeing you running into issues if you use a tool chain provided by Espressif though.

Note however that calling malloc and friends from an interrupt is not safe as the interrupt may have interrupted a call to malloc/free etc.

@raldone01
Copy link
Contributor Author

Thank you I didn't know that was the case as the memory functions are written by expressif.

@negativekelvin
Copy link
Contributor

negativekelvin commented Jul 11, 2019

/* Because malloc/free can happen inside an ISR context,
we need to use portmux spinlocks here not RTOS mutexes */
#define MULTI_HEAP_LOCK(PLOCK) do { \
if((PLOCK) != NULL) { \
portENTER_CRITICAL((portMUX_TYPE *)(PLOCK)); \
} \
} while(0)
#define MULTI_HEAP_UNLOCK(PLOCK) do { \
if ((PLOCK) != NULL) { \
portEXIT_CRITICAL((portMUX_TYPE *)(PLOCK)); \
} \
} while(0)

@raldone01
Copy link
Contributor Author

Thank you. So is malloc safe in ISRs?

@PerMalmberg
Copy link
Contributor

/* Because malloc/free can happen inside an ISR context,
we need to use portmux spinlocks here not RTOS mutexes */
#define MULTI_HEAP_LOCK(PLOCK) do { \
if((PLOCK) != NULL) { \
portENTER_CRITICAL((portMUX_TYPE *)(PLOCK)); \
} \
} while(0)
#define MULTI_HEAP_UNLOCK(PLOCK) do { \
if ((PLOCK) != NULL) { \
portEXIT_CRITICAL((portMUX_TYPE *)(PLOCK)); \
} \
} while(0)

Ah, seems an ISR is allowed to call malloc too, that's nice.

@raldone01
Copy link
Contributor Author

So the documentation is unclear?

@raldone01 raldone01 reopened this Jul 11, 2019
@projectgus
Copy link
Contributor

projectgus commented Jul 12, 2019

@raldone01 malloc(), free(), and other heap functions are thread safe.

It is technically possible to malloc() or free() from an ISR. It is not recommended to call malloc() or free() from an ISR as they can take a long time to run, especially if you have PSRAM with a lot of small allocations in it. Better to structure your code in a way that you pre-allocate any buffer that is needed by the ISR.

@projectgus
Copy link
Contributor

Will reopen as I've made a clarification in the docs about this, but it hasn't pushed to GItHub yet. This issue will be closed once it has.

@projectgus projectgus reopened this Jul 15, 2019
@igrr igrr closed this as completed in 0f27c38 Jul 15, 2019
trombik pushed a commit to trombik/esp-idf that referenced this issue Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants