pastebin - collaborative debugging tool
rovema.kpaste.net RSS


rds: Add PTHREAD_PRIO_INHERIT to all pthread mutex usage
Posted by Anonymous on Tue 2nd Aug 2022 16:34
raw | new post
view followups (newest first): rds: Add PTHREAD_PRIO_INHERIT to all pthread mutex usage by Anonymous

  1. Index: Base/PosixSystem.cpp
  2. ===================================================================
  3. --- Base/PosixSystem.cpp        (revision 9632)
  4. +++ Base/PosixSystem.cpp        (working copy)
  5. @@ -1163,14 +1163,32 @@
  6.  {
  7.      int errorVal = 0;
  8.      EVENT* event = new EVENT; //old: static_cast<EVENT *>(malloc(sizeof(EVENT)));
  9. +    pthread_mutexattr_t pmattr;
  10.  
  11.      event->flag  = false;
  12.      event->activ = true;
  13.  
  14. +
  15. +    errorVal = pthread_mutexattr_init(&pmattr);
  16. +    POSIX_ERROR(errorVal);
  17. +    if (errorVal != 0)
  18. +    {
  19. +        pthread_mutexattr_destroy(&pmattr);
  20. +        return NULL;
  21. +    }
  22. +
  23. +    errorVal = pthread_mutexattr_setprotocol(&pmattr, PTHREAD_PRIO_INHERIT);
  24. +    if (errorVal != 0)
  25. +    {
  26. +        pthread_mutexattr_destroy(&pmattr);
  27. +        return NULL;
  28. +    }
  29. +    
  30.      DBG_ASSERT(event != NULL);
  31. -    errorVal     = pthread_mutex_init(&event->crit, 0);
  32. +    errorVal = pthread_mutex_init(&event->crit, &pmattr);
  33.      if(errorVal != 0) {
  34.          POSIX_ERROR(errorVal);
  35. +       pthread_mutexattr_destroy(&pmattr);
  36.          delete event;
  37.          return NULL;
  38.      }
  39. @@ -1181,6 +1199,9 @@
  40.          (void)pthread_mutex_destroy(&event->crit);
  41.          delete event;
  42.      }
  43. +
  44. +    pthread_mutexattr_destroy(&pmattr);
  45. +
  46.      return 0 == errorVal ? event : NULL;
  47.  }
  48.  
  49. Index: Boost/boost_1_33_1/boost/detail/atomic_count_pthreads.hpp
  50. ===================================================================
  51. --- Boost/boost_1_33_1/boost/detail/atomic_count_pthreads.hpp   (revision 9632)
  52. +++ Boost/boost_1_33_1/boost/detail/atomic_count_pthreads.hpp   (working copy)
  53. @@ -54,7 +54,15 @@
  54.  
  55.      explicit atomic_count(long v): value_(v)
  56.      {
  57. -        pthread_mutex_init(&mutex_, 0);
  58. +       pthread_mutexattr_t muattr;
  59. +       /*
  60. +        * fixme: replace (void)-casts with real error checks
  61. +        * throwing real C++ exceptions
  62. +        */
  63. +       (void)pthread_mutexattr_init(&muattr);
  64. +       (void)pthread_mutexattr_setprotocol(&muattr, PTHREAD_PRIO_INHERIT);
  65. +        (void)pthread_mutex_init(&mutex_, &muattr);
  66. +       (void)pthread_mutexattr_destroy(&muattr);
  67.      }
  68.  
  69.      ~atomic_count()
  70. Index: Communication/CifXTCPServer/OS_Specific.c
  71. ===================================================================
  72. --- Communication/CifXTCPServer/OS_Specific.c   (revision 9632)
  73. +++ Communication/CifXTCPServer/OS_Specific.c   (working copy)
  74. @@ -116,6 +116,13 @@
  75.      perror("set mutex recursive");
  76.      return NULL;
  77.    }
  78. +
  79. +  if( pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT) != 0 )
  80. +  {
  81. +    perror("set mutex protocol PTHREAD_PRIO_INHERIT");
  82. +    return NULL;
  83. +  }
  84. +
  85.    g_ptMutex =(pthread_mutex_t*)malloc( sizeof(pthread_mutex_t) );
  86.    if( g_ptMutex == NULL )
  87.    {
  88. Index: Communication/ESDK_src/Platform/Linux/platform.c
  89. ===================================================================
  90. --- Communication/ESDK_src/Platform/Linux/platform.c    (revision 9632)
  91. +++ Communication/ESDK_src/Platform/Linux/platform.c    (working copy)
  92. @@ -646,8 +646,15 @@
  93.  
  94.         if(pMutex != NULL)
  95.         {
  96. -               pthread_mutex_init(pMutex, NULL);
  97. -       }
  98. +               pthread_mutexattr_t attr;
  99. +               /*
  100. +                * fixme: replace (void)-casts with real error checks
  101. +                */
  102. +               (void)pthread_mutexattr_init(&attr);
  103. +               (void)pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
  104. +               (void)pthread_mutex_init(pMutex, &attr);
  105. +               (void)pthread_mutexattr_destroy(&attr);
  106. +       }
  107.  
  108.         return(pMutex);
  109.  }
  110. Index: Communication/Encryption/mbedtls/crypto/library/threading.c
  111. ===================================================================
  112. --- Communication/Encryption/mbedtls/crypto/library/threading.c (revision 9632)
  113. +++ Communication/Encryption/mbedtls/crypto/library/threading.c (working copy)
  114. @@ -70,10 +70,23 @@
  115.  #if defined(MBEDTLS_THREADING_PTHREAD)
  116.  static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
  117.  {
  118. -    if( mutex == NULL )
  119. -        return;
  120. +       pthread_mutexattr_t attr;
  121.  
  122. -    mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
  123. +       if (mutex == NULL)
  124. +               return;
  125. +       if (pthread_mutexattr_init(&attr) != 0)
  126. +               return;
  127. +       if (pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) != 0)
  128. +               goto err;
  129. +       if (pthread_mutex_init(&mutex->mutex, &attr) != 0)
  130. +               goto err;
  131. +       (void)pthread_mutexattr_destroy(&attr);
  132. +
  133. +       mutex->is_valid = true;
  134. +       return;
  135. +err:
  136. +       (void)pthread_mutexattr_destroy(&attr);
  137. +       mutex->is_valid = false;
  138.  }
  139.  
  140.  static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
  141. Index: Communication/Sercos3Base/src/Driver/SERC_driver.cpp
  142. ===================================================================
  143. --- Communication/Sercos3Base/src/Driver/SERC_driver.cpp        (revision 9632)
  144. +++ Communication/Sercos3Base/src/Driver/SERC_driver.cpp        (working copy)
  145. @@ -197,7 +197,14 @@
  146.          pCard->pDmaRegister = 0;
  147.      }
  148.  
  149. -    pthread_mutex_init(&cards->card[card].mutex, 0);
  150. +    pthread_mutexattr_t muattr;
  151. +    /*
  152. +     * fixme: replace (void)-casts with real error checks
  153. +     */
  154. +    (void)pthread_mutexattr_init(&muattr);
  155. +    (void)pthread_mutexattr_setprotocol(&muattr, PTHREAD_PRIO_INHERIT);
  156. +    (void)pthread_mutex_init(&cards->card[card].mutex, &muattr);
  157. +    (void)pthread_mutexattr_destroy(&muattr);
  158.  
  159.      cards->card[card].param.sched_priority = DEFAULT_CYCLE_PRIO;
  160.      cards->card[card].nrt_param.sched_priority = DEFAULT_NRT_PRIO;
  161. Index: Communication/Sercos3Base/src/Driver/SERC_nrt.cpp
  162. ===================================================================
  163. --- Communication/Sercos3Base/src/Driver/SERC_nrt.cpp   (revision 9632)
  164. +++ Communication/Sercos3Base/src/Driver/SERC_nrt.cpp   (working copy)
  165. @@ -673,7 +673,13 @@
  166.      *pNrt_fd = fd;
  167.  #if 0
  168.      pthread_attr_t attr;
  169. -    pthread_mutex_init(&mutex, 0);
  170. +    pthread_mutexattr_t muattr;
  171. +
  172. +    pthread_mutexattr_init(&muattr);
  173. +    pthread_mutexattr_setprotocol(&muattr, PTHREAD_PRIO_INHERIT);
  174. +    pthread_mutex_init(&mutex, &muattr);
  175. +    pthread_mutexattr_destroy(&muattr);
  176. +
  177.      pthread_cond_init(&nrt_cond, 0);
  178.  
  179.      pthread_attr_init(&attr);
  180. Index: Communication/Sercos3Base/src/Driver/igb.c
  181. ===================================================================
  182. --- Communication/Sercos3Base/src/Driver/igb.c  (revision 9632)
  183. +++ Communication/Sercos3Base/src/Driver/igb.c  (working copy)
  184. @@ -2545,6 +2545,10 @@
  185.          if (pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST) != 0)
  186.              goto err;
  187.  
  188. +       // to avoid priority inversion problems, for example in realtime code
  189. +        if (pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) != 0)
  190. +            goto err;
  191. +
  192.          if (pthread_mutex_init(adapter->memlock, &attr) != 0)
  193.              goto err;
  194.      }
  195. Index: Communication/libcifx/src/OS_Linux.c
  196. ===================================================================
  197. --- Communication/libcifx/src/OS_Linux.c        (revision 9632)
  198. +++ Communication/libcifx/src/OS_Linux.c        (working copy)
  199. @@ -529,16 +529,26 @@
  200.    if( pthread_mutexattr_init(&attr) != 0 )
  201.    {
  202.      perror("mutex_attr_init failed");
  203. -    goto err_out;
  204. +    goto err_out1;
  205.    }
  206. +
  207. +  if( pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) )
  208. +  {
  209. +    perror("Mutex setprotocol PTHREAD_PRIO_INHERIT failed");
  210. +    goto err_out2;
  211. +  }
  212. +
  213.    if( pthread_mutex_init(mut, &attr) != 0 )
  214.    {
  215.      perror("CreateMutex failed");
  216. -    goto err_out;
  217. +    goto err_out2;
  218.    }
  219. +  (void)pthread_mutexattr_destroy(&attr);
  220.    return (void*) mut;
  221.  
  222. -err_out:
  223. +err_out2:
  224. +  (void)pthread_mutexattr_destroy(&attr);
  225. +err_out1:
  226.    free(mut);
  227.    return NULL;
  228.  }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at