This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Suggestion for gcc additions: process/thread synchronization


hello gcc team,

I have an idea for one thing that could be added to gcc c language. May be 
this could be turned on by special switches. It concerns apllication 
synchronization usually done by mutexes and semaphores.
"My" idea was to introduce a new keyword (this was once implemented in modula 
2 as far as I know):

region(s) var;

Where region means a variable that will be automatically accessed by using 
mutexes. Example:

lock(&s)
int var=12345678;
unlock(&s);

Where lock() can be something like:

if (s==1) sleep();
s=1;

And unlock():

if (s==0) sleep;
s=0;

For such simple operations we could use processor specific locked opcodes. 
(TestAndSet, CompareExchange).

A more advanced use will be, to combine region with peaces of code that will 
be accessed atomically:

region (s) {
  int var;
  var++;
}

And to use this mutexes process wide we could combine s with shared memory:

#define shared  __attribute__((section(".shared"),shared))

shared region (s) {
}

This will allocate s in shared memory, so every other process that has access 
to this memory is able to use mutex (variable) s.
May be, to be more c language compatible we could specify the size of s:

shared region (int s) {
}

or

shared int s;
region (s) {
}


Type definitions with region:

shared int s,t;
typedef region(s) tagMonitor {
  int                   var;
  void*                 FunctionPointer;
  region(s) *tagMonitor next;
  region(t) *tagMonitor other;
} Monitor;

Monitor MyMon;

MyMon.var=1;
(void) MyMon.FunctionPointer();

So, every item in Monitor will be accessed atomically, either thread wide, or 
process wide if region is combined with shared.
May be we could combine region with conditionals:

shared int s;
region (s; s=10) var;

This is equivalent with:

if (s==10) sleep();
s++;

Or to be more complex:

shared int s;
int a,b,c;

region(s; s=4; a=1; b=2; c=3) {
  a=a+1;
  b=b+2;
  c=c+3;
}


-- Lock and Unlock that will be then used --

void lock(int s, ...) {
  ...
  if (s==1 && s==vararg[0]) sleep();
  s++;
}

void unlock(int s) {
  if (s==0) sleep;
  s--;
}

-- will be implemented as ---

if (a==1 && b==2 && c==3) {
  lock(s,4);
  a=a+1;
  b=b+2;
  c=c+3;
  unlock(s);
}

I hope this was clear enough to understand. The advantage by using compiler 
generated synchronization is that it is less errorneus.
If for example lock(s) is called without calling unlock(s), or unlock(s) is 
called without calling lock(s) before, this could cause undefined errors.

best regards

Benjamin Kalytta


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]