1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Function does Actual creation and Initialization of POSIX Semaphore |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2009. |
---|
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 <stdarg.h> |
---|
22 | |
---|
23 | #include <errno.h> |
---|
24 | #include <fcntl.h> |
---|
25 | #include <pthread.h> |
---|
26 | #include <semaphore.h> |
---|
27 | #include <limits.h> |
---|
28 | #include <string.h> /* strlen */ |
---|
29 | |
---|
30 | #include <rtems/system.h> |
---|
31 | #include <rtems/score/wkspace.h> |
---|
32 | #include <rtems/posix/semaphoreimpl.h> |
---|
33 | #include <rtems/seterr.h> |
---|
34 | |
---|
35 | /* |
---|
36 | * _POSIX_Semaphore_Create_support |
---|
37 | * |
---|
38 | * This routine does the actual creation and initialization of |
---|
39 | * a poxix semaphore. It is a support routine for sem_init and |
---|
40 | * sem_open. |
---|
41 | */ |
---|
42 | int _POSIX_Semaphore_Create_support( |
---|
43 | const char *name_arg, |
---|
44 | size_t name_len, |
---|
45 | int pshared, |
---|
46 | unsigned int value, |
---|
47 | POSIX_Semaphore_Control **the_sem |
---|
48 | ) |
---|
49 | { |
---|
50 | POSIX_Semaphore_Control *the_semaphore; |
---|
51 | char *name; |
---|
52 | |
---|
53 | /* Sharing semaphores among processes is not currently supported */ |
---|
54 | if (pshared != 0) |
---|
55 | rtems_set_errno_and_return_minus_one( ENOSYS ); |
---|
56 | |
---|
57 | the_semaphore = _POSIX_Semaphore_Allocate_unprotected(); |
---|
58 | if ( !the_semaphore ) { |
---|
59 | rtems_set_errno_and_return_minus_one( ENOSPC ); |
---|
60 | } |
---|
61 | |
---|
62 | /* |
---|
63 | * Make a copy of the user's string for name just in case it was |
---|
64 | * dynamically constructed. |
---|
65 | */ |
---|
66 | if ( name_arg != NULL ) { |
---|
67 | name = _Workspace_String_duplicate( name_arg, name_len ); |
---|
68 | if ( !name ) { |
---|
69 | _POSIX_Semaphore_Free( the_semaphore ); |
---|
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 | /* |
---|
89 | * POSIX does not appear to specify what the discipline for |
---|
90 | * blocking tasks on this semaphore should be. It could somehow |
---|
91 | * be derived from the current scheduling policy. One |
---|
92 | * thing is certain, no matter what we decide, it won't be |
---|
93 | * the same as all other POSIX implementations. :) |
---|
94 | */ |
---|
95 | _CORE_semaphore_Initialize( |
---|
96 | &the_semaphore->Semaphore, |
---|
97 | CORE_SEMAPHORE_DISCIPLINES_FIFO, |
---|
98 | 0xFFFFFFFF, |
---|
99 | value |
---|
100 | ); |
---|
101 | |
---|
102 | /* |
---|
103 | * Make the semaphore available for use. |
---|
104 | */ |
---|
105 | _Objects_Open_string( |
---|
106 | &_POSIX_Semaphore_Information, |
---|
107 | &the_semaphore->Object, |
---|
108 | name |
---|
109 | ); |
---|
110 | |
---|
111 | *the_sem = the_semaphore; |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|