rev 319 - trunk/src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-04-10 17:33:54 -0400 (Sat, 10 Apr 2004)
New Revision: 319

Modified:
   trunk/src/builtins-core.c
   trunk/src/builtins-thread.c
   trunk/src/interp.c
   trunk/src/main.c
   trunk/src/object.h
   trunk/src/thread.h
Log:
added Object.Current_thread(), added several functions
to thread, fixed permission exception reporting problem

Modified: trunk/src/builtins-core.c
===================================================================
--- trunk/src/builtins-core.c	2004-04-10 13:56:12 UTC (rev 318)
+++ trunk/src/builtins-core.c	2004-04-10 21:33:54 UTC (rev 319)
@@ -308,7 +308,9 @@
 	return dict_obj;
 }
 
-
+DEF(Object, Current_thread, NULL) {
+	return os_thread_2_obj(apr_os_thread_current());
+}
 // ***************************** PR_FALSE *****************************************
 
 MODULE_START(False)
@@ -650,6 +652,7 @@
 	MODULE_ADD_SYM(Object, __in__QUES);
 	MODULE_ADD_SYM(Object, __notin__QUES);
 	MODULE_ADD_SYM(Object, attrs);
+	MODULE_ADD_SYM(Object, Current_thread);
 
 	MODULE_SUB_INIT(Seq);
 

Modified: trunk/src/builtins-thread.c
===================================================================
--- trunk/src/builtins-thread.c	2004-04-10 13:56:12 UTC (rev 318)
+++ trunk/src/builtins-thread.c	2004-04-10 21:33:54 UTC (rev 319)
@@ -69,6 +69,24 @@
 
 MODULE_DECLARE(Thread);
 
