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 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <stdarg.h> |
---|
17 | |
---|
18 | #include <errno.h> |
---|
19 | #include <fcntl.h> |
---|
20 | #include <pthread.h> |
---|
21 | #include <semaphore.h> |
---|
22 | #include <limits.h> |
---|
23 | #include <string.h> /* strlen */ |
---|
24 | |
---|
25 | #include <rtems/system.h> |
---|
26 | #include <rtems/score/object.h> |
---|
27 | #include <rtems/posix/semaphore.h> |
---|
28 | #include <rtems/posix/time.h> |
---|
29 | #include <rtems/seterr.h> |
---|
30 | |
---|
31 | /* pure ANSI mode does not have this prototype */ |
---|
32 | size_t strnlen(const char *, size_t); |
---|
33 | |
---|
34 | /* |
---|
35 | * _POSIX_Semaphore_Create_support |
---|
36 | * |
---|
37 | * This routine does the actual creation and initialization of |
---|
38 | * a poxix semaphore. It is a support routine for sem_init and |
---|
39 | * sem_open. |
---|
40 | */ |
---|
41 | int _POSIX_Semaphore_Create_support( |
---|
42 | const char *name, |
---|
43 | int pshared, |
---|
44 | unsigned int value, |
---|
45 | POSIX_Semaphore_Control **the_sem |
---|
46 | ) |
---|
47 | { |
---|
48 | POSIX_Semaphore_Control *the_semaphore; |
---|
49 | CORE_semaphore_Attributes *the_sem_attr; |
---|
50 | char *name_p = (char *)name; |
---|
51 | |
---|
52 | /* Sharing semaphores among processes is not currently supported */ |
---|
53 | if (pshared != 0) |
---|
54 | rtems_set_errno_and_return_minus_one( ENOSYS ); |
---|
55 | |
---|
56 | if ( name ) { |
---|
57 | if ( strnlen( name, NAME_MAX ) >= NAME_MAX ) |
---|
58 | rtems_set_errno_and_return_minus_one( ENAMETOOLONG ); |
---|
59 | } |
---|
60 | |
---|
61 | _Thread_Disable_dispatch(); |
---|
62 | |
---|
63 | the_semaphore = _POSIX_Semaphore_Allocate(); |
---|
64 | |
---|
65 | if ( !the_semaphore ) { |
---|
66 | _Thread_Enable_dispatch(); |
---|
67 | rtems_set_errno_and_return_minus_one( ENOSPC ); |
---|
68 | } |
---|
69 | |
---|
70 | the_semaphore->process_shared = pshared; |
---|
71 | |
---|
72 | if ( name ) { |
---|
73 | the_semaphore->named = true; |
---|
74 | the_semaphore->open_count = 1; |
---|
75 | the_semaphore->linked = true; |
---|
76 | } else { |
---|
77 | the_semaphore->named = false; |
---|
78 | the_semaphore->open_count = 0; |
---|
79 | the_semaphore->linked = false; |
---|
80 | } |
---|
81 | |
---|
82 | the_sem_attr = &the_semaphore->Semaphore.Attributes; |
---|
83 | |
---|
84 | /* |
---|
85 | * POSIX does not appear to specify what the discipline for |
---|
86 | * blocking tasks on this semaphore should be. It could somehow |
---|
87 | * be derived from the current scheduling policy. One |
---|
88 | * thing is certain, no matter what we decide, it won't be |
---|
89 | * the same as all other POSIX implementations. :) |
---|
90 | */ |
---|
91 | the_sem_attr->discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO; |
---|
92 | |
---|
93 | /* |
---|
94 | * This effectively disables limit checking. |
---|
95 | */ |
---|
96 | the_sem_attr->maximum_count = 0xFFFFFFFF; |
---|
97 | |
---|
98 | _CORE_semaphore_Initialize( &the_semaphore->Semaphore, the_sem_attr, value ); |
---|
99 | |
---|
100 | /* |
---|
101 | * Make the semaphore available for use. |
---|
102 | */ |
---|
103 | _Objects_Open_string( |
---|
104 | &_POSIX_Semaphore_Information, |
---|
105 | &the_semaphore->Object, |
---|
106 | name_p |
---|
107 | ); |
---|
108 | |
---|
109 | *the_sem = the_semaphore; |
---|
110 | |
---|
111 | _Thread_Enable_dispatch(); |
---|
112 | return 0; |
---|
113 | } |
---|