/* * Archive writer callback routines for archive creation operation. */ static int arc_open_callback(struct archive *arc, void *ctx) { pc_ctx_t *pctx = (pc_ctx_t *)ctx; Sem_Init(&(pctx->read_sem), 0, 0); Sem_Init(&(pctx->write_sem), 0, 0); pctx->arc_buf = NULL; pctx->arc_buf_pos = 0; pctx->arc_buf_size = 0; return (ARCHIVE_OK); }
void LifecycleEventQueue_Init(_Out_ LifecycleEventQueue* queue) { queue->head = NULL; queue->tail = NULL; ReadWriteLock_Init ( &queue->lock ); Sem_Init( &queue->sem, SEM_USER_ACCESS_ALLOW_ALL, 0 ); }
/** * Initialize data structures and create waiting barber threads. */ static void setup(struct simulator *simulator) { struct chairs *chairs = &simulator->chairs; /* Setup semaphores*/ chairs->max = thrlab_get_num_chairs(); /* Both Front and Rear start and index 0 */ chairs->f = 0; chairs->r = 0; Sem_Init(&chairs->mutex, 0, 1); Sem_Init(&chairs->chair, 0, chairs->max); Sem_Init(&chairs->barber, 0, 0); /* Create chairs*/ chairs->customer = malloc(sizeof(struct customer *) * thrlab_get_num_chairs()); /* Create barber thread data */ simulator->barberThread = malloc(sizeof(pthread_t) * thrlab_get_num_barbers()); simulator->barber = malloc(sizeof(struct barber*) * thrlab_get_num_barbers()); /* Start barber threads */ struct barber *barber; for (unsigned int i = 0; i < thrlab_get_num_barbers(); i++) { barber = calloc(sizeof(struct barber), 1); barber->room = i; barber->simulator = simulator; simulator->barber[i] = barber; if(pthread_create(&simulator->barberThread[i], 0, barber_work, barber) < 0) { printf("Pthread create error"); exit(1); } if(pthread_detach(simulator->barberThread[i]) < 0) { printf("Pthread detach error"); exit(1); } } }
/** * 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); }