[M3devel] further reducing cloned headers wrt pthread?

Jay jay.krell at cornell.edu
Sun Feb 1 15:44:20 CET 2009


I'd like to consider further reducing "system dependent cloned headers".
Upthread.i3 in particular.
 

We have for example:
 

  pthread_mutex_t = ARRAY[1..6] OF INTEGER;
 

Now, pthread_mutex_t is used in two ways.
 

Some statically allocated variables:
 

VAR
  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 *)
 

Some untraced heap allocated variables:
 

PROCEDURE InitMutex (m: Mutex) =
  VAR mutex: UNTRACED REF pthread_mutex_t;
  BEGIN
    TRY
      WITH r = Upthread.mutex_lock(initMu) DO  END;
      IF m.mutex # NIL THEN RETURN END;
      mutex := NEW(UNTRACED REF pthread_mutex_t);
      WITH r = Upthread.mutex_init(mutex^, NIL) DO  END;
      m.mutex := mutex;
    FINALLY
      WITH r = Upthread.mutex_unlock(initMu) DO  END;
    END;
    RTHeapRep.RegisterFinalCleanup (m, CleanMutex);
  END InitMutex;
 

The statically allocated variables can just be moved to C, trivial.
And their initialization then kept efficient.
 

What is the right way to translate the untraced allocation?
 

OR, here is kind of the opposite question:
 

Why is the mutex heap allocated anyway?
Why not embed it "by value"?
 

Granted, that makes it more difficult to smush out platform-specificity.
And then to address that:
 

I would kind of like the idea of an "extern type".
An extern type is a type whose instances may only be embedded within heap allocated objects,
possibly only at the end, possibly with an implied alignment=maximum, their size
would be exposed via constant C variable, probably they would have a designated initialization
function, or be zeroed. They could also be "standalone" heap allocated objects.
 

Specifically, something like this:

 
 TYPE pthread_t;
 

TYPE Thread.T = RECORD
  (* Modula-3 stuff *)
  pthread: pthread_t;
END;
Thread := NEW(Thread.T);
 

roughly equiv to
 Thread = malloc(offsetof(Thread.T, pthread_t) + sizeof(pthread_t)); 
 

pthread.c:
 

extern const size_t pthread_t_size = sizeof(pthread_t);
 

You know -- so that they can be implemented "portably".
Given that they are already opaque and all the Modula-3 code knows
is their size, alignment, and what functions they can be passed to.
 

 - Jay


More information about the M3devel mailing list