[M3devel] reducing system-dependence in Upthread.i3?

Jay jay.krell at cornell.edu
Thu Feb 5 19:57:17 CET 2009


Hm, nevermind that. More later.
 
I think either wrapping it up more in C would be cleaner or even Modula-3 ("Upthread.m3"), than this sort of "split", or at least changing it to ADDRESS and if size <= BYTESIZE(ADDRESS), just use that space and save the heap alloc.
 
 
On systems that already have either a small object or an indirection, save the heap alloc.
 
 
Later,
 - Jay



From: jay.krell at cornell.eduTo: m3devel at elegosoft.comSubject: reducing system-dependence in Upthread.i3?Date: Thu, 5 Feb 2009 18:11:37 +0000

Does this make sense?It is pushing my Modula-3 knowledge.I didn't yet check the number of bytes requested by malloc/calloc.All platforms would have to be changed, not just what is shown here.  UNTRACED REF ARRAY OF CHAR is "adequately hard to instantiate", right? Don't need to resort to ADDRESS?  OR should we go the other way and remove the heap allocs?  (attached and inline)Index: src/thread/PTHREAD/ThreadPThread.m3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/thread/PTHREAD/ThreadPThread.m3,vretrieving revision 1.90diff -u -r1.90 ThreadPThread.m3--- src/thread/PTHREAD/ThreadPThread.m3 5 Feb 2009 09:47:09 -0000 1.90+++ src/thread/PTHREAD/ThreadPThread.m3 5 Feb 2009 18:04:01 -0000@@ -24,20 +24,20 @@    nextId: CARDINAL := 1; -  activeMu := PTHREAD_MUTEX_INITIALIZER; (* global lock for list of active threads *)-  slotMu   := PTHREAD_MUTEX_INITIALIZER; (* global lock for thread slot table *)-  initMu   := PTHREAD_MUTEX_INITIALIZER; (* global lock for initializers *)+  activeMu := NewMutex(); (* global lock for list of active threads *)+  slotMu   := NewMutex(); (* global lock for thread slot table *)+  initMu   := NewMutex(); (* global lock for initializers *)  REVEAL   Mutex = MutexRep.Public BRANDED "Mutex Pthread-1.0" OBJECT-    mutex: UNTRACED REF pthread_mutex_t := NIL;+    mutex: pthread_mutex_t := NIL;   OVERRIDES     acquire := Acquire;     release := Release;   END;    Condition = BRANDED "Thread.Condition Pthread-1.0" OBJECT-    mutex: UNTRACED REF pthread_mutex_t := NIL;+    mutex: pthread_mutex_t := NIL;     waiters: T := NIL;    (* LL = mutex *)   END; @@ -58,7 +58,7 @@     nextWaiter: T := NIL;   (* LL = waitingOn.mutex *)      (* condition for blocking during "Wait" *)-    waitCond: UNTRACED REF pthread_cond_t;+    waitCond: pthread_cond_t;      (* the alert flag *)     alerted : BOOLEAN := FALSE;   (* LL = mutex *)@@ -94,6 +94,8 @@     heapState : RTHeapRep.ThreadState;   END; +(*---------------------------------------------------------------------------*)+ PROCEDURE SetState (act: Activation;  state: ActState) =   CONST text = ARRAY ActState OF TEXT     { "Starting", "Started", "Stopping", "Stopped" };@@ -108,24 +110,41 @@     END;   END SetState;     +(*---------------------------------------------------------------------------*)++(* probably move this to Upthread.m3 *)+PROCEDURE NewMutex(): pthread_mutex_t =+VAR m := NEW(pthread_mutex_t, Upthread.pthread_mutex_t_size);+    r := Upthread.mutex_init(m);+BEGIN+  <*ASSERT r=0*>+  RETURN m;+END NewMutex;++(* probably move this to Upthread.m3 *)+PROCEDURE NewCond(): pthread_cond_t =+VAR c := NEW(pthread_cond_t, Upthread.pthread_cond_t_size);+    r := Upthread.cond_init(c);+BEGIN+  <*ASSERT r=0*>+  RETURN c;+END NewCond;+ (*----------------------------------------------------------------- Mutex ---*)  PROCEDURE CleanMutex (r: REFANY) =   VAR m := NARROW(r, Mutex);   BEGIN-    WITH r = Upthread.mutex_destroy (m.mutex^) DO <*ASSERT r=0*> END;+    WITH r = Upthread.mutex_destroy (m.mutex) DO <*ASSERT r=0*> END;     DISPOSE(m.mutex);   END CleanMutex;  PROCEDURE InitMutex (m: Mutex) =-  VAR mutex: UNTRACED REF pthread_mutex_t;   BEGIN     TRY       WITH r = Upthread.mutex_lock(initMu) DO <*ASSERT r=0*> END;       IF m.mutex # NIL THEN RETURN END;-      mutex := NEW(UNTRACED REF pthread_mutex_t);-      WITH r = Upthread.mutex_init(mutex^, NIL) DO <*ASSERT r=0*> END;-      m.mutex := mutex;+      m.mutex := NewMutex();     FINALLY       WITH r = Upthread.mutex_unlock(initMu) DO <*ASSERT r=0*> END;     END;@@ -140,7 +159,7 @@     END;     IF m.mutex = NIL THEN InitMutex(m) END;     IF perfOn THEN PerfChanged(self.id, State.locking) END;-    WITH r = Upthread.mutex_lock(m.mutex^) DO+    WITH r = Upthread.mutex_lock(m.mutex) DO       IF r # 0 THEN         RTError.MsgI(ThisFile(), ThisLine(),                      "Thread client error: pthread_mutex_lock error: ", r);@@ -156,7 +175,7 @@     IF self = NIL THEN       Die(ThisLine(), "Release called from a non-Modula-3 thread");     END;-    WITH r = Upthread.mutex_unlock(m.mutex^) DO+    WITH r = Upthread.mutex_unlock(m.mutex) DO       IF r # 0 THEN         RTError.MsgI(ThisFile(), ThisLine(),                      "Thread client error: pthread_mutex_unlock error: ", r);@@ -169,19 +188,16 @@ PROCEDURE CleanCondition (r: REFANY) =   VAR c := NARROW(r, Condition);   BEGIN-    WITH r = Upthread.mutex_destroy (c.mutex^) DO <*ASSERT r=0*> END;+    WITH r = Upthread.mutex_destroy (c.mutex) DO <*ASSERT r=0*> END;     DISPOSE(c.mutex);   END CleanCondition;  PROCEDURE InitCondition (c: Condition) =-  VAR mutex: UNTRACED REF pthread_mutex_t;   BEGIN     TRY       WITH r = Upthread.mutex_lock(initMu) DO <*ASSERT r=0*> END;       IF c.mutex # NIL THEN RETURN END;-      mutex := NEW(UNTRACED REF pthread_mutex_t);-      WITH r = Upthread.mutex_init(mutex^, NIL) DO <*ASSERT r=0*> END;-      c.mutex := mutex;+      c.mutex := NewMutex();     FINALLY       WITH r = Upthread.mutex_unlock(initMu) DO <*ASSERT r=0*> END;     END;@@ -198,7 +214,7 @@       <*ASSERT self.waitingOn = NIL*>       <*ASSERT self.nextWaiter = NIL*>       IF perfOn THEN PerfChanged(self.id, State.waiting) END;-      WITH r = Upthread.mutex_lock(c.mutex^) DO <*ASSERT r=0*> END;+      WITH r = Upthread.mutex_lock(c.mutex) DO <*ASSERT r=0*> END;       BEGIN         self.waitingOn := c;         next := c.waiters;@@ -211,10 +227,10 @@           prev.nextWaiter := self;         END;       END;-      WITH r = Upthread.mutex_unlock(c.mutex^) DO <*ASSERT r=0*> END;+      WITH r = Upthread.mutex_unlock(c.mutex) DO <*ASSERT r=0*> END;       IF alertable AND XTestAlert(self) THEN RAISE Alerted; END;       LOOP-        WITH r = Upthread.cond_wait(self.waitCond^, m.mutex^) DO+        WITH r = Upthread.cond_wait(self.waitCond, m.mutex) DO           IF r # 0 THEN             RTError.MsgI(ThisFile(), ThisLine(),                          "Thread client error: pthread_cond_wait error ", r);@@ -224,7 +240,7 @@         IF self.waitingOn = NIL THEN RETURN END;       END;     FINALLY-      WITH r = Upthread.mutex_lock(c.mutex^) DO <*ASSERT r=0*> END;+      WITH r = Upthread.mutex_lock(c.mutex) DO <*ASSERT r=0*> END;       IF self.waitingOn # NIL THEN         <*ASSERT self.waitingOn = c*>         (* alerted: dequeue from condition *)@@ -240,7 +256,7 @@         self.nextWaiter := NIL;         self.waitingOn := NIL;       END;-      WITH r = Upthread.mutex_unlock(c.mutex^) DO <*ASSERT r=0*> END;+      WITH r = Upthread.mutex_unlock(c.mutex) DO <*ASSERT r=0*> END;       IF perfOn THEN PerfRunning(self.id) END;       <*ASSERT self.waitingOn = NIL*>       <*ASSERT self.nextWaiter = NIL*>@@ -275,23 +291,23 @@     t := c.waiters; c.waiters := t.nextWaiter;     t.nextWaiter := NIL;     t.waitingOn := NIL;-    WITH r = Upthread.cond_signal(t.waitCond^) DO <*ASSERT r=0*> END;+    WITH r = Upthread.cond_signal(t.waitCond) DO <*ASSERT r=0*> END;   END DequeueHead;  PROCEDURE Signal (c: Condition) =   BEGIN     IF c.mutex = NIL THEN InitCondition(c) END;-    WITH r = Upthread.mutex_lock(c.mutex^) DO <*ASSERT r=0*> END;+    WITH r = Upthread.mutex_lock(c.mutex) DO <*ASSERT r=0*> END;     IF c.waiters # NIL THEN DequeueHead(c) END;-    WITH r = Upthread.mutex_unlock(c.mutex^) DO <*ASSERT r=0*> END;+    WITH r = Upthread.mutex_unlock(c.mutex) DO <*ASSERT r=0*> END;   END Signal;  PROCEDURE Broadcast (c: Condition) =   BEGIN     IF c.mutex = NIL THEN InitCondition(c) END;-    WITH r = Upthread.mutex_lock(c.mutex^) DO <*ASSERT r=0*> END;+    WITH r = Upthread.mutex_lock(c.mutex) DO <*ASSERT r=0*> END;     WHILE c.waiters # NIL DO DequeueHead(c) END;-    WITH r = Upthread.mutex_unlock(c.mutex^) DO <*ASSERT r=0*> END;+    WITH r = Upthread.mutex_unlock(c.mutex) DO <*ASSERT r=0*> END;   END Broadcast;  PROCEDURE Alert (t: T) =@@ -299,7 +315,7 @@     LOCK t DO       t.alerted := TRUE;       IF t.waitCond # NIL THEN-        WITH r = Upthread.cond_signal(t.waitCond^) DO <*ASSERT r=0*> END;+        WITH r = Upthread.cond_signal(t.waitCond) DO <*ASSERT r=0*> END;       END;     END;   END Alert;@@ -480,8 +496,7 @@      which will try to acquire "activeMu". *)   VAR t := NEW(T, act := act);   BEGIN-    t.waitCond := NEW(UNTRACED REF pthread_cond_t);-    WITH r = Upthread.cond_init (t.waitCond^, NIL) DO <*ASSERT r=0*> END;+    t.waitCond := NewCond();     t.cond     := NEW(Condition);     FloatMode.InitThread (act.floatState);     AssignSlot (t);@@ -533,7 +548,7 @@       (* mark "self" done and clean it up a bit *)       self.completed := TRUE;       Broadcast(self.cond); (* let everybody know that "self" is done *)-      WITH r = Upthread.cond_destroy(self.waitCond^) DO <*ASSERT r=0*> END;+      WITH r = Upthread.cond_destroy(self.waitCond) DO <*ASSERT r=0*> END;       DISPOSE(self.waitCond);     END; Index: src/unix/Common/Uconstants.c===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/Common/Uconstants.c,vretrieving revision 1.15diff -u -r1.15 Uconstants.c--- src/unix/Common/Uconstants.c 3 Feb 2009 23:06:42 -0000 1.15+++ src/unix/Common/Uconstants.c 5 Feb 2009 18:04:01 -0000@@ -105,10 +105,14 @@  #undef X #define X(type, x) const type Upthread_##x = x;+#undef Y+#define Y(x, y) const size_t Upthread_##x = y;  X(pthread_mutex_t, PTHREAD_MUTEX_INITIALIZER) X(pthread_cond_t, PTHREAD_COND_INITIALIZER)-++Y(pthread_mutex_t_size, sizeof(pthread_mutex_t))+Y(pthread_cond_t_size, sizeof(pthread_cond_t))  #undef X #define X(x) const int Usocket_##x = x;Index: src/unix/Common/Upthread.i3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/Common/Upthread.i3,vretrieving revision 1.4diff -u -r1.4 Upthread.i3--- src/unix/Common/Upthread.i3 5 Feb 2009 09:47:09 -0000 1.4+++ src/unix/Common/Upthread.i3 5 Feb 2009 18:04:01 -0000@@ -7,11 +7,15 @@ FROM Ctypes IMPORT int; FROM Utypes IMPORT size_t; IMPORT Usysdep;++(*CONST*)+<*EXTERNAL "Upthread_pthread_mutex_t_size"*> VAR pthread_mutex_t_size: size_t;+<*EXTERNAL "Upthread_pthread_cond_t_size"*> VAR pthread_cond_t_size: size_t;  TYPE   pthread_t = Usysdep.pthread_t;-  pthread_mutex_t = Usysdep.pthread_mutex_t;-  pthread_cond_t = Usysdep.pthread_cond_t;+  pthread_mutex_t = UNTRACED REF ARRAY OF CHAR;+  pthread_cond_t = UNTRACED REF ARRAY OF CHAR;   pthread_key_t = Usysdep.pthread_key_t;    destructor_t = PROCEDURE(arg: ADDRESS);Index: src/unix/cygwin/Usysdep.i3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/cygwin/Usysdep.i3,vretrieving revision 1.13diff -u -r1.13 Usysdep.i3--- src/unix/cygwin/Usysdep.i3 5 Feb 2009 09:47:10 -0000 1.13+++ src/unix/cygwin/Usysdep.i3 5 Feb 2009 18:04:01 -0000@@ -28,8 +28,6 @@ (* INTERFACE Upthread; *)    pthread_t = ADDRESS; (* opaque *)-  pthread_mutex_t = ADDRESS; (* opaque *)-  pthread_cond_t = ADDRESS; (* opaque *)   pthread_key_t = ADDRESS; (* opaque *)  (* INTERFACE Usocket; *)Index: src/unix/darwin-common/Usysdep.i3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/darwin-common/Usysdep.i3,vretrieving revision 1.3diff -u -r1.3 Usysdep.i3--- src/unix/darwin-common/Usysdep.i3 5 Feb 2009 09:47:10 -0000 1.3+++ src/unix/darwin-common/Usysdep.i3 5 Feb 2009 18:04:01 -0000@@ -20,8 +20,6 @@ (* INTERFACE Upthread; *)    pthread_t = INTEGER; (* opaque *)-  pthread_mutex_t = RECORD opaque: ARRAY [1..11] OF INTEGER; END;-  pthread_cond_t = RECORD opaque: ARRAY [1..7] OF INTEGER; END;   pthread_key_t = INTEGER; (* opaque *)  (* INTERFACE Usocket; *)Index: src/unix/freebsd-common/Usysdep.i3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/freebsd-common/Usysdep.i3,vretrieving revision 1.6diff -u -r1.6 Usysdep.i3--- src/unix/freebsd-common/Usysdep.i3 5 Feb 2009 09:47:10 -0000 1.6+++ src/unix/freebsd-common/Usysdep.i3 5 Feb 2009 18:04:01 -0000@@ -22,8 +22,6 @@ (* INTERFACE Upthread; *)    pthread_t = ADDRESS;-  pthread_mutex_t = ADDRESS;-  pthread_cond_t = ADDRESS;   pthread_key_t = int;  (* INTERFACE Usocket; *)Index: src/unix/linux-common/Usysdep.i3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/linux-common/Usysdep.i3,vretrieving revision 1.11diff -u -r1.11 Usysdep.i3--- src/unix/linux-common/Usysdep.i3 5 Feb 2009 09:47:10 -0000 1.11+++ src/unix/linux-common/Usysdep.i3 5 Feb 2009 18:04:01 -0000@@ -21,8 +21,6 @@ (* INTERFACE Upthread; *)    pthread_t = ADDRESS;-  pthread_mutex_t = Upthreadtypes.pthread_mutex_t;-  pthread_cond_t = RECORD data: ARRAY[1..6] OF LONGINT; END;   pthread_key_t = uint32_t;  (* INTERFACE Usocket; *)Index: src/unix/openbsd-common/Usysdep.i3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/openbsd-common/Usysdep.i3,vretrieving revision 1.12diff -u -r1.12 Usysdep.i3--- src/unix/openbsd-common/Usysdep.i3 21 Jan 2009 15:25:13 -0000 1.12+++ src/unix/openbsd-common/Usysdep.i3 5 Feb 2009 18:04:01 -0000@@ -21,8 +21,6 @@    pthread_t = ADDRESS;   pthread_attr_t = ADDRESS;-  pthread_mutex_t = ADDRESS;-  pthread_cond_t = ADDRESS;   pthread_key_t = int;  (* INTERFACE Usocket; *)Index: src/unix/solaris-common/Usysdep.i3===================================================================RCS file: /usr/cvs/cm3/m3-libs/m3core/src/unix/solaris-common/Usysdep.i3,vretrieving revision 1.4diff -u -r1.4 Usysdep.i3--- src/unix/solaris-common/Usysdep.i3 5 Feb 2009 09:47:11 -0000 1.4+++ src/unix/solaris-common/Usysdep.i3 5 Feb 2009 18:04:01 -0000@@ -24,8 +24,6 @@ (* INTERFACE Upthread; *)      pthread_t = int32_t; (* opaque *)-    pthread_mutex_t = RECORD opaque: ARRAY [1..4] OF LONGINT; END; (* 32 bytes with 64 bit alignment *)-    pthread_cond_t = RECORD opaque: ARRAY [1..2] OF LONGINT; END; (* 16 bytes with 64 bit alignment *)     pthread_key_t = int32_t; (* opaque *)  (* INTERFACE Usocket; *)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20090205/63bf3c52/attachment-0002.html>


More information about the M3devel mailing list