Exemplo n.º 1
0
/**
 * Free all used resources and end the barber threads.
 */
static void cleanup(struct simulator *simulator)
{
    /* Destroying all semaphores */
    struct chairs *chairs = &simulator->chairs;
    Sem_Destroy(&chairs->mutex);
    Sem_Destroy(&chairs->chair);
    Sem_Destroy(&chairs->barber);

    /* Free chairs */
    free(simulator->chairs.customer);

    /* Free barber thread data */
    free(simulator->barber);
    free(simulator->barberThread);
}
Exemplo n.º 2
0
void LifecycleEventQueue_Finalize(_Inout_ LifecycleEventQueue* queue)
{
    LifecycleEvent* event = LifecycleEventQueue_Remove(queue);
    while (event)
    {
        LifecycleEvent_Free(event);
        event = LifecycleEventQueue_Remove(queue);
    }

    Sem_Destroy( &queue->sem );
}
Exemplo n.º 3
0
/**
 * Called in a new thread each time a customer has arrived.
 */
static void customer_arrived(struct customer *customer, void *arg)
{
    struct simulator *simulator = arg;
    struct chairs *chairs = &simulator->chairs;

    Sem_Init(&customer->mutex, 0, 0);

    /* If a chair is available we let the customer in, else we turn the
     * customer away. */
    if (sem_trywait(&chairs->chair) == 0) {
        Sem_Wait(&chairs->mutex);
        thrlab_accept_customer(customer);
        /* Incrementing front by one and modulo max to return back to 0 if
         * the que is at max index. */
        chairs->customer[chairs->f++ % chairs->max] = customer;
        Sem_Post(&chairs->mutex);
        Sem_Post(&chairs->barber);
        Sem_Wait(&customer->mutex);
    } else {
        thrlab_reject_customer(customer);
    }
    /* Destroying the customer semaphore */
    Sem_Destroy(&customer->mutex);
}