+void thread_init(isp ist) {
+	obj_p thread_obj;
+	pr_thread_p thread_ptr;
+	thread_obj = new_object(ist, OBJ(THREAD_PROTO));
+	thread_obj->data_type = OBJ_TYPE_DATAPTR;
+	thread_ptr = thread_obj->data.ptr  = pr_malloc(sizeof(pr_thread_t));
+	memset(thread_obj->data.ptr, 0, sizeof(pr_thread_t));
+	thread_ptr->ist           = ist;
+	thread_ptr->apr_os_thread = apr_os_thread_current();
+	thread_ptr->running		  = TRUE;
+	set_attr(ist, thread_obj, sym(ist, "name"), new_string_obj(ist, "ConsoleThread"));
+
+	pr_lock(&thread_registry_lock);
+	thread_registry = new_clist(5);
+	clist_append(thread_registry, thread_obj);
+	pr_unlock(&thread_registry_lock);
+}
+
 //**************************** register_thread ********************************
 /* New threads must call this first thing, it will release the
  * thread_registry_lock so that other threads can start. 
@@ -153,6 +171,23 @@
 	return res;
 }
 
+//********************************* os_thread_2_obj ***************************
+obj_p os_thread_2_obj(apr_os_thread_t os_thread) {
+	int i, llen;
+	obj_p res = NULL;
+
+	pr_lock(&thread_registry_lock);
+	llen = clist_len(thread_registry);
+	for (i = 0; i < llen; i++) {
+		res = clist_item(thread_registry, i);
+		if (((pr_thread_p)(res->data.ptr))->apr_os_thread == os_thread) 
+			break;
+	}
+	if (i == llen) res = NULL;
+	pr_unlock(&thread_registry_lock);
+	return res;
+}
+
 // ***************************** Thread ***************************************
 
 MODULE_START(Thread)
@@ -161,8 +196,13 @@
 
 	Thread_OBJ = OBJ(THREAD_PROTO);
 	MODULE_SET_DOC(Thread, "thread object prototype");
-	set_attr(ist, OBJ(OBJECT), sym(ist, "Thread"), Thread_OBJ);
 
+	set_attr(ist, OBJ(OBJECT), sym(ist,  "Thread"), Thread_OBJ);
+	set_attr(ist,  Thread_OBJ, sym(ist,   "GUEST"), new_int_obj(ist,  ACC_GUEST));
+	set_attr(ist,  Thread_OBJ, sym(ist,   "USER1"), new_int_obj(ist,  ACC_USER1));
+	set_attr(ist,  Thread_OBJ, sym(ist,   "USER2"), new_int_obj(ist,  ACC_USER2));
+	set_attr(ist,  Thread_OBJ, sym(ist,  "SYSTEM"), new_int_obj(ist, ACC_SYSTEM));
+
 	Thread_OBJ->data_type = OBJ_TYPE_DATAPTR;
     thread = obj_malloc(Thread_OBJ, sizeof(pr_thread_t));
     memset(thread, 0, sizeof(pr_thread_t));
@@ -222,6 +262,19 @@
 	return new_int_obj(ist, ist->access);
 }
 
+DEF(Thread, set_access, FORM_RPARAM) {
+	int new_access;
+	CHECK_TYPE_EXC(parms[1], OBJ(INT_PROTO), "integer");
+	new_access = (int) parms[1]->data.i64;
+	if (new_access > ist->access) {
+		raise_exception(ist, OBJ(PERMISSION_EXC), 
+			"attempt to raise thread access level");	
+		return NULL;
+	}
+	ist->access = new_access;
+	return parms[1];
+}
+
 DEF(Thread, access_str, NULL) {
 	switch (ist->access) {
 		case 0:	return new_string_obj(ist, "guest");
@@ -237,6 +290,12 @@
 	else                      return OBJ(PR_FALSE);
 }
 
+DEF(Thread, sleep, FORM_RPARAM) {
+	CHECK_TYPE_EXC(parms[1], OBJ(FLOAT_PROTO), "float");
+	apr_sleep((apr_interval_time_t)((parms[1]->data.f64)*1000000));
+	return parms[1];
+}
+
 DEF(Thread, __objlist__, FORM_RPARAM) {
 	return parms[1];
 }
@@ -247,6 +306,8 @@
 	MODULE_ADD_SYM(Thread, __str__);
 	MODULE_ADD_SYM(Thread, access);
 	MODULE_ADD_SYM(Thread, access_str);
+	MODULE_ADD_SYM(Thread, set_access);
 	MODULE_ADD_SYM(Thread, running);
+	MODULE_ADD_SYM(Thread, sleep);
 	MODULE_ADD_SYM(Thread, __objlist__);
 }

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-04-10 13:56:12 UTC (rev 318)
+++ trunk/src/interp.c	2004-04-10 21:33:54 UTC (rev 319)
@@ -532,31 +532,38 @@
 
 #define MIN_OBJ ((obj_p) 10)
 
-#define pre_call_lock(slf, plist)					\
-	read_lock(ist, slf);							\
-	if (!intrp_exobj && plist) {					\
-		for(i=0; i < clist_len(plist); i++) {		\
-			obj_p p = clist_item(plist, i);			\
-			if (p > MIN_OBJ && p != (slf))			\
-				rdlock_brk(p);						\
-		}											\
-		if (intrp_exobj) {							\
-			for (i--; i >= 0; i--) {				\
-				obj_p p = clist_item(plist, i);		\
-				if (p > MIN_OBJ && p != (slf))		\
-					read_unlock(ist, p);			\
-			}										\
-		}											\
+
+//******************************** pre_call_lock ******************************
+void pre_call_lock(isp ist, obj_p slf, clist_p plist) {
+	int i;
+	read_lock(ist, slf);							
+	if (!intrp_exobj && plist) {					
+		for(i=0; i < clist_len(plist); i++) {		
+			obj_p p = clist_item(plist, i);			
+			if (p > MIN_OBJ && p != (slf))			
+				rdlock_brk(p);						
+		}											
+		if (intrp_exobj) {							
+			for (i--; i >= 0; i--) {				
+				obj_p p = clist_item(plist, i);		
+				if (p > MIN_OBJ && p != (slf))		
+					read_unlock(ist, p);			
+			}										
+		}											
 	}
+}
 
-#define post_call_unlock(slf, plist)				\
-	read_unlock(ist, slf);							\
-	if (plist)										\
-		for(i=0; i < clist_len(plist); i++) {		\
-			obj_p p = clist_item(plist, i);			\
-			if (p > MIN_OBJ && p != (slf))			\
-				read_unlock(ist, p);				\
+//******************************** post_call_unlock ***************************
+void post_call_unlock(isp ist, obj_p slf, clist_p plist) {
+	int i;
+	read_unlock(ist, slf);							
+	if (plist)										
+		for(i=0; i < clist_len(plist); i++) {		
+			obj_p p = clist_item(plist, i);			
+			if (p > MIN_OBJ && p != (slf))			
+				read_unlock(ist, p);				
 		}
+}
 
 //******************************** exec_loop **********************************
 obj_p exec_loop(isp ist){
@@ -869,13 +876,14 @@
 				if ((func_obj = fr_stack[fr_sp])->data_type == OBJ_TYPE_FUNCPTR){
 					obj_p res;
 					clist_p plist = get_func_params(ist, func_obj, param*2, fr_stack+fr_sp+1);
-					pre_call_lock(frame->locals, plist); if (intrp_exobj) break;
+					pre_call_lock(ist, frame->locals, plist); if (intrp_exobj) break;
 					res = ((pr_func*)(func_obj->data.ptr))
 								(ist, frame->self, 
 								(plist ? clist_len(plist) : 0), 
 								(plist ? (obj_p*)plist->items : NULL),
 								frame->locals);
-					post_call_unlock(frame->locals, plist);
+					if (intrp_exobj) break;
+					post_call_unlock(ist, frame->locals, plist);
 					free_clist(plist);
 					fr_push(res);
 				} else if (has_proto_QUES(ist, func_obj, OBJ(FUNC_PROTO))) {
@@ -962,13 +970,14 @@
 					obj_p res, slf = super_flag? frame->self : fr_stack[fr_sp];
 					clist_p plist = get_func_params(ist, func_obj, param*2, fr_stack+fr_sp+2);
 					if (intrp_exobj) break;
-					pre_call_lock(slf, plist);  if (intrp_exobj) break;
+					pre_call_lock(ist, slf, plist);  if (intrp_exobj) break;
 					res = ((pr_func*)(func_obj->data.ptr))
 								(ist, slf, 
 								(plist ? clist_len(plist) : 0), 
 								(plist ? (obj_p*)plist->items : NULL),
 								frame->dyn_locals);
-					post_call_unlock(slf, plist);
+					if (intrp_exobj) break;
+					post_call_unlock(ist, slf, plist);
 					free_clist(plist);
 					if (intrp_exobj) break;
 					if (frame->calling_init)
@@ -1211,14 +1220,14 @@
 		self = new_object(ist, NULL);
 	}
 	if (func_obj->data_type == OBJ_TYPE_FUNCPTR) {
-		int i;
 		obj_p res;
 		clist_p plist = get_func_params( ist, func_obj, parm_cnt, lbl_val_arr);
-		pre_call_lock(self, plist); if (intrp_exobj) return NULL;
+		pre_call_lock(ist, self, plist); if (intrp_exobj) return NULL;
 		res = ((pr_func*)(func_obj->data.ptr)) 
 								(ist, self, (plist ? clist_len(plist) : 0), 
 								(plist ? (obj_p*)plist->items : NULL), dyn_locals);
-		post_call_unlock(self, plist);
+		if (intrp_exobj) return NULL;
+		post_call_unlock(ist, self, plist);
 		free_clist(plist);
 		if (!func_sym) del_unlock(self);
 		return res;

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2004-04-10 13:56:12 UTC (rev 318)
+++ trunk/src/main.c	2004-04-10 21:33:54 UTC (rev 319)
@@ -108,8 +108,6 @@
 	}
 	atexit(&apr_terminate);
 
-	thread_registry = new_clist(5);
-
 	if ((aprerr = apr_pool_create(&apr_root_pool, NULL)))  {
 		printf("Main error: apr_pool_create: %s\n",
 		       apr_strerror(aprerr, buf, sizeof(buf)));
@@ -127,6 +125,8 @@
 
 	object_store_init(ist, 20);
 
+	thread_init(ist);
+
 	sys_argv_obj = arginit(ist, argc, argv);
 	sysinit(ist, argv[0], sys_argv_obj);
 

Modified: trunk/src/object.h
===================================================================
--- trunk/src/object.h	2004-04-10 13:56:12 UTC (rev 318)
+++ trunk/src/object.h	2004-04-10 21:33:54 UTC (rev 319)
@@ -72,6 +72,7 @@
 apr_pool_t* get_pr_head_pool(void);
 
 apr_thread_t* os_thread_2_apr(apr_os_thread_t os_thread);
+obj_p         os_thread_2_obj(apr_os_thread_t os_thread);
 
 void console(isp ist);
 

Modified: trunk/src/thread.h
===================================================================
--- trunk/src/thread.h	2004-04-10 13:56:12 UTC (rev 318)
+++ trunk/src/thread.h	2004-04-10 21:33:54 UTC (rev 319)
@@ -69,6 +69,7 @@
 #define thread_frame(thread_obj)     (((pr_thread_p)((thread_obj)->data.ptr))->frame)
 #define thread_running(thread_obj)   (((pr_thread_p)((thread_obj)->data.ptr))->running)
 
+void thread_init(isp ist);
 obj_p register_thread(isp ist, apr_thread_t *this_thread);
 apr_thread_t* os_thread_2_apr(apr_os_thread_t os_thread);
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.