pkarashchenko commented on code in PR #10752: URL: https://github.com/apache/nuttx/pull/10752#discussion_r1335022806
########## include/pthread.h: ########## @@ -136,7 +136,7 @@ /* Used to initialize a pthread_once_t */ -#define PTHREAD_ONCE_INIT (false) +#define PTHREAD_ONCE_INIT {false, PTHREAD_MUTEX_INITIALIZER} Review Comment: Also POSIX contains: ``` For dynamic library initialization in a multi-threaded process, if an initialization flag is used the flag needs to be protected against modification by multiple threads simultaneously calling into the library. This can be done by using a mutex (initialized by assigning PTHREAD_MUTEX_INITIALIZER). However, the better solution is to use pthread_once() which is designed for exactly this purpose ``` So in this case I do not see a difference from `PTHREAD_MUTEX_INITIALIZER` described case in POSIX ########## libs/libc/pthread/pthread_once.c: ########## @@ -75,23 +73,21 @@ int pthread_once(FAR pthread_once_t *once_control, /* Prohibit pre-emption while we test and set the once_control. */ - nxrmutex_lock(&g_lock); + pthread_mutex_lock(&once_control->mutex); - if (!*once_control) + if (!once_control->done) { - *once_control = true; + once_control->done = true; Review Comment: I think that `done` should be moved after `init_routine` to satisfy POSIX: ``` The pthread_once() function is not a cancellation point. However, if init_routine is a cancellation point and is canceled, the effect on once_control shall be as if pthread_once() was never called. ``` Also the question about calling a cancellation point API is still in place. How do we release a mutex if the cancellation point API is called? ########## libs/libc/pthread/pthread_once.c: ########## @@ -75,23 +73,21 @@ int pthread_once(FAR pthread_once_t *once_control, /* Prohibit pre-emption while we test and set the once_control. */ - nxrmutex_lock(&g_lock); + pthread_mutex_lock(&once_control->mutex); - if (!*once_control) + if (!once_control->done) { - *once_control = true; + once_control->done = true; Review Comment: I think that `done` should be moved after `init_routine` to satisfy POSIX: ``` The pthread_once() function is not a cancellation point. However, if init_routine is a cancellation point and is canceled, the effect on once_control shall be as if pthread_once() was never called. ``` Also the question about calling a cancellation point API is still in place. How do we release a mutex if the cancellation point API is called? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org