Skip to contentSkip to conversation

Posix skin and Mutex initialisation/destruction inconsistencies

DISCUSSION Lange Norbert 2 participants 2 messages Mar 6, 2019

lore
Share & tools
lore
#0 lore raw

Hello,

1)
There is an inconsistency with the documentation [1], which claims that mutex and condition variables need to be explicitly initialized with the *_init functions.
The implementation however checks the state via a flag and calls *_init if necessary, and the program below works correctly.

2)
There is some further issue, if you depend on the "lazy initialization", demonstrated by running the program below with an argument. This case can happen for example if some subsystem was removed before being used (never needed the lock).
In other words, the "lazy initialization" scheme is working with lock/unlock but not with destroy.

#include <assert.h>
#include <pthread.h>

int main(int argc, char const *argv[]) {
        pthread_mutex_t mymutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
  if (argc == 1) {
    assert(0 == pthread_mutex_lock(&mymutex));
    assert(0 == pthread_mutex_unlock(&mymutex));
  }
  assert(0 == pthread_mutex_destroy(&mymutex));

return 0;
}

[1] - https://gitlab.denx.de/Xenomai/xenomai/wikis/Porting_To_Xenomai_POSIX

Regards, Norbert
________________________________

This message and any attachments are solely for the use of the intended recipients. They may contain privileged and/or confidential information or other information protected from disclosure. If you are not an intended recipient, you are hereby notified that you received this email in error and that any review, dissemination, distribution or copying of this email and any attachment is strictly prohibited. If you have received this email in error, please contact the sender and delete the message and any attachment from your system.

ANDRITZ HYDRO GmbH

Rechtsform/ Legal form: Gesellschaft mit beschränkter Haftung / Corporation

Firmensitz/ Registered seat: Wien

Firmenbuchgericht/ Court of registry: Handelsgericht Wien

Firmenbuchnummer/ Company registration: FN 61833 g

DVR: 0605077

UID-Nr.: ATU14756806

Thank You
________________________________

#1 lore raw
Quoted 6 lines · Lange Norbert via Xenomai

Hello,

1)
There is an inconsistency with the documentation [1], which claims that mutex and condition variables need to be explicitly initialized with the *_init functions.
The implementation however checks the state via a flag and calls *_init if necessary, and the program below works correctly.

Lazy init is not a recommended pattern as it implies high costs and potentially unexpected (and often unhandled) errors for typically critical lock/unlock operations. I would guess that this is why it was not explicitly mentioned.

Quoted 11 lines

2)
There is some further issue, if you depend on the "lazy initialization", demonstrated by running the program below with an argument. This case can happen for example if some subsystem was removed before being used (never needed the lock).
In other words, the "lazy initialization" scheme is working with lock/unlock but not with destroy.

#include <assert.h>
#include <pthread.h>

Quoted 8 lines
int main(int argc, char const *argv[]) {
         pthread_mutex_t mymutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
   if (argc == 1) {
     assert(0 == pthread_mutex_lock(&mymutex));
     assert(0 == pthread_mutex_unlock(&mymutex));
   }
   assert(0 == pthread_mutex_destroy(&mymutex));

That should probably be fixed. Patch welcome.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux