gmisc.c 1.95 KB
#ifdef NOT_NEEDED
#include "gdisplay.h"
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include "ms_sync.h"
#include "vcsuser.h"

#define N_CHANNELS 16

static char * sh = 0;
static int s0 = 0;

gb_init(a,b)
{
	int i,n,size,key;

        key = tf_getp(3);
	size = tf_getp(2);
	n = tf_getp(1);
#ifdef DEBUG
	fprintf(stderr,"gb_init(n = %d, size = %d, key = %d)\n", n, size, key);
#endif
	s0 = shmget(key,size+sizeof(ms_sync_t),IPC_CREAT|0600);
	if(s0 == -1)
	{
	    perror("Error getting shared memory\n");
	    exit(1);
	}
	sh = shmat(s0,0,0);
        if((int)sh == -1)
        {
	  perror("Error attaching to shared memory");
	  exit(1);
        }
	ms = (ms_sync_t *)((long)sh+size);
#ifndef __sgi__
	ms->key = key;
#endif
	if(n>=0)
	    ms_init_slave(n);
	else
	    ms_init_master(-n-1);
}

gb_get(a,b)
{
	struct t_tfexprinfo ee;
	long ptr;
	int i,n;

	ptr = tf_getp(2);
	tf_exprinfo(1,&ee);
	n = ee.expr_ngroups;
#ifdef DEBUG
	fprintf(stderr,"gb_get(n = %d, ptr = %d)\n", n, ptr);
#endif
	for(i=0;i<n;i++)
	{
		ee.expr_value_p[i].avalbits = *(int *)((long)sh+ptr+i*4);
		ee.expr_value_p[i].bvalbits = 0;
#ifdef DEBUG
	        fprintf(stderr,"gb_get(avalbits = %d, bvalbits = %d)\n", 
			ee.expr_value_p[i].avalbits, ee.expr_value_p[i].bvalbits);
#endif
	}
	tf_propagatep(1);
}

gb_put(a,b)
{
	struct t_tfexprinfo ee;
	long ptr;
	int i,n;

	ptr = tf_getp(1);
	tf_exprinfo(2,&ee);
	n = ee.expr_ngroups;
#ifdef DEBUG
	fprintf(stderr,"gb_put(n = %d, ptr = %d)\n", n, ptr);
#endif
	for(i=0;i<n;i++)
        {
		*(int *)((long)sh+ptr+i*4) = ee.expr_value_p[i].avalbits;
#ifdef DEBUG
	        fprintf(stderr,"gb_put(val = %d)\n", ee.expr_value_p[i].avalbits);
#endif
        }
}

gb_sync(a,b)
{
	int a0;

	a0 = tf_getp(1);
#ifdef DEBUG
	fprintf(stderr,"gb_sync(a0 = %d)\n", a0);
#endif
	if(a0>=0)
	    ms_slave_sync(a0);
	else
	    ms_master_sync(-a0-1);
}

int gb_rmid(a,b)
{
	if(sh)
		shmdt(sh);
	if(s0>=0)
		shmctl(s0,IPC_RMID,0);
}