[beast/win32: 4/44] Added Birnet::OS code which contains OS specific code.



commit 33479cb597abe0c0681f7d3d3752b303b3e828e3
Author: Stefan Westerfeld <stefan space twc de>
Date:   Fri Aug 28 09:03:30 2009 +0200

    Added Birnet::OS code which contains OS specific code.

 birnet/birnetos.hh      |   66 ++++++++++++++++
 birnet/birnetosunix.cc  |  190 +++++++++++++++++++++++++++++++++++++++++++++++
 birnet/birnetoswin32.cc |  158 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 414 insertions(+), 0 deletions(-)
---
diff --git a/birnet/birnetos.hh b/birnet/birnetos.hh
new file mode 100644
index 0000000..2538189
--- /dev/null
+++ b/birnet/birnetos.hh
@@ -0,0 +1,66 @@
+/* BirnetOS
+ * Copyright (C) 2009 Stefan Westerfeld
+ *
+ * This 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.
+ *
+ * This 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.
+ *
+ * A copy of the GNU Lesser General Public License should ship along
+ * with this library; if not, see http://www.gnu.org/copyleft/.
+ */
+#ifndef __BIRNET_OS_HH__
+#define __BIRNET_OS_HH__
+
+#include <birnet/birnetutils.hh>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+namespace Birnet {
+
+namespace OS {
+
+/* --- functions --- */
+int   get_thread_priority (int thread_id);
+bool  check_sse_sys();
+void  syslog (int priority, const char *format, ...);
+void  raise_sigtrap();
+int   getpid();
+void  memset4 (uint32 *mem, uint32 filler, uint length); 
+int   mkdir (const char *path, mode_t mode);
+
+// stat
+int   lstat (const char *path, struct stat *buf);
+bool  stat_is_socket (mode_t mode);        /* S_ISSOCK */
+bool  stat_is_link (mode_t mode);          /* S_ISLNK */
+
+// (at least) 48-bit random number generator
+void  srand48 (int32 seedval);
+int32 lrand48();
+
+// printf formatting
+int   vasprintf (char **strp, const char *fmt, va_list ap);
+
+// errno
+int   strerror_r (int errnum, char *buf, size_t buflen);
+
+} // OS
+
+// permissions
+extern int OS_S_IXGRP;
+extern int OS_S_IXOTH;
+
+// some errno values are not supported by every system
+extern int OS_ENOTBLK;
+extern int OS_ENOTSOCK;
+
+} // Birnet
+
+#endif /* __BIRNET_OS_HH__ */
+/* vim:set ts=8 sts=2 sw=2: */
diff --git a/birnet/birnetosunix.cc b/birnet/birnetosunix.cc
new file mode 100644
index 0000000..9429c16
--- /dev/null
+++ b/birnet/birnetosunix.cc
@@ -0,0 +1,190 @@
+/* BirnetOSUnix
+ * Copyright (C) 2009 Stefan Westerfeld
+ *
+ * This 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.
+ *
+ * This 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.
+ *
+ * A copy of the GNU Lesser General Public License should ship along
+ * with this library; if not, see http://www.gnu.org/copyleft/.
+ */
+#include "birnetos.hh"
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <signal.h>
+#include <setjmp.h>
+#include <glib.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <syslog.h>
+#include <errno.h>
+#include <string.h>
+
+namespace Birnet {
+namespace OS {
+
+int
+get_thread_priority (int thread_id)
+{
+  return getpriority (PRIO_PROCESS, thread_id);
+}
+
+static jmp_buf check_sse_sys_jmp_buf;
+
+static void BIRNET_NORETURN
+check_sse_sys_sigill_handler (int dummy)
+{
+  longjmp (check_sse_sys_jmp_buf, 1);
+}
+
+bool
+check_sse_sys()
+{
+  struct sigaction action, old_action;
+  bool x86_ssesys = false;
+
+  action.sa_handler = check_sse_sys_sigill_handler;
+  sigemptyset (&action.sa_mask);
+  action.sa_flags = SA_NOMASK;
+  sigaction (SIGILL, &action, &old_action);
+  if (setjmp (check_sse_sys_jmp_buf) == 0)
+    {
+      unsigned int mxcsr;
+      __asm__ __volatile__ ("stmxcsr %0 ; sfence ; emms" : "=m" (mxcsr));
+      /* executed SIMD instructions without exception */
+      x86_ssesys = true;
+    }
+  else
+    {
+      /* signal handler jumped here */
+      // g_printerr ("caught SIGILL\n");
+    }
+  sigaction (SIGILL, &old_action, NULL);
+  return x86_ssesys;
+}
+
+void
+raise_sigtrap ()
+{
+  raise (SIGTRAP);
+}
+
+void
+memset4 (guint32   *mem,
+         guint32    filler,
+         guint      length) 
+{
+  BIRNET_STATIC_ASSERT (sizeof (*mem) == 4);
+  BIRNET_STATIC_ASSERT (sizeof (filler) == 4);
+  BIRNET_STATIC_ASSERT (sizeof (wchar_t) == 4);
+  wmemset ((wchar_t*) mem, filler, length);
+}
+
+// (at least) 48-bit random number generator
+void
+srand48 (int32 seedval)
+{
+  ::srand48 (seedval);
+}
+
+int32
+lrand48()
+{
+  return ::lrand48();
+}
+
+int
+getpid()
+{
+  return ::getpid();
+}
+
+int
+strerror_r (int errnum, char *buf, size_t buflen)
+{
+  char *result = ::strerror_r (errnum, buf, buflen);
+  if (!result)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  if (result != buf)
+    {
+      if (strlen (result) + 1 < buflen)
+	{
+	  strcpy (buf, result);
+	  return 0;
+	}
+      else
+	{
+	  errno = ERANGE;
+	}
+    }
+  else
+    {
+      return 0;
+    }
+  return -1;
+}
+
+void
+syslog (int priority, char const* format, ...)
+{
+  va_list ap;
+
+  va_start(ap, format);
+  vsyslog (priority, format, ap);
+  va_end(ap);
+}
+
+int
+lstat (const char  *path,
+       struct stat *buf)
+{
+  return ::lstat (path, buf);
+}
+
+bool
+stat_is_socket (mode_t mode)
+{
+  return S_ISSOCK (mode);
+}
+
+bool
+stat_is_link (mode_t mode)
+{
+  return S_ISLNK (mode);
+}
+
+int
+vasprintf (char **strp, const char *fmt, va_list ap)
+{
+  return ::vasprintf (strp, fmt, ap);
+}
+
+int
+mkdir (const char *path,
+       mode_t      mode)
+{
+  return ::mkdir (path, mode);
+}
+
+} // OS
+
+// permissions
+int OS_S_IXGRP = S_IXGRP;
+int OS_S_IXOTH = S_IXOTH;
+
+// some errno values are not supported by every system
+int OS_ENOTBLK = ENOTBLK;
+int OS_ENOTSOCK = ENOTSOCK;
+
+
+} // Birnet
+
diff --git a/birnet/birnetoswin32.cc b/birnet/birnetoswin32.cc
new file mode 100644
index 0000000..d6a796d
--- /dev/null
+++ b/birnet/birnetoswin32.cc
@@ -0,0 +1,158 @@
+/* BirnetOSWin32
+ * Copyright (C) 2009 Stefan Westerfeld
+ *
+ * This 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.
+ *
+ * This 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.
+ *
+ * A copy of the GNU Lesser General Public License should ship along
+ * with this library; if not, see http://www.gnu.org/copyleft/.
+ */
+#include "birnetos.hh"
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <process.h>
+#include <sys/stat.h>
+
+namespace Birnet {
+namespace OS {
+
+void
+raise_sigtrap()
+{
+  G_BREAKPOINT();
+}
+
+int
+get_thread_priority (int thread_id)
+{
+  /* FIXME */
+  return 0;
+}
+
+bool
+check_sse_sys()
+{
+  return true;
+}
+
+void
+syslog (int priority, char const* format, ...)
+{
+  /* FIXME: */
+  g_error ("syslog is not supported on windows\n");
+}
+
+static inline GRand *
+grandom48()
+{
+  static GRand *r48 = NULL;
+  if (BIRNET_UNLIKELY (!r48))
+    r48 = g_rand_new();
+  return r48;
+}
+
+// (at least) 48-bit random number generator
+void
+srand48 (int32 seedval)
+{
+  g_rand_set_seed (grandom48(), seedval);
+}
+
+int32
+lrand48()
+{
+  return g_rand_int (grandom48());
+}
+
+int
+getpid()
+{
+  return _getpid();
+}
+
+int
+lstat (const char  *path,
+       struct stat *buf)
+{
+  return ::stat (path, buf);
+}
+
+bool
+stat_is_socket (mode_t mode)
+{
+  return false;
+}
+
+bool
+stat_is_link (mode_t mode)
+{
+  return false;
+}
+
+int
+vasprintf (char **strp, const char *fmt, va_list ap)
+{
+  return g_vasprintf (strp, fmt, ap);
+}
+
+/* MSDN recommends using strerror_s as threadsafe replacement for strerror, however it seems to
+   be impossible to use it from mingw gcc, so we reimplement a threadsafe version
+*/
+int
+strerror_r (int errnum, char *buf, size_t buflen)
+{
+  if (errnum >= 0 && errnum < _sys_nerr)
+    {
+      char *err_msg = _sys_errlist[errnum];
+      size_t len = strlen (err_msg);
+      if (len < buflen)
+	{
+	  strcpy (buf, err_msg);
+	  errno = 0;
+	  return 0;
+	}
+      else
+	{
+	  errno = ERANGE;
+	}
+    }
+  else
+    {
+      errno = EINVAL;
+    }
+  return -1;
+}
+
+void
+memset4 (uint32 *mem, uint32 filler, uint length)
+{
+  while (length--)
+    *mem++ = filler;
+}
+
+int
+mkdir (const char *path,
+       mode_t      mode)
+{
+  return ::mkdir (path);
+}
+
+} // OS
+
+/* fake errno values */
+int OS_ENOTBLK  = 1000;
+int OS_ENOTSOCK = 1001;
+
+/* fake permissions */
+int OS_S_IXOTH = 0;
+int OS_S_IXGRP = 0;
+
+} // Birnet
+/* vim:set ts=8 sts=2 sw=2: */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]