source: rtems/cpukit/posix/src/mutexget.c @ dfdad6ab

4.115
Last change on this file since dfdad6ab was dfdad6ab, checked in by Sebastian Huber <sebastian.huber@…>, on 10/08/14 at 08:09:14

posix: Fix mutex auto initialization

Use the once lock to prevent race conditions during auto initialization.

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/**
2 * @file
3 *
4 * @brief Convert POSIX Mutex ID to local object pointer
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2007.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/posix/muteximpl.h>
22#include <rtems/score/apimutex.h>
23
24static bool _POSIX_Mutex_Check_id_and_auto_init(
25  pthread_mutex_t   *mutex,
26  Objects_Locations *location
27)
28{
29  if ( mutex == NULL ) {
30    *location = OBJECTS_ERROR;
31
32    return false;
33  }
34
35  if ( *mutex == PTHREAD_MUTEX_INITIALIZER ) {
36    int eno;
37
38    _Once_Lock();
39
40    if ( *mutex == PTHREAD_MUTEX_INITIALIZER ) {
41      eno = pthread_mutex_init( mutex, NULL );
42    } else {
43      eno = 0;
44    }
45
46    _Once_Unlock();
47
48    if ( eno != 0 ) {
49      *location = OBJECTS_ERROR;
50
51      return false;
52    }
53  }
54
55  return true;
56}
57
58POSIX_Mutex_Control *_POSIX_Mutex_Get (
59  pthread_mutex_t   *mutex,
60  Objects_Locations *location
61)
62{
63  if ( !_POSIX_Mutex_Check_id_and_auto_init( mutex, location ) ) {
64    return NULL;
65  }
66
67  return (POSIX_Mutex_Control *)
68    _Objects_Get( &_POSIX_Mutex_Information, (Objects_Id) *mutex, location );
69}
70
71POSIX_Mutex_Control *_POSIX_Mutex_Get_interrupt_disable (
72  pthread_mutex_t   *mutex,
73  Objects_Locations *location,
74  ISR_Level         *level
75)
76{
77  if ( !_POSIX_Mutex_Check_id_and_auto_init( mutex, location ) ) {
78    return NULL;
79  }
80
81  return (POSIX_Mutex_Control *) _Objects_Get_isr_disable(
82    &_POSIX_Mutex_Information,
83    (Objects_Id) *mutex,
84    location,
85    level
86  );
87}
Note: See TracBrowser for help on using the repository browser.