This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 033/114] Add implementation for semaphores and shared memory for Pheonix.


From: Kuba Sejdak <jakub.sejdak@phoesys.com>

---
 newlib/libc/sys/phoenix/semaphore.c | 163 ++++++++++++++++++++++++++++++++++++
 newlib/libc/sys/phoenix/shm.c       |  61 ++++++++++++++
 2 files changed, 224 insertions(+)
 create mode 100644 newlib/libc/sys/phoenix/semaphore.c
 create mode 100644 newlib/libc/sys/phoenix/shm.c

diff --git a/newlib/libc/sys/phoenix/semaphore.c b/newlib/libc/sys/phoenix/semaphore.c
new file mode 100644
index 0000000..11d5623
--- /dev/null
+++ b/newlib/libc/sys/phoenix/semaphore.c
@@ -0,0 +1,163 @@
+/* Copyright (C) 2012-2016 Phoenix Systems (http://www.phoesys.com/).
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <malloc.h>
+#include <semaphore.h>
+#include <stdarg.h>
+#include <string.h>
+#include <sys/dirent.h>
+#include <sys/ioctl.h>
+#include <sys/ipc.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int sem_init(sem_t *sem, int pshared, unsigned int value)
+{
+	errno = ENOSYS;
+	return -1;
+}
+
+int sem_destroy(sem_t *sem)
+{
+	errno = ENOSYS;
+	return -1;
+}
+
+sem_t *sem_open(const char *name, int oflag, ...)
+{
+	sem_t *sem = (sem_t *) malloc(sizeof(sem_t));
+	if(sem == NULL) {
+		errno = ENOMEM;
+		return SEM_FAILED;
+	}
+
+	if (*name == '/')
+		++name;
+
+	char sem_name[PATH_MAX_SIZE + 24] = "/dev/ipc/sem_";
+	strncpy(sem_name + 13, name, PATH_MAX_SIZE + 10);
+
+	int mode = S_IRWXUGO;
+	int initval = 1;
+	if (oflag & O_CREAT) {
+		va_list ap;
+		va_start(ap, oflag);
+		mode = va_arg(ap, int);
+		initval = va_arg(ap, int);
+		va_end(ap);
+	}
+
+	sem->fd = open(sem_name, oflag, mode);
+	if (sem->fd != -1) {
+		int flags = fcntl(sem->fd, F_GETFD, 0);
+		if (flags >= 0) {
+			flags |= FD_CLOEXEC;
+			flags = fcntl (sem->fd, F_SETFD, flags);
+		}
+
+		if (flags == -1) {
+			close (sem->fd);
+			free(sem);
+			return SEM_FAILED;
+		}
+	}
+	else {
+		free(sem);
+		return SEM_FAILED;
+	}
+
+	if (sem != SEM_FAILED && (oflag | O_CREAT)) {
+		if (ioctl(sem->fd, IPC_SEM_INIT, initval) < 0) {
+			close(sem->fd);
+			free(sem);
+			return SEM_FAILED;
+		}
+	}
+
+	return sem;
+}
+
+int sem_close(sem_t *sem)
+{
+	if (sem == NULL || sem->fd < 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	close(sem->fd);
+	free(sem);
+
+	return 0;
+}
+
+int sem_unlink(const char *name)
+{
+	return unlink(name);
+}
+
+int sem_wait(sem_t *sem)
+{
+	if(sem == NULL || sem->fd < 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	return ioctl(sem->fd, IPC_SEM_DOWN, 0);
+}
+
+int sem_timedwait(sem_t * sem, const struct timespec *abs_timeout)
+{
+	if(sem == NULL || sem->fd < 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	return ioctl(sem->fd, IPC_SEM_DOWN_TIMEOUT, 0);
+}
+
+int sem_trywait(sem_t *sem)
+{
+	if(sem == NULL || sem->fd < 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	return ioctl(sem->fd, IPC_SEM_TRYDOWN, 0);
+}
+
+int sem_post(sem_t *sem)
+{
+	if(sem == NULL || sem->fd < 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	return ioctl(sem->fd, IPC_SEM_UP, 0);
+}
+
+int sem_getvalue (sem_t *sem, int *sval)
+{
+	if(sem == NULL || sem->fd < 0) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	return ioctl(sem->fd, IPC_SEM_GETVAL, (unsigned long) sval);
+}
diff --git a/newlib/libc/sys/phoenix/shm.c b/newlib/libc/sys/phoenix/shm.c
new file mode 100644
index 0000000..b16696e
--- /dev/null
+++ b/newlib/libc/sys/phoenix/shm.c
@@ -0,0 +1,61 @@
+/* Copyright (C) 2012-2016 Phoenix Systems (http://www.phoesys.com/).
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <fcntl.h>
+#include <string.h>
+#include <sys/dirent.h>
+#include <sys/ioctl.h>
+#include <sys/ipc.h>
+#include <unistd.h>
+
+int shm_open(const char *name, int oflag, mode_t mode)
+{
+	char shm_name[PATH_MAX_SIZE + 24] = "/dev/ipc/shm_";
+
+	if (*name == '/')
+	  ++name;
+
+	strncpy(shm_name + 13, name, PATH_MAX_SIZE + 10);
+
+	int fd = open(shm_name, oflag, mode);
+	if (fd >= 0) {
+		int flags = fcntl(fd, F_GETFD, 0);
+		if (flags >= 0) {
+			flags |= FD_CLOEXEC;
+			flags = fcntl(fd, F_SETFD, flags);
+		}
+
+		if (flags == -1) {
+			close(fd);
+			return -1;
+		}
+	}
+
+	if (fd != -1 && (oflag | O_CREAT)) {
+		if (ioctl(fd, IPC_SHM_INIT, 0) < 0)
+			close(fd);
+			return -1;
+	}
+
+	return fd;
+}
+
+int shm_unlink(const char *name)
+{
+	return unlink(name);
+}
-- 
2.5.0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]