[rkward-cvs] SF.net SVN: rkward: [1457] trunk/rkward/rkward/rbackend

tfry at users.sourceforge.net tfry at users.sourceforge.net
Fri Feb 23 11:31:45 UTC 2007


Revision: 1457
          http://svn.sourceforge.net/rkward/?rev=1457&view=rev
Author:   tfry
Date:     2007-02-23 03:31:44 -0800 (Fri, 23 Feb 2007)

Log Message:
-----------
More portable way of determining stack limits

Modified Paths:
--------------
    trunk/rkward/rkward/rbackend/Makefile.am
    trunk/rkward/rkward/rbackend/rembedinternal.cpp
    trunk/rkward/rkward/rbackend/rembedinternal.h
    trunk/rkward/rkward/rbackend/rthread.cpp

Added Paths:
-----------
    trunk/rkward/rkward/rbackend/rkpthreadsupport.cpp
    trunk/rkward/rkward/rbackend/rkpthreadsupport.h

Modified: trunk/rkward/rkward/rbackend/Makefile.am
===================================================================
--- trunk/rkward/rkward/rbackend/Makefile.am	2007-02-23 10:58:46 UTC (rev 1456)
+++ trunk/rkward/rkward/rbackend/Makefile.am	2007-02-23 11:31:44 UTC (rev 1457)
@@ -5,7 +5,7 @@
 
 noinst_LIBRARIES =  librbackend.a
 librbackend_a_SOURCES = rembedinternal.cpp rinterface.cpp rthread.cpp rcommand.cpp rcommandreceiver.cpp rcommandstack.cpp \
-rdata.cpp
+rdata.cpp rkpthreadsupport.cpp
 noinst_HEADERS = rembedinternal.h rinterface.h rthread.h rcommand.h rcommandreceiver.h rcommandstack.h \
-rdata.h
+rdata.h rkpthreadsupport.h
 SUBDIRS =  rpackages

Modified: trunk/rkward/rkward/rbackend/rembedinternal.cpp
===================================================================
--- trunk/rkward/rkward/rbackend/rembedinternal.cpp	2007-02-23 10:58:46 UTC (rev 1456)
+++ trunk/rkward/rkward/rbackend/rembedinternal.cpp	2007-02-23 11:31:44 UTC (rev 1457)
@@ -49,7 +49,6 @@
 //#include <signal.h>
 //#include <unistd.h>
 #include <math.h>
-#include <pthread.h>
 
 #if (R_VERSION > R_Version(2, 2, 9))
 #define R_2_3
@@ -517,16 +516,9 @@
 	return R_NilValue;
 }
 
