rev 149 - in trunk: include/prothon src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-03-27 03:15:55 -0500 (Sat, 27 Mar 2004)
New Revision: 149

Modified:
   trunk/include/prothon/prothon.h
   trunk/src/main.c
   trunk/src/object.c
   trunk/src/object.h
Log:
changed start_a_thread to new_thread_obj, thread
registry is now a list of objects, new_thread_obj and
register_thread both return thread object now.

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-03-27 07:01:43 UTC (rev 148)
+++ trunk/include/prothon/prothon.h	2004-03-27 08:15:55 UTC (rev 149)
@@ -58,7 +58,9 @@
 
 #include <apr.h>
 #include <apr_pools.h>
- 
+#include <apr_thread_proc.h>
+#include <apr_portable.h>
+
 #define PROTHON_VERSION_NUMBER	"0.0"
 #define PROTHON_VERSION_BUILD   "$Rev$"  
 #define PROTHON_VERSION_DATE	__DATE__
@@ -175,6 +177,15 @@
 
 typedef pr_str_t* pr_str_p;
 
+typedef struct {
+	apr_os_thread_t*	apr_os_thread;
+	apr_thread_t*		apr_thread;
+	int					main_id;
+	int					running;
+} pr_thread_t;
+
+typedef pr_thread_t* pr_thread_p;
+
 //************************* MALLOC FUNCTIONS **********************************
 // These behave identical to the standard OS functions, but on segments smaller
 // than 64 bytes are much faster than most OS implementations.  See prmalloc.c.
@@ -209,33 +220,34 @@
 	SLICE_PROTO,		// 16 Slice
 	SUPER_PROTO,		// 17 Super
 	GEN_PROTO,			// 18 gen (generator)
+	THREAD_PROTO,		// 19 Thread
 
 // Exceptions
-	EXCEPTION,			//  19 Exception
-	INTERNAL_EXC,		//  20 InternalError
-	PARSEERROR_EXC,		//  21
-	INTERPRETER_EXC,	//  22
-	ASSERTION_EXC,		//  23 AssertionError
-	NAME_EXC,			//  24 NameError
-	INDEX_EXC,			//  25 IndexError
-	TYPE_EXC,			//  26 TypeError
-	MUTABLE_EXC,		//	27 Mutable Error
-	DIVIDEZERO_EXC,		//  28
-	OUTOFMEMORY_EXC,	//  29
-	IOEXCEPTION,		//  30
-	FUNCNOTFOUND_EXC,	//  31
-	FILENOTFOUND_EXC,	//  32
-	END_OF_GEN_EXC,		//  33
-	LOCK_EXC,			//  34
+	EXCEPTION,			//  Exception
+	INTERNAL_EXC,		//  InternalError
+	PARSEERROR_EXC,		//  
+	INTERPRETER_EXC,	//  
+	ASSERTION_EXC,		//  AssertionError
+	NAME_EXC,			//  NameError
+	INDEX_EXC,			//  IndexError
+	TYPE_EXC,			//  TypeError
+	MUTABLE_EXC,		//	Mutable Error
+	DIVIDEZERO_EXC,		//  
+	OUTOFMEMORY_EXC,	//  
+	IOEXCEPTION,		//  
+	FUNCNOTFOUND_EXC,	//  
+	FILENOTFOUND_EXC,	//  
+	END_OF_GEN_EXC,		//  
+	LOCK_EXC,			//  
 
 // constants
-	ZERO_INT,			// 35 0
-	MAX_INT,			// 36 MaxInt
+	ZERO_INT,			//  
+	MAX_INT,			//  MaxInt
 
 // Containers
-	ROOT_GLOBALS,		//  37 Root_Globals
-	SYMBOLS,			//  38 Symbols_dict
-	MODULES,			//  39 Modules
+	ROOT_GLOBALS,		//  Root_Globals
+	SYMBOLS,			//  Symbols_dict
+	MODULES,			//  Modules
 
 	OBJ_ENUM_COUNT
 } obj_enum_t;
@@ -588,6 +600,23 @@
 // liberally to make browsing objects easier and to make dumps readable.
 void add_doc_to_obj(isp ist, obj_p obj, char* str);
 
+// NEW_THREAD_OBJ: Create a new thread object and start the thread
+// thread_func is the C function to execute.  thread_data points to 
+// arugments for the thread_func function.
+// Note: thread_func must call register_thread() (see below) immediately after
+// starting.
+obj_p new_thread_obj(apr_thread_start_t thread_func, void *thread_data);
+
+// REGISTER_THREAD: Register a newly started thread
+// This tells Prothon that a thread has actually started, releasees the
+// thread_registry_lock so that other threads can start, and returns the
+// thread object for the thread that has just started running.
+// The parameter "this_thread" that is needed for this call is passed to 
+// the thread_func function (see APR docs).
+// Example thread_func: void *main_thread(apr_thread_t *handle, void *argv);
+// The handle parameter is used for this_thread:  register_thread(handle);
+obj_p register_thread(apr_thread_t *this_thread);
+
 //****************** HIGHER LEVEL OBJECT ACCESS ROUTINES ************************
 
 // xxx_ITEM: These are higher-level access routines for attributes and data

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2004-03-27 07:01:43 UTC (rev 148)
+++ trunk/src/main.c	2004-03-27 08:15:55 UTC (rev 149)
@@ -81,22 +81,29 @@
 	return apr_root_pool;
 }
 
