1 | /* |
---|
2 | * POSIX Barrier Manager -- Initialize a Barrier Instance |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2006. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <pthread.h> |
---|
17 | #include <errno.h> |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/posix/barrier.h> |
---|
21 | |
---|
22 | /* |
---|
23 | * pthread_barrier_init |
---|
24 | * |
---|
25 | * This directive creates a barrier. A barrier id is returned. |
---|
26 | * |
---|
27 | * Input parameters: |
---|
28 | * barrier - pointer to barrier id |
---|
29 | * attr - barrier attributes |
---|
30 | * count - number of threads before automatic release |
---|
31 | * |
---|
32 | * Output parameters: |
---|
33 | * barrier - barrier id |
---|
34 | * 0 - if successful |
---|
35 | * error code - if unsuccessful |
---|
36 | */ |
---|
37 | |
---|
38 | int pthread_barrier_init( |
---|
39 | pthread_barrier_t *barrier, |
---|
40 | const pthread_barrierattr_t *attr, |
---|
41 | unsigned int count |
---|
42 | ) |
---|
43 | { |
---|
44 | POSIX_Barrier_Control *the_barrier; |
---|
45 | CORE_barrier_Attributes the_attributes; |
---|
46 | pthread_barrierattr_t my_attr; |
---|
47 | const pthread_barrierattr_t *the_attr; |
---|
48 | |
---|
49 | /* |
---|
50 | * Error check parameters |
---|
51 | */ |
---|
52 | if ( !barrier ) |
---|
53 | return EINVAL; |
---|
54 | |
---|
55 | if ( count == 0 ) |
---|
56 | return EINVAL; |
---|
57 | |
---|
58 | /* |
---|
59 | * If the user passed in NULL, use the default attributes |
---|
60 | */ |
---|
61 | if ( attr ) { |
---|
62 | the_attr = attr; |
---|
63 | } else { |
---|
64 | (void) pthread_barrierattr_init( &my_attr ); |
---|
65 | the_attr = &my_attr; |
---|
66 | } |
---|
67 | |
---|
68 | /* |
---|
69 | * Now start error checking the attributes that we are going to use |
---|
70 | */ |
---|
71 | if ( !the_attr->is_initialized ) |
---|
72 | return EINVAL; |
---|
73 | |
---|
74 | switch ( the_attr->process_shared ) { |
---|
75 | case PTHREAD_PROCESS_PRIVATE: /* only supported values */ |
---|
76 | break; |
---|
77 | case PTHREAD_PROCESS_SHARED: |
---|
78 | default: |
---|
79 | return EINVAL; |
---|
80 | } |
---|
81 | |
---|
82 | /* |
---|
83 | * Convert from POSIX attributes to Core Barrier attributes |
---|
84 | */ |
---|
85 | the_attributes.discipline = CORE_BARRIER_AUTOMATIC_RELEASE; |
---|
86 | the_attributes.maximum_count = count; |
---|
87 | |
---|
88 | /* |
---|
89 | * Enter dispatching critical section to allocate and initialize barrier |
---|
90 | */ |
---|
91 | _Thread_Disable_dispatch(); /* prevents deletion */ |
---|
92 | |
---|
93 | the_barrier = _POSIX_Barrier_Allocate(); |
---|
94 | |
---|
95 | if ( !the_barrier ) { |
---|
96 | _Thread_Enable_dispatch(); |
---|
97 | return EAGAIN; |
---|
98 | } |
---|
99 | |
---|
100 | _CORE_barrier_Initialize( &the_barrier->Barrier, &the_attributes ); |
---|
101 | |
---|
102 | _Objects_Open_u32( |
---|
103 | &_POSIX_Barrier_Information, |
---|
104 | &the_barrier->Object, |
---|
105 | 0 |
---|
106 | ); |
---|
107 | |
---|
108 | /* |
---|
109 | * Exit the critical section and return the user an operational barrier |
---|
110 | */ |
---|
111 | *barrier = the_barrier->Object.id; |
---|
112 | _Thread_Enable_dispatch(); |
---|
113 | return 0; |
---|
114 | } |
---|