예제 #1
0
void take_chopsticks(int i, shared_data_t *data)
{
    fprintf(stderr, "Philosopher %d is taking chopsticks %d and %d\n", i, i, (i+1)%NPHILOSOPHERS);
    if (i == (NPHILOSOPHERS-1))
    {
        ta_sem_wait(&data->chopstickSem[0]);
        ta_sem_wait(&data->chopstickSem[i]);
    }
    else
    {
        ta_sem_wait(&data->chopstickSem[i]);
        ta_sem_wait(&data->chopstickSem[i+1]);
    }
}
예제 #2
0
void producer(void *vptr)
{
    shared_data_t *data = (shared_data_t*)vptr;

    while (!data->stop)
    {
	ta_yield();
	ta_sem_wait(&(data->emptyBuffer));

        // produce
        ta_sem_wait(&(data->pmutex));

        ta_yield();
        printf("producer filling slot %d\n", data->pindex);
        data->buffer[data->pindex] = pcount++;
        data->pindex = (data->pindex + 1) % BUFFERSIZE;

        ta_sem_signal(&(data->pmutex));
	ta_sem_signal(&(data->fullBuffer));
    }
}
예제 #3
0
void consumer(void *vptr)
{
    shared_data_t *data = (shared_data_t*)vptr;

    while (!data->stop)
    {
	ta_yield();
	ta_sem_wait(&(data->fullBuffer));

        // consume
        ta_sem_wait(&(data->cmutex));

        printf("consumer eating slot %d\n", data->cindex);
        ta_yield();

	assert(data->buffer[data->cindex] == ccount);
        ccount++;
        data->cindex = (data->cindex + 1) % BUFFERSIZE;

        ta_sem_signal(&(data->cmutex));
	ta_sem_signal(&(data->emptyBuffer));
    }
}
예제 #4
0
void reader(void *arg)
{
    int tid = (int)arg;
    int val = 0;
    while (!stop) {
        ta_sem_wait(&writersem);
        ta_lock(&wmutex);
        int loc = readerloc;
        readerloc = (readerloc+1) % datalen;
        ta_unlock(&wmutex);
        val = data[loc];
        ta_sem_post(&readersem);
        fprintf(stderr, "reader %d read location %d\n", tid, loc);

        if (random() % 2 == 0)
            ta_yield();
    }
}
예제 #5
0
void philosophize(void *vptr)
{
    shared_data_t *data = (shared_data_t*)vptr;
    int philosopher_index = 0;

    ta_sem_wait(&data->atTableSem);
    philosopher_index = data->atTable++; 
    ta_sem_signal(&data->atTableSem);
    fprintf(stderr, "Philosopher %d has been seated\n", philosopher_index);

    while (!data->stop)
    {
        think(philosopher_index);
        take_chopsticks(philosopher_index, data);
        eat(philosopher_index);
        release_chopsticks(philosopher_index, data);
    }
}
예제 #6
0
void writer(void *arg)
{
    fprintf(stderr,"in writer\n");
    int tid = (int)arg;
    int val = 1000000;
    int writerloc = 0;
    while (!stop) {
        ta_sem_wait(&readersem);
        ta_lock(&rmutex);
        int loc = writerloc;
        writerloc = (writerloc+1) % datalen;
        ta_unlock(&rmutex);
        data[loc] = val++;
        ta_sem_post(&writersem);
        fprintf(stderr, "writer %d wrote location %d\n", tid, loc);

        if (random() % 2 == 0)
            ta_yield();
    }
}
예제 #7
0
void producer(void *vptr)
{
    shared_data_t *data = (shared_data_t*)vptr;
    int i = 0;

    while (!data->stop)
    {
	ta_yield();
	ta_sem_wait(&(data->emptyBuffer));

        // produce
	assert(data->buffer[i] == 0xFEEDFEED);
        ta_yield();
        printf("producer filled slot %d\n", i);
	data->buffer[i] = 0xDEADBEEF;

	i = (i+1)%BUFFERSIZE;
	ta_sem_signal(&(data->fullBuffer));
    }
}
예제 #8
0
void consumer(void *vptr)
{
    shared_data_t *data = (shared_data_t*)vptr;
    int j = 0;

    while (!data->stop)
    {
	ta_yield();
	ta_sem_wait(&(data->fullBuffer));

        // consume
	assert(data->buffer[j] == 0xDEADBEEF);
	data->buffer[j] = 0xFEEDFEED;
        ta_yield();
        printf("consumer read slot %d\n", j);

	j = (j+1)%BUFFERSIZE;

	ta_sem_signal(&(data->emptyBuffer));
    }
}
예제 #9
0
void ta_lock(talock_t *mutex) {

	ta_sem_wait(&mutex->binary_sem);
}