Ejemplo n.º 1
0
// setup data structures and UART of a terminal. Clear char queues,
// and preset the bounded buffer semaphore. Put UART to interrupt mode:
// clear IER, enable TXRDY & RXRDY for interrupts.
void TerminalInit(int term_num)
{
    int BAUD_RATE = 9600;
    int divisor = 115200 / BAUD_RATE;

// first setup our vars
    terminals[term_num].echo_mode = TRUE;
    terminals[term_num].missed_intr = TRUE;

// Use a pair of sems. One limits available space in the output queue
// (terminal display), the other limits chars that are typed from the
// terminal. As part of initialization, the count of the output queue
// is set to the capacity of the char queue
    terminals[term_num].out_sid = SemInit(0); // a circular q, capacity CHAR_Q_SIZE
    terminals[term_num].in_sid = SemInit(0);

    InitCharQ(&terminals[term_num].in_q);   // initially empty
    InitCharQ(&terminals[term_num].out_q);  // initially empty
    InitCharQ(&terminals[term_num].echo_q); // initially empty

// then setup the terminal for 7-E-1 at 9600 baud
// abbrevs:
// CFCR Char Format Control Reg, MSR Modem Status Reg, IIR Intr Indicator Reg
// MCR Modem Control Reg, IER Intr Enable Reg, LSR Line Status Reg
// ERXRDY Enable Recv Ready, ETXRDY Enable Xmit Ready
// LSR_TSRE Line Status Reg Xmit+Shift Regs Empty
    outportb(terminals[term_num].io_base + CFCR, CFCR_DLAB); // CFCR_DLAB is 0x80
    outportb(terminals[term_num].io_base + BAUDLO,LOBYTE(divisor));
    outportb(terminals[term_num].io_base + BAUDHI,HIBYTE(divisor));
    //outportb(terminals[term_num].io_base + CFCR, CFCR_PEVEN | CFCR_PENAB | CFCR_7BITS); //7-E-1
    outportb(terminals[term_num].io_base + CFCR, CFCR_8BITS); //8-N-1
    outportb(terminals[term_num].io_base + IER,0);
// raise DTR & RTS of the serial port to start read/write
    outportb(terminals[term_num].io_base + MCR, MCR_DTR | MCR_RTS | MCR_IENABLE);
    IO_DELAY();
    outportb(terminals[term_num].io_base + IER, IER_ERXRDY | IER_ETXRDY);
    IO_DELAY();
// A null-terminated test message is sent to display to the terminal.
// "ESC *" will clear screen on TVI 910, but we use newlines for portability
    //StdoutString(term_num, "\n\n\nWill I dream, Dave? (2001 Space Odyssey)\n\n\n\0");
}
Ejemplo n.º 2
0
//----------------------------------------------------------------------
// 	SemCreate
//
//	Grabs a Semaphore, initializes it and returns a handle to this
//	semaphore. All subsequent accesses to this semaphore should be made
//	through this handle.  Returns SYNC_FAIL on failure.
//----------------------------------------------------------------------
sem_t SemCreate(int count) {
  sem_t sem;
  uint32 intrval;

  // grabbing a semaphore should be an atomic operation
  intrval = DisableIntrs();
  for(sem=0; sem<MAX_SEMS; sem++) {
    if(sems[sem].inuse==0) {
      sems[sem].inuse = 1;
      break;
    }
  }
  RestoreIntrs(intrval);
  if(sem==MAX_SEMS) return SYNC_FAIL;

  if (SemInit(&sems[sem], count) != SYNC_SUCCESS) return SYNC_FAIL;
  return sem;
}
Ejemplo n.º 3
0
/* initialisation de la liste chainee
*  avec un seul bloc libre de taille passee en parametre
*/
void mem_init(size_t taille)
{
	if(SemInit(&sem, 1) == -1)
	{
		PutString("[mem.c] erreur sem init\n");
		Exit(0);
	}
	taille_memoire = taille;
	char* mem = (char*)Mmap(taille);
	if(mem == NULL)
	{
		PutString("[mem.c] erreur de recuperation de memoire\n");
		Exit(1);
	}
	// stockage du pointeur du tableau memoire afin de placer la premiere structure
	head = (struct fb*) mem;
	// sauvegarde du debut du tableau et de la taille totale de la memoire dans des variables globales
	d_memoire = mem;
	head -> size = taille;
	head -> next = NULL;
	// initialisation de l'alignement
	alignement = sizeof(void*);
    mem_fit(mem_fit_first);
}
Ejemplo n.º 4
0
int main()
{
	PutString("\n-----------------------------------------\n");
	PutString("Lancement du test testBcpThreads : \n");
	PutString("Lance plusieurs threads affichant un nombre different avec semaphore pour l'acces.\n");
	PutString("-----------------------------------------\n");
	int i;
	int nb[NB_THREADS];
	SemInit(&s, 1);
	for(i = 0 ; i < NB_THREADS ; i++)
	{
		nb[i] = i;
		if(UserThreadCreate(f, &nb[i]) == -1)
		{
			SemWait(&s);
			PutString("Echec de creation du thread : ");
			PutInt(i);
			PutString("\n");
			SemPost(&s);
		}
	}
	PutString("thread main se termine\n");
    return 0;
}
Ejemplo n.º 5
0
HSEM threadlib_create_sem()
{
	HSEM s;
	SemInit(s);
	return s;
}
Ejemplo n.º 6
0
TInt CTestSemtrywait::TestSem392( )
	{
	
	int errsum=0, err = 0;
	int retval = 0;
	ThreadData lThreadData;

	sem_t lSignalSemaphore;
	sem_t lSuspendSemaphore;

	sem_t           lTestSemaphore;
	pthread_mutex_t lTestMutex;
	pthread_cond_t  lTestCondVar;
	pthread_condattr_t  lCondAttr;
	pthread_mutexattr_t lTestMutexAttr;

	pthread_mutexattr_t defaultattr;
	pthread_mutexattr_t errorcheckattr;
	pthread_mutexattr_t recursiveattr;

	pthread_mutexattr_init(&defaultattr);
	pthread_mutexattr_init(&errorcheckattr);
	pthread_mutexattr_init(&recursiveattr);

	pthread_mutexattr_settype(&errorcheckattr,PTHREAD_MUTEX_ERRORCHECK);
	pthread_mutexattr_settype(&recursiveattr,PTHREAD_MUTEX_RECURSIVE);


	pthread_mutex_t l_staticmutex = PTHREAD_MUTEX_INITIALIZER;
	pthread_mutex_t l_errorcheckmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
	pthread_mutex_t l_recursivemutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
    pthread_cond_t  l_staticcondvar = PTHREAD_COND_INITIALIZER;


    CommonData lCommonData;
    lCommonData.iStaticMutex = &l_staticmutex;
	lCommonData.iErrorCheckMutex = &l_errorcheckmutex;
	lCommonData.iRecursiveMutex = &l_recursivemutex;
	lCommonData.iStaticCondVar = &l_staticcondvar;

	retval = sem_init(&lSignalSemaphore,0,0);
	if(retval != 0)
		{
		return retval;
		}

	retval = sem_init(&lSuspendSemaphore,0,0);
	if(retval != 0)
		{
		return retval;
		}

	lThreadData.iSignalSemaphore = &lSignalSemaphore;
	lThreadData.iSuspendSemaphore = &lSuspendSemaphore;
	lThreadData.iTestSemaphore   = &lTestSemaphore;
	lThreadData.iTestMutex       = &lTestMutex;
	lThreadData.iTestMutexAttr   = &lTestMutexAttr;
	lThreadData.iTestCondVar     = &lTestCondVar;
	lThreadData.iDefaultAttr     = &defaultattr;
	lThreadData.iErrorcheckAttr = &errorcheckattr;
	lThreadData.iRecursiveAttr   = &recursiveattr;

	lThreadData.iCondAttr        = &lCondAttr;
	for (int loop = 0; loop < EThreadMain; loop++)
		{
	    g_spinFlag[loop] = true;
		}
	lThreadData.iSuspending      = false;
	lThreadData.iSpinCounter     = 0;
	lThreadData.iCurrentCommand  = -1;
	lThreadData.iSelf            = EThreadMain;
	lThreadData.iValue           = 0;
	lThreadData.iRetValue        = 0;
	lThreadData.ierrno           = 0;
	lThreadData.iExpectederrno   = 0;
	lThreadData.iTimes           = 0;
	lThreadData.iStopped         = false;
	lThreadData.iCommonData      = &lCommonData;

	retval = SemInit(&lThreadData);
	
	fp=func2;
	retval = ThreadCreate(&lThreadData, (void*) EThread1);
	
	fp=func4;
	retval = ThreadCreate(&lThreadData, (void*) EThread2);

	retval = SemPost(&lThreadData);
	retval = ThreadDestroy(&lThreadData, (void*) EThread1);
	retval = ThreadDestroy(&lThreadData, (void*) EThread2);
	retval = SemDestroy(&lThreadData);
	StopThread(&lThreadData);
	
	err = pthread_cond_destroy(&l_staticcondvar);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_recursivemutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_errorcheckmutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutex_destroy(&l_staticmutex);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = pthread_mutexattr_destroy(&recursiveattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
	err = pthread_mutexattr_destroy(&errorcheckattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
	err = pthread_mutexattr_destroy(&defaultattr);
	if(err != EINVAL)
		{
		errsum += err;
		}
    err = sem_destroy(&lSignalSemaphore);
	if(err != EINVAL)
		{	
		errsum += err;
		}
	err = sem_destroy(&lSuspendSemaphore);
	if(err != EINVAL)
		{
		errsum += err;
		}

	return retval+errsum;
	
	}
Ejemplo n.º 7
0
int main (int argc, char *argv []) {
    key_t key = ftok (argv [0], 0);
    semid = semget (key, nsems, 0644 | IPC_CREAT | IPC_EXCL);
    
    if (semid >= 0) { //got it first
        SemInit();
    }
    else if (errno == EEXIST) {  //someone else got it first
        
        semid = semget (key, nsems, 0);
        if (semid < 0) {
            fprintf (stderr, "Failed to open semaphore\n");
            semctl (semid, 0, IPC_RMID, NULL);
            shmdt (shared);
            exit(EXIT_FAILURE);
        }
        unsigned short *ptr = (unsigned short *) 
                            calloc (nsems, sizeof (unsigned short));
        semctl (semid, mutex, GETALL, ptr);
        if (ptr [mutex] != 1 || ptr [full] != 0 || ptr [empty] != buf_num) {
            SemInit();
        }
    }
    else {
            semctl (semid, 0, IPC_RMID, NULL);
            shmdt (shared);
            perror("\nFailed to create semaphore.");
            semctl (semid, 0, IPC_RMID, NULL);
            shmdt (shared);
            exit(EXIT_FAILURE);
    }
    
    int shmid = shmget (key, sizeof (in_data), 0644 | IPC_CREAT);
    if (shmid == -1) {
        perror("\nFailed to allocate shared memory.");
        semctl (semid, 0, IPC_RMID, NULL);
        shmdt (shared);
        exit(EXIT_FAILURE);
    }
    shared = (in_data*) shmat (shmid, NULL, 0);
    
    if (argc > 1) {
        SemOpMul (semid, ping1,  1, SEM_UNDO);
        SemOpMul (semid, ping2,  1, SEM_UNDO);
        SemOpMul (semid, init1, -1, SEM_UNDO);
        SemOpMul (semid, init2,  0, 0);
        //writer (Producer)
        int src = open (argv [1], O_RDONLY, 0);
        int ret_num = 1;
        while (ret_num > 0) {
            SemOp ('P', semid, empty, 0, ping1);
            SemOp ('P', semid, mutex, 0, ping1);
            ret_num = read (src, shared -> buffer, buf_size);
            shared -> num = ret_num;
            SemOp ('V', semid, mutex, 0, ping1);
            SemOp ('V', semid, full,  0, ping1);
        }
    } 
    else {
        //reader (Consumer)
        SemOpMul (semid, ping2, 1, SEM_UNDO);
        SemOpMul (semid, ping1, 1, SEM_UNDO);
        SemOpMul (semid, init2, -1, SEM_UNDO);
        SemOpMul (semid, init1,  0, 0);
        while (1) {
            SemOp ('P', semid, full,  0, ping2);
            SemOp ('P', semid, mutex, 0, ping2);
            write (1, shared -> buffer, shared -> num);
            fflush (stdout);
            SemOp ('V', semid, mutex, 0, ping2);
            SemOp ('V', semid, empty, 0, ping2);
            
        }
    }
    semctl (semid, 0, IPC_RMID, NULL);
    shmdt (shared);
    return 0;
}