* cygwin.din: Add sem_close, sem_getvalue, sem_open and sem_timedwait.

* pthread.cc (+mangle_sem_name): New function.
	(sem_open): Ditto.
	(sem_close: Ditto.
	(sem_timedwait): Ditto.
	(sem_getvalue): Ditto.
	* thread.cc (semaphore::semaphore): Rearrange member initialization.
	Use appropriate security attribute for process shared semaphores.
	(semaphore::semaphore): New constructor for named semaphores.
	(semaphore::~semaphore): Care for semaphore name.
	(semaphore::_post): Accomodate failing ReleaseSemaphore. Use value
	returned by ReleaseSemaphore vor currentvalue.
	(semaphore::_getvalue): New method.
	(semaphore::_timedwait): Ditto.
	(semaphore::_fixup_after_fork): Rearrange. Don't fail for process
	shared semaphores.
	(semaphore::open): New method.
	(semaphore::timedwait): Ditto.
	(semaphore::post): Fix return value.  Set errno appropriately.
	(semaphore::getvalue): New method.
	* thread.h (class semaphore): Add prototypes for open, getvalue,
	timedwait, _getvalue, _timedwait.  Add prototypes for new constructor.
	Add name member.
	* include/semaphore.h: Add prototypes for sem_open, sem_close,
	sem_timedwait and sem_getvalue.
	include/cygwin/version.h: Bump API minor number.
This commit is contained in:
Corinna Vinschen
2003-10-27 11:48:29 +00:00
parent d845665e0f
commit 072339664d
7 changed files with 297 additions and 19 deletions

View File

@ -12,7 +12,9 @@
#include "winsup.h"
#include "thread.h"
#include "errno.h"
#include "cygerrno.h"
#include <stdarg.h>
#include <sys/fcntl.h>
extern "C"
{
@ -157,6 +159,55 @@ sem_destroy (sem_t * sem)
return semaphore::destroy (sem);
}
/* Mangle semaphore name to follow windows naming rules. Prepend "Global\"
if running on terminal service aware machine. Substitute invalid backslash
by forward slash characters, hoping not to collide. */
static bool
mangle_sem_name (char *mangled, const char *name)
{
if (check_null_empty_str_errno (name))
return false;
int len = strlen (name);
if (len > MAX_PATH
|| (wincap.has_terminal_services () && len > MAX_PATH - 7))
{
set_errno (EINVAL);
return false;
}
strcpy (mangled, wincap.has_terminal_services () ? "Global\\" : "");
char *d = mangled + strlen (mangled);
const char *s = name;
while (*s)
*d++ = (*s == '\\') ? '/' : *s++;
*d = '\0';
return true;
}
sem_t *
sem_open (const char *name, int oflag, ...)
{
mode_t mode = 0;
unsigned int value = 0;
if (oflag & O_CREAT)
{
va_list ap;
va_start (ap, oflag);
mode = va_arg (ap, mode_t);
value = va_arg (ap, unsigned int);
va_end (ap);
}
char mangled_name[MAX_PATH + 1];
if (!mangle_sem_name (mangled_name, name))
return NULL;
return semaphore::open (mangled_name, oflag, mode, value);
}
int
sem_close (sem_t * sem)
{
return semaphore::destroy (sem);
}
int
sem_wait (sem_t * sem)
{
@ -169,10 +220,22 @@ sem_trywait (sem_t * sem)
return semaphore::trywait (sem);
}
int
sem_timedwait (sem_t * sem, const struct timespec *abstime)
{
return semaphore::timedwait (sem, abstime);
}
int
sem_post (sem_t * sem)
{
return semaphore::post (sem);
}
int
sem_getvalue (sem_t * sem, int *sval)
{
return semaphore::getvalue (sem, sval);
}
}