[799c767] | 1 | /* |
---|
| 2 | * $Id$ |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | #include <stdarg.h> |
---|
| 6 | |
---|
| 7 | #include <errno.h> |
---|
| 8 | #include <fcntl.h> |
---|
| 9 | #include <pthread.h> |
---|
| 10 | #include <semaphore.h> |
---|
| 11 | #include <limits.h> |
---|
| 12 | |
---|
| 13 | #include <rtems/system.h> |
---|
| 14 | #include <rtems/score/object.h> |
---|
| 15 | #include <rtems/posix/semaphore.h> |
---|
| 16 | #include <rtems/posix/time.h> |
---|
| 17 | #include <rtems/posix/seterr.h> |
---|
| 18 | |
---|
| 19 | /*PAGE |
---|
| 20 | * |
---|
| 21 | * _POSIX_Semaphore_Create_support |
---|
[0ebf5694] | 22 | * |
---|
| 23 | * This routine does the actual creation and initialization of |
---|
| 24 | * a poxix semaphore. It is a support routine for sem_init and |
---|
| 25 | * sem_open. |
---|
[799c767] | 26 | */ |
---|
| 27 | |
---|
| 28 | int _POSIX_Semaphore_Create_support( |
---|
| 29 | const char *name, |
---|
| 30 | int pshared, |
---|
| 31 | unsigned int value, |
---|
| 32 | POSIX_Semaphore_Control **the_sem |
---|
| 33 | ) |
---|
| 34 | { |
---|
| 35 | POSIX_Semaphore_Control *the_semaphore; |
---|
| 36 | CORE_semaphore_Attributes *the_sem_attr; |
---|
| 37 | |
---|
| 38 | _Thread_Disable_dispatch(); |
---|
| 39 | |
---|
| 40 | /* Sharing semaphores among processes is not currently supported */ |
---|
| 41 | if (pshared != 0) { |
---|
| 42 | _Thread_Enable_dispatch(); |
---|
| 43 | set_errno_and_return_minus_one( ENOSYS ); |
---|
| 44 | } |
---|
| 45 | |
---|
[afebb3f] | 46 | if ( name ) { |
---|
| 47 | |
---|
| 48 | if( strlen(name) > PATH_MAX ) { /* XXX - Is strlen ok to use here ? */ |
---|
| 49 | _Thread_Enable_dispatch(); |
---|
| 50 | set_errno_and_return_minus_one( ENAMETOOLONG ); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /* |
---|
| 54 | * XXX Greater than NAME_MAX while POSIX_NO_TRUNC in effect. |
---|
| 55 | * XXX Error description in POSIX book different for mq_open and mq_unlink |
---|
| 56 | * Why??? |
---|
| 57 | */ |
---|
| 58 | } |
---|
| 59 | |
---|
[799c767] | 60 | the_semaphore = _POSIX_Semaphore_Allocate(); |
---|
| 61 | |
---|
| 62 | if ( !the_semaphore ) { |
---|
| 63 | _Thread_Enable_dispatch(); |
---|
| 64 | set_errno_and_return_minus_one( ENOSPC ); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 68 | if ( pshared == PTHREAD_PROCESS_SHARED && |
---|
| 69 | !( _Objects_MP_Allocate_and_open( &_POSIX_Semaphore_Information, 0, |
---|
| 70 | the_semaphore->Object.id, FALSE ) ) ) { |
---|
| 71 | _POSIX_Semaphore_Free( the_semaphore ); |
---|
| 72 | _Thread_Enable_dispatch(); |
---|
| 73 | set_errno_and_return_minus_one( EAGAIN ); |
---|
| 74 | } |
---|
| 75 | #endif |
---|
| 76 | |
---|
| 77 | the_semaphore->process_shared = pshared; |
---|
| 78 | |
---|
| 79 | if ( name ) { |
---|
| 80 | the_semaphore->named = TRUE; |
---|
| 81 | the_semaphore->open_count = 1; |
---|
| 82 | the_semaphore->linked = TRUE; |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | the_semaphore->named = FALSE; |
---|
| 86 | |
---|
| 87 | the_sem_attr = &the_semaphore->Semaphore.Attributes; |
---|
| 88 | |
---|
[0ebf5694] | 89 | /* |
---|
| 90 | * POSIX does not appear to specify what the discipline for |
---|
| 91 | * blocking tasks on this semaphore should be. It could somehow |
---|
| 92 | * be derived from the current scheduling policy. One |
---|
| 93 | * thing is certain, no matter what we decide, it won't be |
---|
| 94 | * the same as all other POSIX implementations. :) |
---|
[799c767] | 95 | */ |
---|
| 96 | |
---|
| 97 | the_sem_attr->discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO; |
---|
| 98 | |
---|
| 99 | /* |
---|
| 100 | * This effectively disables limit checking. |
---|
| 101 | */ |
---|
| 102 | |
---|
| 103 | the_sem_attr->maximum_count = 0xFFFFFFFF; |
---|
| 104 | |
---|
| 105 | _CORE_semaphore_Initialize( |
---|
| 106 | &the_semaphore->Semaphore, |
---|
| 107 | OBJECTS_POSIX_SEMAPHORES, |
---|
| 108 | the_sem_attr, |
---|
| 109 | value, |
---|
[0ebf5694] | 110 | NULL /* multiprocessing is not supported */ |
---|
[799c767] | 111 | ); |
---|
[0ebf5694] | 112 | |
---|
| 113 | /* |
---|
| 114 | * Make the semaphore available for use. |
---|
| 115 | */ |
---|
[799c767] | 116 | |
---|
| 117 | _Objects_Open( |
---|
| 118 | &_POSIX_Semaphore_Information, |
---|
| 119 | &the_semaphore->Object, |
---|
| 120 | (char *) name |
---|
| 121 | ); |
---|
| 122 | |
---|
| 123 | *the_sem = the_semaphore; |
---|
| 124 | |
---|
| 125 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 126 | if ( pshared == PTHREAD_PROCESS_SHARED ) |
---|
| 127 | _POSIX_Semaphore_MP_Send_process_packet( |
---|
| 128 | POSIX_SEMAPHORE_MP_ANNOUNCE_CREATE, |
---|
| 129 | the_semaphore->Object.id, |
---|
| 130 | (char *) name, |
---|
[0ebf5694] | 131 | 0 /* proxy id - Not used */ |
---|
[799c767] | 132 | ); |
---|
| 133 | #endif |
---|
| 134 | |
---|
| 135 | _Thread_Enable_dispatch(); |
---|
| 136 | return 0; |
---|
| 137 | } |
---|