source: rtems/cpukit/posix/src/prwlockinit.c @ bdf6cc2

5
Last change on this file since bdf6cc2 was b9f95225, checked in by Sebastian Huber <sebastian.huber@…>, on 10/08/14 at 08:31:36

posix: Add auto initializaton for rwlock

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/**
2 * @file
3 *
4 * @brief Allocate resources to use the read-write lock and Initialize it
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  POSIX RWLock Manager -- Destroy a RWLock Instance
10 *
11 *  COPYRIGHT (c) 1989-2006.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <pthread.h>
24#include <errno.h>
25
26#include <rtems/posix/rwlockimpl.h>
27#include <rtems/score/apimutex.h>
28
29static bool _POSIX_RWLock_Check_id_and_auto_init(
30  pthread_mutex_t   *rwlock,
31  Objects_Locations *location
32)
33{
34  if ( rwlock == NULL ) {
35    *location = OBJECTS_ERROR;
36
37    return false;
38  }
39
40  if ( *rwlock == PTHREAD_RWLOCK_INITIALIZER ) {
41    int eno;
42
43    _Once_Lock();
44
45    if ( *rwlock == PTHREAD_RWLOCK_INITIALIZER ) {
46      eno = pthread_rwlock_init( rwlock, NULL );
47    } else {
48      eno = 0;
49    }
50
51    _Once_Unlock();
52
53    if ( eno != 0 ) {
54      *location = OBJECTS_ERROR;
55
56      return false;
57    }
58  }
59
60  return true;
61}
62
63POSIX_RWLock_Control *_POSIX_RWLock_Get(
64  pthread_rwlock_t  *rwlock,
65  Objects_Locations *location
66)
67{
68  if ( !_POSIX_RWLock_Check_id_and_auto_init( rwlock, location ) ) {
69    return NULL;
70  }
71
72  return (POSIX_RWLock_Control *) _Objects_Get(
73    &_POSIX_RWLock_Information,
74    *rwlock,
75    location
76  );
77}
78
79/*
80 *  pthread_rwlock_init
81 *
82 *  This directive creates a rwlock.  A rwlock id is returned.
83 *
84 *  Input parameters:
85 *    rwlock          - pointer to rwlock id
86 *    attr            - rwlock attributes
87 *
88 *  Output parameters:
89 *    rwlock     - rwlock id
90 *    0          - if successful
91 *    error code - if unsuccessful
92 */
93
94int pthread_rwlock_init(
95  pthread_rwlock_t           *rwlock,
96  const pthread_rwlockattr_t *attr
97)
98{
99  POSIX_RWLock_Control        *the_rwlock;
100  CORE_RWLock_Attributes       the_attributes;
101  pthread_rwlockattr_t         default_attr;
102  const pthread_rwlockattr_t  *the_attr;
103
104  /*
105   *  Error check parameters
106   */
107  if ( !rwlock )
108    return EINVAL;
109
110  /*
111   * If the user passed in NULL, use the default attributes
112   */
113  if ( attr ) {
114    the_attr = attr;
115  } else {
116    (void) pthread_rwlockattr_init( &default_attr );
117    the_attr = &default_attr;
118  }
119
120  /*
121   * Now start error checking the attributes that we are going to use
122   */
123  if ( !the_attr->is_initialized )
124    return EINVAL;
125
126  switch ( the_attr->process_shared ) {
127    case PTHREAD_PROCESS_PRIVATE:    /* only supported values */
128      break;
129    case PTHREAD_PROCESS_SHARED:
130    default:
131      return EINVAL;
132  }
133
134  /*
135   * Convert from POSIX attributes to Core RWLock attributes
136   *
137   * NOTE: Currently there are no core rwlock attributes
138   */
139  _CORE_RWLock_Initialize_attributes( &the_attributes );
140
141  the_rwlock = _POSIX_RWLock_Allocate();
142
143  if ( !the_rwlock ) {
144    _Objects_Allocator_unlock();
145    return EAGAIN;
146  }
147
148  _CORE_RWLock_Initialize( &the_rwlock->RWLock, &the_attributes );
149
150  _Objects_Open_u32(
151    &_POSIX_RWLock_Information,
152    &the_rwlock->Object,
153    0
154  );
155
156  *rwlock = the_rwlock->Object.id;
157
158  _Objects_Allocator_unlock();
159  return 0;
160}
Note: See TracBrowser for help on using the repository browser.