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

Last change on this file since bbd6d27a was bbd6d27a, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 15, 2016 at 2:27:56 PM

score: Delete unused CORE_RWLock_Attributes

  • Property mode set to 100644
File size: 2.9 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  pthread_rwlockattr_t         default_attr;
101  const pthread_rwlockattr_t  *the_attr;
102
103  /*
104   *  Error check parameters
105   */
106  if ( !rwlock )
107    return EINVAL;
108
109  /*
110   * If the user passed in NULL, use the default attributes
111   */
112  if ( attr ) {
113    the_attr = attr;
114  } else {
115    (void) pthread_rwlockattr_init( &default_attr );
116    the_attr = &default_attr;
117  }
118
119  /*
120   * Now start error checking the attributes that we are going to use
121   */
122  if ( !the_attr->is_initialized )
123    return EINVAL;
124
125  switch ( the_attr->process_shared ) {
126    case PTHREAD_PROCESS_PRIVATE:    /* only supported values */
127      break;
128    case PTHREAD_PROCESS_SHARED:
129    default:
130      return EINVAL;
131  }
132
133  the_rwlock = _POSIX_RWLock_Allocate();
134
135  if ( !the_rwlock ) {
136    _Objects_Allocator_unlock();
137    return EAGAIN;
138  }
139
140  _CORE_RWLock_Initialize( &the_rwlock->RWLock );
141
142  _Objects_Open_u32(
143    &_POSIX_RWLock_Information,
144    &the_rwlock->Object,
145    0
146  );
147
148  *rwlock = the_rwlock->Object.id;
149
150  _Objects_Allocator_unlock();
151  return 0;
152}
Note: See TracBrowser for help on using the repository browser.