+static obj_p new_thread_object;
 static clist_p thread_registry;
 static DECLARE_PR_LOCK(thread_registry_lock);
 
 /* New threads must call this first thing, it will release the
- * thread_registry_lock so that other threads can start. */
-void register_thread(apr_thread_t *this_thread)
+ * thread_registry_lock so that other threads can start. 
+ * Returns the matching thread object */
+obj_p register_thread(apr_thread_t *this_thread)
 {
-	clist_append(thread_registry, (void *)apr_os_thread_current());
-	clist_append(thread_registry, this_thread);
+	obj_p res = new_thread_object;
+	pr_thread_p thread_ptr    = res->data.ptr;
+	thread_ptr->apr_os_thread = apr_os_thread_current();
+	thread_ptr->apr_thread    = this_thread;
+	clist_append(thread_registry, res);
+	thread_ptr->running		  = TRUE;
 	pr_unlock(&thread_registry_lock);
+	return res;
 }
 
 static apr_threadattr_t *thread_attr = NULL;
 static apr_pool_t *thread_pool = NULL;
 
-static void start_new_thread(apr_thread_start_t thread_func, void *thread_data)
+static obj_p new_thread_obj(apr_thread_start_t thread_func, void *thread_data)
 {
 	apr_status_t aprerr;
 	apr_thread_t *new_thread = NULL;
@@ -123,14 +130,18 @@
 	 * calls register_thread(). */
 	pr_lock(&thread_registry_lock);
 
-	if ((aprerr = apr_thread_create(&new_thread, thread_attr, thread_func,
-					thread_data, thread_pool)) != APR_SUCCESS ) {
+	new_thread_object = new_object(OBJ(THREAD_PROTO));
+	new_thread_object->data_type = OBJ_TYPE_DATAPTR;
+	new_thread_object->data.ptr  = pr_malloc(sizeof(pr_thread_t));
+	memset(new_thread_object->data.ptr, 0, sizeof(pr_thread_t));
+
+	if ( ( aprerr = apr_thread_create( &new_thread, thread_attr, thread_func,
+					            thread_data, thread_pool ) ) != APR_SUCCESS ) {
 		printf("Main error: apr_thread_create: %s\n",
 			apr_strerror(aprerr, buf, sizeof(buf)));
 		pr_exit(1);
 	}
-
-	return;
+	return new_thread_object;
 }
 
 apr_thread_t* os_thread_2_apr(apr_os_thread_t os_thread)
@@ -198,21 +209,25 @@
 	}
 
 	main_argv_obj = get_attr(ist, sys_argv_obj, sym(ist, "argv"));
-	if (main_argv_obj && list_len(ist, main_argv_obj))
-		start_new_thread((apr_thread_start_t)main_thread, main_argv_obj);
+	if (main_argv_obj && list_len(ist, main_argv_obj)) {
+		new_thread_obj((apr_thread_start_t)main_thread, main_argv_obj);
+		/* Grab this lock, so we know main1 thread has started */
+		pr_lock(&thread_registry_lock); pr_unlock(&thread_registry_lock);
 
+	}
+
 	threads = get_attr(ist, sys_argv_obj, sym(ist, "threads"));
 	if (threads) {
 		int i;
 		for (i = 0; i < list_len(ist, threads); i++)
-			start_new_thread((apr_thread_start_t)main_thread, list_item(ist, threads, i));
+			new_thread_obj((apr_thread_start_t)main_thread, list_item(ist, threads, i));
 	}
 
-	start_new_thread((apr_thread_start_t)mem_mgr_thread, NULL);
+	new_thread_obj((apr_thread_start_t)mem_mgr_thread, NULL);
 
 #ifdef DEBUG_THREADS
 	SetThreadName("main");
-	start_new_thread((apr_thread_start_t)torture_scan_thread, torture_scan_num_ptr);
+	new_thread_obj((apr_thread_start_t)torture_scan_thread, torture_scan_num_ptr);
 #endif
 
 #ifdef DEBUG_THREADS
@@ -220,8 +235,7 @@
 #endif
 
 	/* Grab this lock, so we know all threads are started */
-	pr_lock(&thread_registry_lock);
-	pr_unlock(&thread_registry_lock);
+	pr_lock(&thread_registry_lock); pr_unlock(&thread_registry_lock);
 
 	if (!main_threads_started) 
 		console(ist);

Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c	2004-03-27 07:01:43 UTC (rev 148)
+++ trunk/src/object.c	2004-03-27 08:15:55 UTC (rev 149)
@@ -148,6 +148,7 @@
 	OBJ(SLICE_PROTO) 			    = new_object(NULL);
 	OBJ(SUPER_PROTO) 			    = new_object(NULL);
 	OBJ(GEN_PROTO) 				    = new_object(NULL);
+	OBJ(THREAD_PROTO) 			    = new_object(NULL);
 	OBJ(EXCEPTION) 				    = new_object(NULL);
 	OBJ(INTERNAL_EXC)			    = new_object(OBJ(EXCEPTION));
 	OBJ(PARSEERROR_EXC)			    = new_object(OBJ(EXCEPTION));

Modified: trunk/src/object.h
===================================================================
--- trunk/src/object.h	2004-03-27 07:01:43 UTC (rev 148)
+++ trunk/src/object.h	2004-03-27 08:15:55 UTC (rev 149)
@@ -67,7 +67,6 @@
  * subpools. */
 apr_pool_t* get_pr_head_pool(void);
 
-void register_thread(apr_thread_t *this_thread);
 apr_thread_t* os_thread_2_apr(apr_os_thread_t os_thread);
 
 void console(isp ist);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.