Exemplo n.º 1
0
// XXX: we want something faster than this
// and maybe want to recover from failure
char *new_buf(thread_t *thread, int size) {
	buf_t *buf = malloc(sizeof(buf_t) + size);
	if (!buf) fail(1, "allocating buffer");
	Q_INIT_ELEM(buf, gc_link);
	Q_INSERT_TAIL(&thread->bufs, buf, gc_link);
	return buf->buffer;
}
Exemplo n.º 2
0
void registerBlock(Block* block)
{
  Q_INSERT_TAIL(&blockList, block, blockLink);
  if (block->id >= allBlockSize) {
    //fprintf(stderr, "Reallocing to %d\n", allBlockSize*2);
    allBlocks = (Block**)realloc(allBlocks, allBlockSize*2*sizeof(Block*));
    allBlockSize = allBlockSize*2;
  }
  allBlocks[block->id] = block;
}
Exemplo n.º 3
0
// need to have the sched lock
void wakeup_sleepers(void) {
	struct timespec time;
	clock_gettime(CLOCK_REALTIME, &time);

	thread_t *t;
	while ((t = Q_GET_FRONT(&state.sleep_queue)) &&
	       time_lt(t->sleep_event.u.wakeup, time)) {
		Q_REMOVE(&state.sleep_queue, t, q_link);
		Q_INSERT_TAIL(&state.sched_queue, t, q_link);
	}
}
Exemplo n.º 4
0
void channel_send(channel_t *ch, int tag, data_t payload)
{
	msg_t *msg = calloc(1, sizeof(msg_t));
	if (!msg) fail(1, "allocating msg");
	msg->data.tag = tag;
	msg->data.payload = payload;

	rt_mutex_lock(&ch->lock);
	Q_INSERT_TAIL(&ch->msgs, msg, q_link);
	handle_event(&ch->ev);
	rt_mutex_unlock(&ch->lock);
}
Exemplo n.º 5
0
void register_sleep_event(thread_t *me, event_t *e)
{
	sched_lock();
	thread_t *t;
	Q_SEARCH(t, &state.sleep_queue, q_link,
	         time_lt(e->u.wakeup, t->sleep_event.u.wakeup));
	if (t) {
		Q_INSERT_BEFORE(&state.sleep_queue, t, me, q_link);
	} else {
		Q_INSERT_TAIL(&state.sleep_queue, me, q_link);
	}
	sched_unlock();
}
Exemplo n.º 6
0
event_t *mk_nb_event(thread_t *thread, int fd, int mode)
{
	event_t *e = mk_event();
	e->type = EVENT_NB;
	e->u.nb.fd = fd;
	e->u.nb.mode = mode;

	if (epoll_ctler(EPOLL_CTL_ADD, fd, 0, e) < 0)
		fail(1, "epoll_ctl: %d", fd);

	Q_INSERT_TAIL(&thread->nb_events, e, gc_link);

	return e;
}
Exemplo n.º 7
0
/** @brief Function that implements the wait for the condition variable
 *  
 *  Put the thread in the waiting list of the condition variable and 
 *  block the thread until it receives a signal to wake up.
 *
 *  @param cv condition variable that is waited on
 *  @param mp the mutex variable be acquired when signalled
 *  @return void 
 */
void
cond_wait (cond_t * cv, mutex_t * mp)
{
    int status = 0;
    assert (cv != NULL);
    assert (mp != NULL);

    /* If cond_variable has been destroyed, do nothing and return */
    if (cv->status == COND_DESTORY)
    {
        lprintf ("The cond_variable has been destroyed already");
        return;
    }

    /* Allocate memory for condition variable */
    cond_t *node_ptr = malloc (sizeof (cond_t));

    /* Initialize the struct to store the thread's it and 
    * prepare to insert */
    Q_INIT_ELEM (node_ptr, cond_link);
    node_ptr->tid = gettid ();

    /* Lock the queue to store the thread info */
    spin_lock_request (&cv->spinlock);

    /* Insert into the tail of the waiting list */
    Q_INSERT_TAIL (&cv->waiting_list, node_ptr, cond_link);

    /* Release the mutex mp passed in */
    mutex_unlock (mp);

    /* Release the lock of the queue */
    spin_lock_release (&cv->spinlock);

    /* Deschedule the thread who has inserted into the queue */
    deschedule (&status);

    /* Lock the passed mutex mp again */
    mutex_lock (mp);
    return;
}
Exemplo n.º 8
0
void make_runnable(thread_t *t)
{
	sched_lock();
	Q_INSERT_TAIL(&state.sched_queue, t, q_link);
	sched_unlock();
}