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 17:13
raw | new post
modification of post by Anonymous (view diff)

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

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