rev 320 - in trunk: include/prothon modules/File src

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

Modified:
   trunk/include/prothon/prothon.h
   trunk/modules/File/File.c
   trunk/src/builtins-thread.c
   trunk/src/lock.h
   trunk/src/object.c
Log:
added Mutex object and complete set of functions

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-04-10 21:33:54 UTC (rev 319)
+++ trunk/include/prothon/prothon.h	2004-04-10 22:56:28 UTC (rev 320)
@@ -257,6 +257,7 @@
 	SUPER_PROTO,		// 17 Super
 	GEN_PROTO,			// 18 gen (generator)
 	THREAD_PROTO,		// 19 Thread
+	MUTEX_PROTO,		// 20 Mutex
 
 // Exceptions
 	EXCEPTION,			//  Exception

Modified: trunk/modules/File/File.c
===================================================================
--- trunk/modules/File/File.c	2004-04-10 21:33:54 UTC (rev 319)
+++ trunk/modules/File/File.c	2004-04-10 22:56:28 UTC (rev 320)
@@ -64,7 +64,7 @@
 
 typedef struct {
 	apr_file_t	*stream;	/* APR file reference */
-	int		bufsize;	/* Size of our buffer */
+	int			bufsize;	/* Size of our buffer */
 	apr_pool_t	*pool;		/* Pool associated with this file */
 	apr_int32_t	flags;		/* APR flags */
 } pr_file_t;

Modified: trunk/src/builtins-thread.c
===================================================================
--- trunk/src/builtins-thread.c	2004-04-10 21:33:54 UTC (rev 319)
+++ trunk/src/builtins-thread.c	2004-04-10 22:56:28 UTC (rev 320)
@@ -68,6 +68,7 @@
 DECLARE_PR_LOCK(thread_registry_lock);
 
 MODULE_DECLARE(Thread);
+MODULE_DECLARE(Mutex);
 
 void thread_init(isp ist) {
 	obj_p thread_obj;
@@ -296,10 +297,99 @@
 	return parms[1];
 }
 
+DEF(Thread, join, NULL) {
+	pr_thread_p thread_ptr = self->data.ptr;
+	apr_status_t retval, aprerr;
+	if (!(thread_ptr->apr_thread)) {
+		raise_exception(ist, OBJ(INTERPRETER_EXC), "cannot join this thread");
+		return NULL;
+	}
+	aprerr = apr_thread_join(&retval, thread_ptr->apr_thread); 
+	if (aprerr != APR_SUCCESS) {
+		raise_exception(ist, OBJ(INTERNAL_EXC), "error calling apr_thread_join");
+		return NULL;
+	}
+	return new_int_obj(ist, retval);
+}
+
 DEF(Thread, __objlist__, FORM_RPARAM) {
 	return parms[1];
 }
 
