
/* cift11-c.c -- CIF test 11, C part */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>

#include "cifccsp.h"

/*
 * note: the current organisation of channel-types has
 * the reference-count in the first word, followed by the
 * declared channels in order.  also relies on gcc allocating
 * the structure in the order given..
 */

struct chantype_LINK;

typedef struct chantype_LINK {
	int refcount;
#ifdef CHANTYPEDESC
	unsigned int *typedesc;
	void *ihook;
#endif
	Channel in;
	Channel out;
	CTSem clisem;
	CTSem svrsem;
} ct_LINK;


void my_process (Process *me, ct_LINK *out)
{
	int i;

	for (i=99; i>=95; i--) {
		CTSemClaim (&(out->clisem));
		ChanOutInt (&(out->in), i);
		CTSemRelease (&(out->clisem));
	}

	/* ought to tidy up properly */
	out->refcount--;
	if (!out->refcount) {
		DMemFree (out);
	}
	out = NULL;

	return;
}


/* allocates process and returns address to occam world */
void real_cift11_init (int *raddr, ct_LINK *out)
{
	Process *p = ProcAlloc (my_process, 65536, 1, out);

	*raddr = (int)p;
	return;
}

/* frees memory used by a process -- lets hope it's finished :) */
void real_cift11_free (int addr)
{
	Process *p = (Process *)addr;

	ProcAllocClean (p);
	return;
}

/* better make sure we "move" the channel-end into this process */
void _cift11_init (int *ws) { real_cift11_init ((int *)(ws[0]), (ct_LINK *)(ws[1])); ws[1] = 0;}
void _cift11_free (int *ws) { real_cift11_free ((int)(ws[0])); }

