summaryrefslogtreecommitdiffstats
path: root/c/src/exec/posix
diff options
context:
space:
mode:
authorJoel Sherrill <joel.sherrill@OARcorp.com>1999-11-02 18:00:15 +0000
committerJoel Sherrill <joel.sherrill@OARcorp.com>1999-11-02 18:00:15 +0000
commit799c767d319d65c94b3770e82490c8dd4dc80378 (patch)
treebf9909c7f3ae617a34e401a0f88dc314f982f772 /c/src/exec/posix
parentSplit some more stuff out of psignal.c. (diff)
downloadrtems-799c767d319d65c94b3770e82490c8dd4dc80378.tar.bz2
Split the POSIX semaphore manager into multiple files.
Diffstat (limited to 'c/src/exec/posix')
-rw-r--r--c/src/exec/posix/include/rtems/posix/psignal.h6
-rw-r--r--c/src/exec/posix/include/rtems/posix/semaphore.h33
-rw-r--r--c/src/exec/posix/src/Makefile.in12
-rw-r--r--c/src/exec/posix/src/semaphore.c584
-rw-r--r--c/src/exec/posix/src/semaphorecreatesupp.c113
-rw-r--r--c/src/exec/posix/src/semaphoredeletesupp.c49
-rw-r--r--c/src/exec/posix/src/semaphoremp.c25
-rw-r--r--c/src/exec/posix/src/semaphorenametoid.c41
-rw-r--r--c/src/exec/posix/src/semaphorewaitsupp.c66
-rw-r--r--c/src/exec/posix/src/semdestroy.c82
-rw-r--r--c/src/exec/posix/src/semgetvalue.c46
-rw-r--r--c/src/exec/posix/src/seminit.c44
-rw-r--r--c/src/exec/posix/src/semopen.c109
-rw-r--r--c/src/exec/posix/src/sempost.c53
-rw-r--r--c/src/exec/posix/src/semtimedwait.c36
-rw-r--r--c/src/exec/posix/src/semtrywait.c31
-rw-r--r--c/src/exec/posix/src/semunlink.c65
-rw-r--r--c/src/exec/posix/src/semwait.c31
18 files changed, 838 insertions, 588 deletions
diff --git a/c/src/exec/posix/include/rtems/posix/psignal.h b/c/src/exec/posix/include/rtems/posix/psignal.h
index 1471a282bc..c15ca78d34 100644
--- a/c/src/exec/posix/include/rtems/posix/psignal.h
+++ b/c/src/exec/posix/include/rtems/posix/psignal.h
@@ -80,6 +80,12 @@ boolean _POSIX_signals_Unblock_thread(
siginfo_t *info
);
+boolean _POSIX_signals_Check_signal(
+ POSIX_API_Control *api,
+ int signo,
+ boolean is_global
+);
+
boolean _POSIX_signals_Clear_signals(
POSIX_API_Control *api,
int signo,
diff --git a/c/src/exec/posix/include/rtems/posix/semaphore.h b/c/src/exec/posix/include/rtems/posix/semaphore.h
index da6b65c642..af80893db4 100644
--- a/c/src/exec/posix/include/rtems/posix/semaphore.h
+++ b/c/src/exec/posix/include/rtems/posix/semaphore.h
@@ -21,6 +21,7 @@
extern "C" {
#endif
+#include <semaphore.h>
#include <rtems/score/coresem.h>
#define SEM_FAILED (sem_t *) -1
@@ -113,11 +114,41 @@ RTEMS_INLINE_ROUTINE boolean _POSIX_Semaphore_Is_null (
);
/*
+ * _POSIX_Semaphore_Create_support
+ *
+ * DESCRIPTION:
+ *
+ * This routine supports the sem_init and sem_open routines.
+ */
+
+int _POSIX_Semaphore_Create_support(
+ const char *name,
+ int pshared,
+ unsigned int value,
+ POSIX_Semaphore_Control **the_sem
+);
+
+/*
+ * _POSIX_Semaphore_Wait_support
+ *
+ * DESCRIPTION:
+ *
+ * This routine supports the sem_wait, sem_trywait, and sem_timedwait
+ * services.
+ */
+
+int _POSIX_Semaphore_Wait_support(
+ sem_t *sem,
+ boolean blocking,
+ Watchdog_Interval timeout
+);
+
+/*
* _POSIX_Semaphore_Name_to_id
*
* DESCRIPTION:
*
- * XXX
+ * This routine performs name to id translation.
*/
int _POSIX_Semaphore_Name_to_id(
diff --git a/c/src/exec/posix/src/Makefile.in b/c/src/exec/posix/src/Makefile.in
index c6d55c73e2..a27b1416e6 100644
--- a/c/src/exec/posix/src/Makefile.in
+++ b/c/src/exec/posix/src/Makefile.in
@@ -14,10 +14,10 @@ PROJECT_ROOT = @PROJECT_ROOT@
VPATH = @srcdir@
# following are semi-implemented and untested
-# C_PIECES=aio cancel devctl intr mqueue semaphore utsname
+# C_PIECES=aio cancel devctl intr utsname
# These are really in the stand but not really functional
-BUILD_FOR_NOW_C_PIECES = aio cancel mqueue semaphore utsname
+BUILD_FOR_NOW_C_PIECES = aio cancel utsname
ENOSYS_C_PIECES = execl execle execlp execv execve execvp fork pthreadatfork \
wait waitpid
@@ -53,9 +53,15 @@ PSIGNAL_C_PIECES = psignal alarm kill killinfo pause \
sigismember sigpending sigprocmask sigqueue sigsuspend sigtimedwait \
sigwait sigwaitinfo signal_2
+SEMAPHORE_C_PIECES= semaphore semaphorecreatesupp semaphoredeletesupp \
+ semaphoremp semaphorenametoid semaphorewaitsupp semdestroy \
+ semgetvalue seminit semopen sempost semtimedwait semtrywait \
+ semunlink semwait
+
C_PIECES = adasupp cond getpid key $(MESSAGE_QUEUE_PIECES) \
$(MUTEX_C_PIECES) $(PTHREAD_C_PIECES) \
- $(PSIGNAL_C_PIECES) ptimer sched time types unistd $(ENOSYS_C_PIECES) \
+ $(PSIGNAL_C_PIECES) ptimer sched $(SEMAPHORE_C_PIECES) \
+ time types unistd $(ENOSYS_C_PIECES) \
$(BUILD_FOR_NOW_C_PIECES)
C_FILES = $(C_PIECES:%=%.c)
diff --git a/c/src/exec/posix/src/semaphore.c b/c/src/exec/posix/src/semaphore.c
index 32c7ca4867..eb9664534a 100644
--- a/c/src/exec/posix/src/semaphore.c
+++ b/c/src/exec/posix/src/semaphore.c
@@ -43,587 +43,3 @@ void _POSIX_Semaphore_Manager_initialization(
FALSE
);
}
-
-/*PAGE
- *
- * _POSIX_Semaphore_Create_support
- */
-
-int _POSIX_Semaphore_Create_support(
- const char *name,
- int pshared,
- unsigned int value,
- POSIX_Semaphore_Control **the_sem
-)
-{
- POSIX_Semaphore_Control *the_semaphore;
- CORE_semaphore_Attributes *the_sem_attr;
-
- _Thread_Disable_dispatch();
-
- /* Sharing semaphores among processes is not currently supported */
- if (pshared != 0) {
- _Thread_Enable_dispatch();
- set_errno_and_return_minus_one( ENOSYS );
- }
-
- the_semaphore = _POSIX_Semaphore_Allocate();
-
- if ( !the_semaphore ) {
- _Thread_Enable_dispatch();
- set_errno_and_return_minus_one( ENOSPC );
- }
-
-#if defined(RTEMS_MULTIPROCESSING)
- if ( pshared == PTHREAD_PROCESS_SHARED &&
- !( _Objects_MP_Allocate_and_open( &_POSIX_Semaphore_Information, 0,
- the_semaphore->Object.id, FALSE ) ) ) {
- _POSIX_Semaphore_Free( the_semaphore );
- _Thread_Enable_dispatch();
- set_errno_and_return_minus_one( EAGAIN );
- }
-#endif
-
- the_semaphore->process_shared = pshared;
-
- if ( name ) {
- the_semaphore->named = TRUE;
- the_semaphore->open_count = 1;
- the_semaphore->linked = TRUE;
- }
- else
- the_semaphore->named = FALSE;
-
- the_sem_attr = &the_semaphore->Semaphore.Attributes;
-
- /* XXX
- *
- * Note should this be based on the current scheduling policy?
- */
-
- the_sem_attr->discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
-
- /*
- * This effectively disables limit checking.
- */
-
- the_sem_attr->maximum_count = 0xFFFFFFFF;
-
- _CORE_semaphore_Initialize(
- &the_semaphore->Semaphore,
- OBJECTS_POSIX_SEMAPHORES,
- the_sem_attr,
- value,
- 0 /* XXX - proxy_extract_callout is unused */
- );
-
- /* XXX - need Names to be a string!!! */
- _Objects_Open(
- &_POSIX_Semaphore_Information,
- &the_semaphore->Object,
- (char *) name
- );
-
- *the_sem = the_semaphore;
-
-#if defined(RTEMS_MULTIPROCESSING)
- if ( pshared == PTHREAD_PROCESS_SHARED )
- _POSIX_Semaphore_MP_Send_process_packet(
- POSIX_SEMAPHORE_MP_ANNOUNCE_CREATE,
- the_semaphore->Object.id,
- (char *) name,
- 0 /* proxy id - Not used */
- );
-#endif
-
- _Thread_Enable_dispatch();
- return 0;
-}
-
-
-/*PAGE
- *
- * 11.2.1 Initialize an Unnamed Semaphore, P1003.1b-1993, p.219
- */
-
-int sem_init(
- sem_t *sem,
- int pshared,
- unsigned int value
-)
-{
- int status;
- POSIX_Semaphore_Control *the_semaphore;
-
- status = _POSIX_Semaphore_Create_support(
- NULL,
- pshared,
- value,
- &the_semaphore
- );
-
- if ( status != -1 )
- *sem = the_semaphore->Object.id;
-
- return status;
-}
-
-/*PAGE
- *
- * 11.2.2 Destroy an Unnamed Semaphore, P1003.1b-1993, p.220
- */
-
-int sem_destroy(
- sem_t *sem
-)
-{
- register POSIX_Semaphore_Control *the_semaphore;
- Objects_Locations location;
-
- the_semaphore = _POSIX_Semaphore_Get( sem, &location );
- switch ( location ) {
- case OBJECTS_ERROR:
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_REMOTE:
- _Thread_Dispatch();
- return POSIX_MP_NOT_IMPLEMENTED();
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_LOCAL:
- /*
- * Undefined operation on a named semaphore.
- */
-
- if ( the_semaphore->named == TRUE ) {
- set_errno_and_return_minus_one( EINVAL );
- }
-
- _Objects_Close( &_POSIX_Semaphore_Information, &the_semaphore->Object );
-
- _CORE_semaphore_Flush(
- &the_semaphore->Semaphore,
-#if defined(RTEMS_MULTIPROCESSING)
- _POSIX_Semaphore_MP_Send_object_was_deleted,
-#else
- NULL,
-#endif
- -1 /* XXX should also seterrno -> EINVAL */
- );
-
- _POSIX_Semaphore_Free( the_semaphore );
-
-#if defined(RTEMS_MULTIPROCESSING)
- if ( the_semaphore->process_shared == PTHREAD_PROCESS_SHARED ) {
-
- _Objects_MP_Close(
- &_POSIX_Semaphore_Information,
- the_semaphore->Object.id
- );
-
- _POSIX_Semaphore_MP_Send_process_packet(
- POSIX_SEMAPHORE_MP_ANNOUNCE_DELETE,
- the_semaphore->Object.id,
- 0, /* Not used */
- 0 /* Not used */
- );
- }
-#endif
- _Thread_Enable_dispatch();
- return 0;
- }
- return POSIX_BOTTOM_REACHED();
-}
-
-/*PAGE
- *
- * 11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221
- *
- * NOTE: When oflag is O_CREAT, then optional third and fourth
- * parameters must be present.
- */
-
-sem_t *sem_open(
- const char *name,
- int oflag,
- ...
- /* mode_t mode, */
- /* unsigned int value */
-)
-{
- va_list arg;
- mode_t mode;
- unsigned int value = 0;
- int status;
- Objects_Id the_semaphore_id;
- POSIX_Semaphore_Control *the_semaphore;
- Objects_Locations location;
-
-
- if ( oflag & O_CREAT ) {
- va_start(arg, oflag);
- /*mode = (mode_t) va_arg( arg, mode_t * );*/
- mode = va_arg( arg, mode_t );
- /*value = (unsigned int) va_arg( arg, unsigned int * );*/
- value = va_arg( arg, unsigned int );
- va_end(arg);
- }
-
- status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id );
-
- /*
- * If the name to id translation worked, then the semaphore exists
- * and we can just return a pointer to the id. Otherwise we may
- * need to check to see if this is a "semaphore does not exist"
- * or some other miscellaneous error on the name.
- */
-
- if ( status ) {
-
- if ( status == EINVAL ) { /* name -> ID translation failed */
- if ( !(oflag & O_CREAT) ) { /* willing to create it? */
- set_errno_and_return_minus_one_cast( ENOENT, sem_t * );
- }
- /* we are willing to create it */
- }
- /* some type of error */
- /*set_errno_and_return_minus_one_cast( status, sem_t * );*/
-
- } else { /* name -> ID translation succeeded */
-
- if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
- set_errno_and_return_minus_one_cast( EEXIST, sem_t * );
- }
-
- /*
- * XXX In this case we need to do an ID->pointer conversion to
- * check the mode. This is probably a good place for a subroutine.
- */
-
- the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location );
- the_semaphore->open_count += 1;
-
- return (sem_t *)&the_semaphore->Object.id;
-
- }
-
- /* XXX verify this comment...
- *
- * At this point, the semaphore does not exist and everything has been
- * checked. We should go ahead and create a semaphore.
- */
-
- status = _POSIX_Semaphore_Create_support(
- name,
- FALSE, /* not shared across processes */
- value,
- &the_semaphore
- );
-
- if ( status == -1 )
- return (sem_t *) -1;
-
- return (sem_t *) &the_semaphore->Object.id;
-
-}
-
-/*PAGE
- *
- * _POSIX_Semaphore_Delete
- */
-
-void _POSIX_Semaphore_Delete(
- POSIX_Semaphore_Control *the_semaphore
-)
-{
- if ( !the_semaphore->linked && !the_semaphore->open_count ) {
- _POSIX_Semaphore_Free( the_semaphore );
-
-#if defined(RTEMS_MULTIPROCESSING)
- if ( the_semaphore->process_shared == PTHREAD_PROCESS_SHARED ) {
-
- _Objects_MP_Close(
- &_POSIX_Semaphore_Information,
- the_semaphore->Object.id
- );
-
- _POSIX_Semaphore_MP_Send_process_packet(
- POSIX_SEMAPHORE_MP_ANNOUNCE_DELETE,
- the_semaphore->Object.id,
- 0, /* Not used */
- 0 /* Not used */
- );
- }
-#endif
-
- }
-}
-
-/*PAGE
- *
- * 11.2.4 Close a Named Semaphore, P1003.1b-1993, p.224
- */
-
-int sem_close(
- sem_t *sem
-)
-{
- register POSIX_Semaphore_Control *the_semaphore;
- Objects_Locations location;
-
- the_semaphore = _POSIX_Semaphore_Get( sem, &location );
- switch ( location ) {
- case OBJECTS_ERROR:
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_REMOTE:
- _Thread_Dispatch();
- return POSIX_MP_NOT_IMPLEMENTED();
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_LOCAL:
- the_semaphore->open_count -= 1;
- _POSIX_Semaphore_Delete( the_semaphore );
- _Thread_Enable_dispatch();
- return 0;
- }
- return POSIX_BOTTOM_REACHED();
-}
-
-/*PAGE
- *
- * 11.2.5 Remove a Named Semaphore, P1003.1b-1993, p.225
- */
-
-int sem_unlink(
- const char *name
-)
-{
- int status;
- register POSIX_Semaphore_Control *the_semaphore;
- Objects_Id the_semaphore_id;
- Objects_Locations location;
-
- status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id );
-
- if ( !status )
- set_errno_and_return_minus_one( status );
-
- the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location );
- switch ( location ) {
- case OBJECTS_ERROR:
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_REMOTE:
- _Thread_Dispatch();
- return POSIX_MP_NOT_IMPLEMENTED();
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_LOCAL:
-
-#if defined(RTEMS_MULTIPROCESSING)
- if ( the_semaphore->process_shared == PTHREAD_PROCESS_SHARED ) {
- _Objects_MP_Close(
- &_POSIX_Semaphore_Information,
- the_semaphore->Object.id
- );
- }
-#endif
-
- the_semaphore->linked = FALSE;
-
- _POSIX_Semaphore_Delete( the_semaphore );
-
- _Thread_Enable_dispatch();
- return 0;
- }
- return POSIX_BOTTOM_REACHED();
-}
-
-/*PAGE
- *
- * _POSIX_Semaphore_Wait_support
- */
-
-int _POSIX_Semaphore_Wait_support(
- sem_t *sem,
- boolean blocking,
- Watchdog_Interval timeout
-)
-{
- register POSIX_Semaphore_Control *the_semaphore;
- Objects_Locations location;
- int code;
-
- the_semaphore = _POSIX_Semaphore_Get( sem, &location );
- switch ( location ) {
- case OBJECTS_ERROR:
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_REMOTE:
- _Thread_Dispatch();
- return POSIX_MP_NOT_IMPLEMENTED();
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_LOCAL:
- _CORE_semaphore_Seize(
- &the_semaphore->Semaphore,
- the_semaphore->Object.id,
- blocking,
- timeout
- );
- _Thread_Enable_dispatch();
- code = _Thread_Executing->Wait.return_code;
- switch (_Thread_Executing->Wait.return_code) {
- case 1:
- errno = EAGAIN;
- code = -1;
- break;
- case 3:
- errno = ETIMEDOUT;
- code = -1;
- break;
- }
-
- /*return _Thread_Executing->Wait.return_code;*/
- return code;
- }
- return POSIX_BOTTOM_REACHED();
-}
-
-/*PAGE
- *
- * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226
- *
- * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27
- */
-
-int sem_wait(
- sem_t *sem
-)
-{
- return _POSIX_Semaphore_Wait_support( sem, TRUE, THREAD_QUEUE_WAIT_FOREVER );
-}
-
-/*PAGE
- *
- * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226
- *
- * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27
- */
-
-int sem_trywait(
- sem_t *sem
-)
-{
- return _POSIX_Semaphore_Wait_support( sem, FALSE, THREAD_QUEUE_WAIT_FOREVER );
-}
-
-/*PAGE
- *
- * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226
- *
- * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27
- */
-
-int sem_timedwait(
- sem_t *sem,
- const struct timespec *timeout
-)
-{
- return _POSIX_Semaphore_Wait_support(
- sem,
- TRUE,
- _POSIX_Timespec_to_interval( timeout )
- );
-}
-
-/*PAGE
- *
- * 11.2.7 Unlock a Semaphore, P1003.1b-1993, p.227
- */
-
-void POSIX_Semaphore_MP_support(
- Thread_Control *the_thread,
- Objects_Id id
-)
-{
- (void) POSIX_MP_NOT_IMPLEMENTED();
-}
-
-
-int sem_post(
- sem_t *sem
-)
-{
- register POSIX_Semaphore_Control *the_semaphore;
- Objects_Locations location;
-
- the_semaphore = _POSIX_Semaphore_Get( sem, &location );
- switch ( location ) {
- case OBJECTS_ERROR:
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_REMOTE:
- _Thread_Dispatch();
- return POSIX_MP_NOT_IMPLEMENTED();
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_LOCAL:
- _CORE_semaphore_Surrender(
- &the_semaphore->Semaphore,
- the_semaphore->Object.id,
-#if defined(RTEMS_MULTIPROCESSING)
- POSIX_Semaphore_MP_support
-#else
- NULL
-#endif
- );
- _Thread_Enable_dispatch();
- return 0;
- }
- return POSIX_BOTTOM_REACHED();
-}
-
-/*PAGE
- *
- * 11.2.8 Get the Value of a Semaphore, P1003.1b-1993, p.229
- */
-
-int sem_getvalue(
- sem_t *sem,
- int *sval
-)
-{
- register POSIX_Semaphore_Control *the_semaphore;
- Objects_Locations location;
-
- the_semaphore = _POSIX_Semaphore_Get( sem, &location );
- switch ( location ) {
- case OBJECTS_ERROR:
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_REMOTE:
- _Thread_Dispatch();
- return POSIX_MP_NOT_IMPLEMENTED();
- set_errno_and_return_minus_one( EINVAL );
- case OBJECTS_LOCAL:
- *sval = _CORE_semaphore_Get_count( &the_semaphore->Semaphore );
- _Thread_Enable_dispatch();
- return 0;
- }
- return POSIX_BOTTOM_REACHED();
-}
-
-/*PAGE
- *
- * _POSIX_Semaphore_Name_to_id
- *
- * XXX
- */
-
-int _POSIX_Semaphore_Name_to_id(
- const char *name,
- Objects_Id *id
-)
-{
- Objects_Name_to_id_errors status;
-
- status = _Objects_Name_to_id( &_POSIX_Semaphore_Information, (char *)name, 0, id );
-
- if ( status == OBJECTS_SUCCESSFUL ) {
- return 0;
- } else {
- return EINVAL;
- }
-}
-
diff --git a/c/src/exec/posix/src/semaphorecreatesupp.c b/c/src/exec/posix/src/semaphorecreatesupp.c
new file mode 100644
index 0000000000..caf1c60c62
--- /dev/null
+++ b/c/src/exec/posix/src/semaphorecreatesupp.c
@@ -0,0 +1,113 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * _POSIX_Semaphore_Create_support
+ */
+
+int _POSIX_Semaphore_Create_support(
+ const char *name,
+ int pshared,
+ unsigned int value,
+ POSIX_Semaphore_Control **the_sem
+)
+{
+ POSIX_Semaphore_Control *the_semaphore;
+ CORE_semaphore_Attributes *the_sem_attr;
+
+ _Thread_Disable_dispatch();
+
+ /* Sharing semaphores among processes is not currently supported */
+ if (pshared != 0) {
+ _Thread_Enable_dispatch();
+ set_errno_and_return_minus_one( ENOSYS );
+ }
+
+ the_semaphore = _POSIX_Semaphore_Allocate();
+
+ if ( !the_semaphore ) {
+ _Thread_Enable_dispatch();
+ set_errno_and_return_minus_one( ENOSPC );
+ }
+
+#if defined(RTEMS_MULTIPROCESSING)
+ if ( pshared == PTHREAD_PROCESS_SHARED &&
+ !( _Objects_MP_Allocate_and_open( &_POSIX_Semaphore_Information, 0,
+ the_semaphore->Object.id, FALSE ) ) ) {
+ _POSIX_Semaphore_Free( the_semaphore );
+ _Thread_Enable_dispatch();
+ set_errno_and_return_minus_one( EAGAIN );
+ }
+#endif
+
+ the_semaphore->process_shared = pshared;
+
+ if ( name ) {
+ the_semaphore->named = TRUE;
+ the_semaphore->open_count = 1;
+ the_semaphore->linked = TRUE;
+ }
+ else
+ the_semaphore->named = FALSE;
+
+ the_sem_attr = &the_semaphore->Semaphore.Attributes;
+
+ /* XXX
+ *
+ * Note should this be based on the current scheduling policy?
+ */
+
+ the_sem_attr->discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;
+
+ /*
+ * This effectively disables limit checking.
+ */
+
+ the_sem_attr->maximum_count = 0xFFFFFFFF;
+
+ _CORE_semaphore_Initialize(
+ &the_semaphore->Semaphore,
+ OBJECTS_POSIX_SEMAPHORES,
+ the_sem_attr,
+ value,
+ 0 /* XXX - proxy_extract_callout is unused */
+ );
+
+ /* XXX - need Names to be a string!!! */
+ _Objects_Open(
+ &_POSIX_Semaphore_Information,
+ &the_semaphore->Object,
+ (char *) name
+ );
+
+ *the_sem = the_semaphore;
+
+#if defined(RTEMS_MULTIPROCESSING)
+ if ( pshared == PTHREAD_PROCESS_SHARED )
+ _POSIX_Semaphore_MP_Send_process_packet(
+ POSIX_SEMAPHORE_MP_ANNOUNCE_CREATE,
+ the_semaphore->Object.id,
+ (char *) name,
+ 0 /* proxy id - Not used */
+ );
+#endif
+
+ _Thread_Enable_dispatch();
+ return 0;
+}
diff --git a/c/src/exec/posix/src/semaphoredeletesupp.c b/c/src/exec/posix/src/semaphoredeletesupp.c
new file mode 100644
index 0000000000..04f0d1ad93
--- /dev/null
+++ b/c/src/exec/posix/src/semaphoredeletesupp.c
@@ -0,0 +1,49 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * _POSIX_Semaphore_Delete
+ */
+
+void _POSIX_Semaphore_Delete(
+ POSIX_Semaphore_Control *the_semaphore
+)
+{
+ if ( !the_semaphore->linked && !the_semaphore->open_count ) {
+ _POSIX_Semaphore_Free( the_semaphore );
+
+#if defined(RTEMS_MULTIPROCESSING)
+ if ( the_semaphore->process_shared == PTHREAD_PROCESS_SHARED ) {
+
+ _Objects_MP_Close(
+ &_POSIX_Semaphore_Information,
+ the_semaphore->Object.id
+ );
+
+ _POSIX_Semaphore_MP_Send_process_packet(
+ POSIX_SEMAPHORE_MP_ANNOUNCE_DELETE,
+ the_semaphore->Object.id,
+ 0, /* Not used */
+ 0 /* Not used */
+ );
+ }
+#endif
+
+ }
+}
diff --git a/c/src/exec/posix/src/semaphoremp.c b/c/src/exec/posix/src/semaphoremp.c
new file mode 100644
index 0000000000..ef83a4ee6c
--- /dev/null
+++ b/c/src/exec/posix/src/semaphoremp.c
@@ -0,0 +1,25 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+void POSIX_Semaphore_MP_support(
+ Thread_Control *the_thread,
+ Objects_Id id
+)
+{
+ (void) POSIX_MP_NOT_IMPLEMENTED();
+}
diff --git a/c/src/exec/posix/src/semaphorenametoid.c b/c/src/exec/posix/src/semaphorenametoid.c
new file mode 100644
index 0000000000..8761f79f8b
--- /dev/null
+++ b/c/src/exec/posix/src/semaphorenametoid.c
@@ -0,0 +1,41 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * _POSIX_Semaphore_Name_to_id
+ *
+ * XXX
+ */
+
+int _POSIX_Semaphore_Name_to_id(
+ const char *name,
+ Objects_Id *id
+)
+{
+ Objects_Name_to_id_errors status;
+
+ status = _Objects_Name_to_id( &_POSIX_Semaphore_Information, (char *)name, 0, id );
+
+ if ( status == OBJECTS_SUCCESSFUL ) {
+ return 0;
+ } else {
+ return EINVAL;
+ }
+}
+
diff --git a/c/src/exec/posix/src/semaphorewaitsupp.c b/c/src/exec/posix/src/semaphorewaitsupp.c
new file mode 100644
index 0000000000..1162b71c49
--- /dev/null
+++ b/c/src/exec/posix/src/semaphorewaitsupp.c
@@ -0,0 +1,66 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * _POSIX_Semaphore_Wait_support
+ */
+
+int _POSIX_Semaphore_Wait_support(
+ sem_t *sem,
+ boolean blocking,
+ Watchdog_Interval timeout
+)
+{
+ register POSIX_Semaphore_Control *the_semaphore;
+ Objects_Locations location;
+ int code;
+
+ the_semaphore = _POSIX_Semaphore_Get( sem, &location );
+ switch ( location ) {
+ case OBJECTS_ERROR:
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_REMOTE:
+ _Thread_Dispatch();
+ return POSIX_MP_NOT_IMPLEMENTED();
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_LOCAL:
+ _CORE_semaphore_Seize(
+ &the_semaphore->Semaphore,
+ the_semaphore->Object.id,
+ blocking,
+ timeout
+ );
+ _Thread_Enable_dispatch();
+ code = _Thread_Executing->Wait.return_code;
+ switch (_Thread_Executing->Wait.return_code) {
+ case 1:
+ errno = EAGAIN;
+ code = -1;
+ break;
+ case 3:
+ errno = ETIMEDOUT;
+ code = -1;
+ break;
+ }
+
+ /*return _Thread_Executing->Wait.return_code;*/
+ return code;
+ }
+ return POSIX_BOTTOM_REACHED();
+}
diff --git a/c/src/exec/posix/src/semdestroy.c b/c/src/exec/posix/src/semdestroy.c
new file mode 100644
index 0000000000..fc73a93ada
--- /dev/null
+++ b/c/src/exec/posix/src/semdestroy.c
@@ -0,0 +1,82 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.2 Destroy an Unnamed Semaphore, P1003.1b-1993, p.220
+ */
+
+int sem_destroy(
+ sem_t *sem
+)
+{
+ register POSIX_Semaphore_Control *the_semaphore;
+ Objects_Locations location;
+
+ the_semaphore = _POSIX_Semaphore_Get( sem, &location );
+ switch ( location ) {
+ case OBJECTS_ERROR:
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_REMOTE:
+ _Thread_Dispatch();
+ return POSIX_MP_NOT_IMPLEMENTED();
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_LOCAL:
+ /*
+ * Undefined operation on a named semaphore.
+ */
+
+ if ( the_semaphore->named == TRUE ) {
+ set_errno_and_return_minus_one( EINVAL );
+ }
+
+ _Objects_Close( &_POSIX_Semaphore_Information, &the_semaphore->Object );
+
+ _CORE_semaphore_Flush(
+ &the_semaphore->Semaphore,
+#if defined(RTEMS_MULTIPROCESSING)
+ _POSIX_Semaphore_MP_Send_object_was_deleted,
+#else
+ NULL,
+#endif
+ -1 /* XXX should also seterrno -> EINVAL */
+ );
+
+ _POSIX_Semaphore_Free( the_semaphore );
+
+#if defined(RTEMS_MULTIPROCESSING)
+ if ( the_semaphore->process_shared == PTHREAD_PROCESS_SHARED ) {
+
+ _Objects_MP_Close(
+ &_POSIX_Semaphore_Information,
+ the_semaphore->Object.id
+ );
+
+ _POSIX_Semaphore_MP_Send_process_packet(
+ POSIX_SEMAPHORE_MP_ANNOUNCE_DELETE,
+ the_semaphore->Object.id,
+ 0, /* Not used */
+ 0 /* Not used */
+ );
+ }
+#endif
+ _Thread_Enable_dispatch();
+ return 0;
+ }
+ return POSIX_BOTTOM_REACHED();
+}
diff --git a/c/src/exec/posix/src/semgetvalue.c b/c/src/exec/posix/src/semgetvalue.c
new file mode 100644
index 0000000000..25d8c8d8d4
--- /dev/null
+++ b/c/src/exec/posix/src/semgetvalue.c
@@ -0,0 +1,46 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.8 Get the Value of a Semaphore, P1003.1b-1993, p.229
+ */
+
+int sem_getvalue(
+ sem_t *sem,
+ int *sval
+)
+{
+ register POSIX_Semaphore_Control *the_semaphore;
+ Objects_Locations location;
+
+ the_semaphore = _POSIX_Semaphore_Get( sem, &location );
+ switch ( location ) {
+ case OBJECTS_ERROR:
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_REMOTE:
+ _Thread_Dispatch();
+ return POSIX_MP_NOT_IMPLEMENTED();
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_LOCAL:
+ *sval = _CORE_semaphore_Get_count( &the_semaphore->Semaphore );
+ _Thread_Enable_dispatch();
+ return 0;
+ }
+ return POSIX_BOTTOM_REACHED();
+}
diff --git a/c/src/exec/posix/src/seminit.c b/c/src/exec/posix/src/seminit.c
new file mode 100644
index 0000000000..a2cf4ba6b9
--- /dev/null
+++ b/c/src/exec/posix/src/seminit.c
@@ -0,0 +1,44 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.1 Initialize an Unnamed Semaphore, P1003.1b-1993, p.219
+ */
+
+int sem_init(
+ sem_t *sem,
+ int pshared,
+ unsigned int value
+)
+{
+ int status;
+ POSIX_Semaphore_Control *the_semaphore;
+
+ status = _POSIX_Semaphore_Create_support(
+ NULL,
+ pshared,
+ value,
+ &the_semaphore
+ );
+
+ if ( status != -1 )
+ *sem = the_semaphore->Object.id;
+
+ return status;
+}
diff --git a/c/src/exec/posix/src/semopen.c b/c/src/exec/posix/src/semopen.c
new file mode 100644
index 0000000000..c0ac7ffe3b
--- /dev/null
+++ b/c/src/exec/posix/src/semopen.c
@@ -0,0 +1,109 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221
+ *
+ * NOTE: When oflag is O_CREAT, then optional third and fourth
+ * parameters must be present.
+ */
+
+sem_t *sem_open(
+ const char *name,
+ int oflag,
+ ...
+ /* mode_t mode, */
+ /* unsigned int value */
+)
+{
+ va_list arg;
+ mode_t mode;
+ unsigned int value = 0;
+ int status;
+ Objects_Id the_semaphore_id;
+ POSIX_Semaphore_Control *the_semaphore;
+ Objects_Locations location;
+
+
+ if ( oflag & O_CREAT ) {
+ va_start(arg, oflag);
+ /*mode = (mode_t) va_arg( arg, mode_t * );*/
+ mode = va_arg( arg, mode_t );
+ /*value = (unsigned int) va_arg( arg, unsigned int * );*/
+ value = va_arg( arg, unsigned int );
+ va_end(arg);
+ }
+
+ status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id );
+
+ /*
+ * If the name to id translation worked, then the semaphore exists
+ * and we can just return a pointer to the id. Otherwise we may
+ * need to check to see if this is a "semaphore does not exist"
+ * or some other miscellaneous error on the name.
+ */
+
+ if ( status ) {
+
+ if ( status == EINVAL ) { /* name -> ID translation failed */
+ if ( !(oflag & O_CREAT) ) { /* willing to create it? */
+ set_errno_and_return_minus_one_cast( ENOENT, sem_t * );
+ }
+ /* we are willing to create it */
+ }
+ /* some type of error */
+ /*set_errno_and_return_minus_one_cast( status, sem_t * );*/
+
+ } else { /* name -> ID translation succeeded */
+
+ if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
+ set_errno_and_return_minus_one_cast( EEXIST, sem_t * );
+ }
+
+ /*
+ * XXX In this case we need to do an ID->pointer conversion to
+ * check the mode. This is probably a good place for a subroutine.
+ */
+
+ the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location );
+ the_semaphore->open_count += 1;
+
+ return (sem_t *)&the_semaphore->Object.id;
+
+ }
+
+ /* XXX verify this comment...
+ *
+ * At this point, the semaphore does not exist and everything has been
+ * checked. We should go ahead and create a semaphore.
+ */
+
+ status = _POSIX_Semaphore_Create_support(
+ name,
+ FALSE, /* not shared across processes */
+ value,
+ &the_semaphore
+ );
+
+ if ( status == -1 )
+ return (sem_t *) -1;
+
+ return (sem_t *) &the_semaphore->Object.id;
+
+}
diff --git a/c/src/exec/posix/src/sempost.c b/c/src/exec/posix/src/sempost.c
new file mode 100644
index 0000000000..d3375ce80d
--- /dev/null
+++ b/c/src/exec/posix/src/sempost.c
@@ -0,0 +1,53 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.7 Unlock a Semaphore, P1003.1b-1993, p.227
+ */
+
+int sem_post(
+ sem_t *sem
+)
+{
+ register POSIX_Semaphore_Control *the_semaphore;
+ Objects_Locations location;
+
+ the_semaphore = _POSIX_Semaphore_Get( sem, &location );
+ switch ( location ) {
+ case OBJECTS_ERROR:
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_REMOTE:
+ _Thread_Dispatch();
+ return POSIX_MP_NOT_IMPLEMENTED();
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_LOCAL:
+ _CORE_semaphore_Surrender(
+ &the_semaphore->Semaphore,
+ the_semaphore->Object.id,
+#if defined(RTEMS_MULTIPROCESSING)
+ POSIX_Semaphore_MP_support
+#else
+ NULL
+#endif
+ );
+ _Thread_Enable_dispatch();
+ return 0;
+ }
+ return POSIX_BOTTOM_REACHED();
+}
diff --git a/c/src/exec/posix/src/semtimedwait.c b/c/src/exec/posix/src/semtimedwait.c
new file mode 100644
index 0000000000..e237749ff7
--- /dev/null
+++ b/c/src/exec/posix/src/semtimedwait.c
@@ -0,0 +1,36 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226
+ *
+ * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27
+ */
+
+int sem_timedwait(
+ sem_t *sem,
+ const struct timespec *timeout
+)
+{
+ return _POSIX_Semaphore_Wait_support(
+ sem,
+ TRUE,
+ _POSIX_Timespec_to_interval( timeout )
+ );
+}
diff --git a/c/src/exec/posix/src/semtrywait.c b/c/src/exec/posix/src/semtrywait.c
new file mode 100644
index 0000000000..c29a2f3dde
--- /dev/null
+++ b/c/src/exec/posix/src/semtrywait.c
@@ -0,0 +1,31 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226
+ *
+ * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27
+ */
+
+int sem_trywait(
+ sem_t *sem
+)
+{
+ return _POSIX_Semaphore_Wait_support( sem, FALSE, THREAD_QUEUE_WAIT_FOREVER );
+}
diff --git a/c/src/exec/posix/src/semunlink.c b/c/src/exec/posix/src/semunlink.c
new file mode 100644
index 0000000000..7fc2c56a28
--- /dev/null
+++ b/c/src/exec/posix/src/semunlink.c
@@ -0,0 +1,65 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.5 Remove a Named Semaphore, P1003.1b-1993, p.225
+ */
+
+int sem_unlink(
+ const char *name
+)
+{
+ int status;
+ register POSIX_Semaphore_Control *the_semaphore;
+ Objects_Id the_semaphore_id;
+ Objects_Locations location;
+
+ status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id );
+
+ if ( !status )
+ set_errno_and_return_minus_one( status );
+
+ the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location );
+ switch ( location ) {
+ case OBJECTS_ERROR:
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_REMOTE:
+ _Thread_Dispatch();
+ return POSIX_MP_NOT_IMPLEMENTED();
+ set_errno_and_return_minus_one( EINVAL );
+ case OBJECTS_LOCAL:
+
+#if defined(RTEMS_MULTIPROCESSING)
+ if ( the_semaphore->process_shared == PTHREAD_PROCESS_SHARED ) {
+ _Objects_MP_Close(
+ &_POSIX_Semaphore_Information,
+ the_semaphore->Object.id
+ );
+ }
+#endif
+
+ the_semaphore->linked = FALSE;
+
+ _POSIX_Semaphore_Delete( the_semaphore );
+
+ _Thread_Enable_dispatch();
+ return 0;
+ }
+ return POSIX_BOTTOM_REACHED();
+}
diff --git a/c/src/exec/posix/src/semwait.c b/c/src/exec/posix/src/semwait.c
new file mode 100644
index 0000000000..f46db81852
--- /dev/null
+++ b/c/src/exec/posix/src/semwait.c
@@ -0,0 +1,31 @@
+/*
+ * $Id$
+ */
+
+#include <stdarg.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <limits.h>
+
+#include <rtems/system.h>
+#include <rtems/score/object.h>
+#include <rtems/posix/semaphore.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/seterr.h>
+
+/*PAGE
+ *
+ * 11.2.6 Lock a Semaphore, P1003.1b-1993, p.226
+ *
+ * NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27
+ */
+
+int sem_wait(
+ sem_t *sem
+)
+{
+ return _POSIX_Semaphore_Wait_support( sem, TRUE, THREAD_QUEUE_WAIT_FOREVER );
+}