+// ***************************** Mutex ***************************************
+
+MODULE_START(Mutex)
+{
+	Mutex_OBJ = OBJ(MUTEX_PROTO);
+	MODULE_SET_DOC(Mutex, "mutex object prototype");
+
+	set_attr(ist, OBJ(OBJECT), sym(ist,  "Mutex"), Mutex_OBJ);
+	Mutex_OBJ->data_type = OBJ_TYPE_DATAPTR;
+}
+
+
+DEF(Mutex, __init__, NULL) {
+	apr_status_t aprerr;
+	apr_pool_t *subpool;
+
+	aprerr = apr_pool_create(&subpool, get_pr_head_pool());
+	if (aprerr != APR_SUCCESS) {
+		raise_exception(ist, OBJ(OUTOFMEMORY_EXC), "out of memory creating mutex");
+		return NULL;
+	}
+	self->data_type = OBJ_TYPE_DATAPTR;
+	aprerr = apr_thread_mutex_create(&((apr_thread_mutex_t*)(self->data.ptr)), APR_THREAD_MUTEX_UNNESTED, subpool);
+	if (aprerr != APR_SUCCESS) {
+		raise_exception(ist, OBJ(INTERNAL_EXC), "error creating mutex in apr_thread_mutex_create");
+		return NULL;
+	}
+	return OBJ(NONE);
+}
+
+DEF(Mutex, __str__, NULL) {
+	char name[30];
+	apr_snprintf(name, sizeof(name), "<Mutex:%lx>", (unsigned long)(intptr_t)self);
+	return new_string_obj(ist, name);
+}
+
+DEF(Mutex, lock, NULL) {
+	apr_status_t aprerr = apr_thread_mutex_lock((apr_thread_mutex_t*)(self->data.ptr));
+	if (aprerr != APR_SUCCESS) {
+		raise_exception(ist, OBJ(INTERNAL_EXC), "error in apr_thread_mutex_lock");
+		return NULL;
+	}
+	return self;
+}
+
+DEF(Mutex, try_lock, NULL) {
+	apr_status_t aprerr = apr_thread_mutex_trylock((apr_thread_mutex_t*)(self->data.ptr));
+	if (APR_STATUS_IS_EBUSY(aprerr)) return OBJ(PR_FALSE);
+	else if (aprerr == APR_SUCCESS)	 return OBJ(PR_TRUE);
+	else {
+		raise_exception(ist, OBJ(INTERNAL_EXC), "error in apr_thread_mutex_trylock");
+		return NULL;
+	}
+}
+
+DEF(Mutex, unlock, NULL) {
+	apr_status_t aprerr = apr_thread_mutex_unlock((apr_thread_mutex_t*)(self->data.ptr));
+	if (aprerr != APR_SUCCESS) {
+		raise_exception(ist, OBJ(INTERNAL_EXC), "error in apr_thread_mutex_unlock");
+		return NULL;
+	}
+	return self;
+}
+
+DEF(Mutex, __del__, NULL) {
+	apr_thread_mutex_destroy((apr_thread_mutex_t*)(self->data.ptr));
+	return OBJ(NONE);
+}
+
+DEF(Mutex, __objlist__, FORM_RPARAM) {
+	return parms[1];
+}
+
+
 MAIN_MODULE_INIT(Thread) {
 	MODULE_SUB_INIT(Thread);
 	MODULE_ADD_SYM(Thread, __init__);
@@ -309,5 +399,15 @@
 	MODULE_ADD_SYM(Thread, set_access);
 	MODULE_ADD_SYM(Thread, running);
 	MODULE_ADD_SYM(Thread, sleep);
+	MODULE_ADD_SYM(Thread, join);
 	MODULE_ADD_SYM(Thread, __objlist__);
+
+	MODULE_SUB_INIT(Mutex);
+	MODULE_ADD_SYM(Mutex, __init__);
+	MODULE_ADD_SYM(Mutex, __str__);
+	MODULE_ADD_SYM(Mutex, lock);
+	MODULE_ADD_SYM(Mutex, try_lock);
+	MODULE_ADD_SYM(Mutex, unlock);
+	MODULE_ADD_SYM(Mutex, __del__);
+	MODULE_ADD_SYM(Mutex, __objlist__);
 }

Modified: trunk/src/lock.h
===================================================================
--- trunk/src/lock.h	2004-04-10 21:33:54 UTC (rev 319)
+++ trunk/src/lock.h	2004-04-10 22:56:28 UTC (rev 320)
@@ -72,7 +72,7 @@
 #endif
 
 typedef struct {
-	int			waiting;
+	int					waiting;
 	apr_uint32_t		lock;
 	apr_thread_cond_t*	cond;
 	apr_thread_mutex_t*	mutex;

Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c	2004-04-10 21:33:54 UTC (rev 319)
+++ trunk/src/object.c	2004-04-10 22:56:28 UTC (rev 320)
@@ -187,6 +187,7 @@
 	OBJ(SLICE_PROTO)        = new_object(ist, NULL);
 	OBJ(SUPER_PROTO)        = new_object(ist, NULL);
 	OBJ(THREAD_PROTO)       = new_object(ist, NULL);
+	OBJ(MUTEX_PROTO)        = new_object(ist, NULL);
 	OBJ(ROOT_GLOBALS)       = new_object(ist, NULL);
 	OBJ(MODULES)            = new_object(ist, NULL);
 	OBJ(PR_FALSE)           = new_object(ist, NULL);
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.