-bool REmbedInternal::startR (int argc, char** argv) {
+bool REmbedInternal::startR (int argc, char** argv, size_t stacksize, void *stackstart) {
 	r_running = true;
 #ifdef R_2_3
-	// see http://source.winehq.org/source/loader/pthread.c?v=wine-0.9.26 for a portable way of getting stack limits. Now, how to write configure checks for that?
-	pthread_attr_t attr;
-	size_t stacksize;
-	void *stackstart;
-	pthread_getattr_np (pthread_self (), &attr);
-	pthread_attr_getstack (&attr, &stackstart, &stacksize);
-
 	Rf_initialize_R (argc, argv);
 	R_CStackStart = (uintptr_t) stackstart;
 	R_CStackLimit = stacksize;
@@ -534,8 +526,6 @@
 	RKGlobals::na_double = NA_REAL;
 	R_Interactive = (Rboolean) TRUE;
 	R_ReplDLLinit ();
-
-	pthread_attr_destroy (&attr);
 	return true;
 #else
 	bool ok = (Rf_initEmbeddedR (argc, argv) >= 0);

Modified: trunk/rkward/rkward/rbackend/rembedinternal.h
===================================================================
--- trunk/rkward/rkward/rbackend/rembedinternal.h	2007-02-23 10:58:46 UTC (rev 1456)
+++ trunk/rkward/rkward/rbackend/rembedinternal.h	2007-02-23 11:31:44 UTC (rev 1457)
@@ -18,6 +18,8 @@
 #ifndef R_EMBED_H
 #define R_EMBED_H
 
+#include <stddef.h>
+
 /** This struct is an ugly hack that allows us to pass all R standard callbacks to the main thread and back using the same mechanism.
 Basically, it contains enough space for all arguments and return values ever used. However, of course, it is inherently totally unsafe.
 All rests on having the handling functions know exactly, how these variables are used. So be extremely careful with modifications!
@@ -83,8 +85,10 @@
 protected:
 /** low-level initialization of R
 @param argc Number of arguments as would be passed on the commandline to R
- at param argv Arguments as would be passed on the commandline to R */
-	bool startR (int argc, char **argv);
+ at param argv Arguments as would be passed on the commandline to R
+ at param stacksize Stacksize to use in the R thread
+ at param stackstart Base of stack to use in the R thread */
+	bool startR (int argc, char **argv, size_t stacksize, void *stackstart);
 /** low-level running of a command.
 @param command char* of the command to be run
 @param error this will be set to a value in RKWardError depending on success/failure of the command

Added: trunk/rkward/rkward/rbackend/rkpthreadsupport.cpp
===================================================================
--- trunk/rkward/rkward/rbackend/rkpthreadsupport.cpp	                        (rev 0)
+++ trunk/rkward/rkward/rbackend/rkpthreadsupport.cpp	2007-02-23 11:31:44 UTC (rev 1457)
@@ -0,0 +1,55 @@
+/***************************************************************************
+                          rkpthreadsupport  -  description
+                             -------------------
+    begin                : Fri Feb 23 2007
+    copyright            : (C) 2007 by Thomas Friedrichsmeier
+    email                : tfry at users.sourceforge.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "rkpthreadsupport.h"
+
+#include "../../config.h"
+
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#	include <pthread_np.h>
+#endif
+
+/* This code is mostly borrowed from WINE (http://www.winehq.org) */
+
+void RKGetCurrentThreadStackLimits (size_t *size, void **base) {
+#ifdef HAVE_PTHREAD_GETATTR_NP
+	pthread_attr_t attr;
+	pthread_getattr_np (pthread_self (), &attr);
+	pthread_attr_getstack (&attr, base, size);
+	pthread_attr_destroy (&attr);
+#elif defined(HAVE_PTHREAD_ATTR_GET_NP)
+	pthread_attr_t attr;
+	pthread_attr_init (&attr);
+	pthread_attr_get_np (pthread_self (), &attr);
+	pthread_attr_getstack (&attr, base, size);
+	pthread_attr_destroy (&attr);
+#elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
+	char dummy;
+	size = pthread_get_stacksize_np (pthread_self ());
+	base = pthread_get_stackaddr_np (pthread_self ());
+	/* if base is too large assume it's the top of the stack instead */
+	if ((char *) base > &dummy)
+	base = (char *) base - size;
+#else
+#	warning Can't determine the stack limits of a pthread on this system
+#	warning R C stack checking will be disabled
+	char dummy;
+	*base = &dummy;
+	*size = (unsigned long) -1;
+#endif
+}

Added: trunk/rkward/rkward/rbackend/rkpthreadsupport.h
===================================================================
--- trunk/rkward/rkward/rbackend/rkpthreadsupport.h	                        (rev 0)
+++ trunk/rkward/rkward/rbackend/rkpthreadsupport.h	2007-02-23 11:31:44 UTC (rev 1457)
@@ -0,0 +1,26 @@
+/***************************************************************************
+                          rkpthreadsupport  -  description
+                             -------------------
+    begin                : Fri Feb 23 2007
+    copyright            : (C) 2007 by Thomas Friedrichsmeier
+    email                : tfry at users.sourceforge.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef RKPTHREADSUPPORT_H
+#define RKPTHREADSUPPORT_H
+
+#include <stddef.h>
+
+/** Try different ways to get the stack limits of the currently running thread */
+void RKGetCurrentThreadStackLimits (size_t *size, void **base);
+
+#endif

Modified: trunk/rkward/rkward/rbackend/rthread.cpp
===================================================================
--- trunk/rkward/rkward/rbackend/rthread.cpp	2007-02-23 10:58:46 UTC (rev 1456)
+++ trunk/rkward/rkward/rbackend/rthread.cpp	2007-02-23 11:31:44 UTC (rev 1457)
@@ -18,6 +18,7 @@
 
 #include "rinterface.h"
 #include "rcommandstack.h"
+#include "rkpthreadsupport.h"
 #include "../settings/rksettingsmoduler.h"
 #include "../settings/rksettingsmodulegeneral.h"
 #include "../rkglobals.h"
@@ -424,7 +425,10 @@
 	int argc = 2;
 	char* argv[2] = { qstrdup ("--slave"), qstrdup ("--no-save") };
 
-	startR (argc, argv);
+	size_t stacksize;
+	void *stackstart;
+	RKGetCurrentThreadStackLimits (&stacksize, &stackstart);
+	startR (argc, argv, stacksize, stackstart);
 
 	connectCallbacks ();
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list