1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2009. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #if HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <stdarg.h> |
---|
15 | |
---|
16 | #include <errno.h> |
---|
17 | #include <fcntl.h> |
---|
18 | #include <pthread.h> |
---|
19 | #include <semaphore.h> |
---|
20 | #include <limits.h> |
---|
21 | #include <string.h> /* strlen */ |
---|
22 | |
---|
23 | #include <rtems/system.h> |
---|
24 | #include <rtems/score/object.h> |
---|
25 | #include <rtems/score/wkspace.h> |
---|
26 | #include <rtems/posix/semaphore.h> |
---|
27 | #include <rtems/posix/time.h> |
---|
28 | #include <rtems/seterr.h> |
---|
29 | |
---|
30 | /* |
---|
31 | * _POSIX_Semaphore_Create_support |
---|
32 | * |
---|
33 | * This routine does the actual creation and initialization of |
---|
34 | * a poxix semaphore. It is a support routine for sem_init and |
---|
35 | * sem_open. |
---|
36 | */ |
---|
37 | int _POSIX_Semaphore_Create_support( |
---|
38 | const char *name_arg, |
---|
39 | size_t name_len, |
---|
40 | int pshared, |
---|
41 | unsigned int value, |
---|
42 | POSIX_Semaphore_Control **the_sem |
---|
43 | ) |
---|
44 | { |
---|
45 | POSIX_Semaphore_Control *the_semaphore; |
---|
46 | CORE_semaphore_Attributes *the_sem_attr; |
---|
47 | char *name; |
---|
48 | |
---|
49 | /* Sharing semaphores among processes is not currently supported */ |
---|
50 | if (pshared != 0) |
---|
51 | rtems_set_errno_and_return_minus_one( ENOSYS ); |
---|
52 | |
---|
53 | _Thread_Disable_dispatch(); |
---|
54 | |
---|
55 | the_semaphore = _POSIX_Semaphore_Allocate(); |
---|
56 | if ( !the_semaphore ) { |
---|
57 | _Thread_Enable_dispatch(); |
---|
58 | rtems_set_errno_and_return_minus_one( ENOSPC ); |
---|
59 | } |
---|
60 | |
---|
61 | /* |
---|
62 | * Make a copy of the user's string for name just in case it was |
---|
63 | * dynamically constructed. |
---|
64 | */ |
---|
65 | if ( name_arg != NULL ) { |
---|
66 | name = _Workspace_String_duplicate( name_arg, name_len ); |
---|
67 | if ( !name ) { |
---|
68 | _POSIX_Semaphore_Free( the_semaphore ); |
---|
69 | _Thread_Enable_dispatch(); |
---|
70 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
71 | } |
---|
72 | } else { |
---|
73 | name = NULL; |
---|
74 | } |
---|
75 | |
---|
76 | the_semaphore->process_shared = pshared; |
---|
77 | |
---|
78 | if ( name ) { |
---|
79 | the_semaphore->named = true; |
---|
80 | the_semaphore->open_count = 1; |
---|
81 | the_semaphore->linked = true; |
---|
82 | } else { |
---|
83 | the_semaphore->named = false; |
---|
84 | the_semaphore->open_count = 0; |
---|
85 | the_semaphore->linked = false; |
---|
86 | } |
---|
87 | |
---|
88 | the_sem_attr = &the_semaphore->Semaphore.Attributes; |
---|
89 | |
---|
90 | /* |
---|
91 | * POSIX does not appear to specify what the discipline for |
---|
92 | * blocking tasks on this semaphore should be. It could somehow |
---|
93 | * be derived from the current scheduling policy. One |
---|
94 | * thing is certain, no matter what we decide, it won't be |
---|
95 | * the same as all other POSIX implementations. :) |
---|
96 | */ |
---|
97 | the_sem_attr->discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO; |
---|
98 | |
---|
99 | /* |
---|
100 | * This effectively disables limit checking. |
---|
101 | */ |
---|
102 | the_sem_attr->maximum_count = 0xFFFFFFFF; |
---|
103 | |
---|
104 | _CORE_semaphore_Initialize( &the_semaphore->Semaphore, the_sem_attr, value ); |
---|
105 | |
---|
106 | /* |
---|
107 | * Make the semaphore available for use. |
---|
108 | */ |
---|
109 | _Objects_Open_string( |
---|
110 | &_POSIX_Semaphore_Information, |
---|
111 | &the_semaphore->Object, |
---|
112 | name |
---|
113 | ); |
---|
114 | |
---|
115 | *the_sem = the_semaphore; |
---|
116 | |
---|
117 | _Thread_Enable_dispatch(); |
---|
118 | return 0; |
---|
119 | } |
---|