Example #1
0
void acl_fiber_rwlock_wunlock(ACL_FIBER_RWLOCK *lk)
{
	ACL_FIBER *fiber;
	
	if (lk->writer == NULL)
		acl_msg_fatal("%s(%d), %s: wunlock: not locked",
			__FILE__, __LINE__, __FUNCTION__);

	lk->writer = NULL;

	if (lk->readers != 0)
		acl_msg_fatal("%s(%d), %s: wunlock: readers",
			__FILE__, __LINE__, __FUNCTION__);

	while ((fiber = FIRST_FIBER(&lk->rwaiting)) != NULL) {
		acl_ring_detach(&lk->rwaiting);
		lk->readers++;
		acl_fiber_ready(fiber);
	}

	if (lk->readers == 0 && (fiber = FIRST_FIBER(&lk->wwaiting)) != NULL) {
		acl_ring_detach(&lk->wwaiting);
		lk->writer = fiber;
		acl_fiber_ready(fiber);
	}
}
Example #2
0
void acl_fiber_kill(ACL_FIBER *fiber)
{
	ACL_FIBER *curr = __thread_fiber->running;

	if (fiber == NULL) {
		acl_msg_error("%s(%d), %s: fiber NULL",
			__FILE__, __LINE__, __FUNCTION__);
		return;
	}

	if (curr == NULL) {
		acl_msg_error("%s(%d), %s: current fiber NULL",
			__FILE__, __LINE__, __FUNCTION__);
		return;
	}

	fiber->flag |= FIBER_F_KILLED;

	if (fiber == curr) {
		acl_msg_error("%s(%d), %s: fiber-%d kill itself disable!",
			__FILE__, __LINE__, __FUNCTION__, acl_fiber_id(fiber));
		return;
	}

	acl_ring_detach(&curr->me);
	acl_ring_detach(&fiber->me);
	acl_fiber_ready(fiber);
	acl_fiber_yield();
}
Example #3
0
void fiber::ready(fiber& f)
{
	ACL_FIBER *fb = f.get_fiber();

	if (fb)
		acl_fiber_ready(f.get_fiber());
}
Example #4
0
void acl_fiber_rwlock_runlock(ACL_FIBER_RWLOCK *lk)
{
	ACL_FIBER *fiber;

	if (--lk->readers == 0 && (fiber = FIRST_FIBER(&lk->wwaiting))) {
		acl_ring_detach(&lk->wwaiting);
		lk->writer = fiber;
		acl_fiber_ready(fiber);
	}
}
Example #5
0
int acl_fiber_yield(void)
{
	int  n;

	if (acl_ring_size(&__thread_fiber->ready) == 0)
		return 0;

	n = __thread_fiber->switched;
	acl_fiber_ready(__thread_fiber->running);
	acl_fiber_switch();

	return __thread_fiber->switched - n - 1;
}
Example #6
0
File: main.c Project: iYefeng/acl
static void io_timer(ACL_FIBER *fiber, void *ctx)
{
	FIBER_TIMER *ft = (FIBER_TIMER *) ctx;

	assert(fiber == ft->timer);

	acl_fiber_set_errno(ft->fiber, ETIMEDOUT);
	acl_fiber_keep_errno(ft->fiber, 1);

	printf("timer-%d wakeup, set fiber-%d, errno: %d, %d\r\n",
		acl_fiber_id(fiber), acl_fiber_id(ft->fiber),
		ETIMEDOUT, acl_fiber_errno(ft->fiber));

	acl_fiber_ready(ft->fiber);
}
Example #7
0
int acl_fiber_sem_post(ACL_FIBER_SEM *sem)
{
	ACL_FIBER *ready;

	if (sem->tid != acl_pthread_self())
		return -1;

	if ((ready = FIRST_FIBER(&sem->waiting)) == NULL) {
		sem->num++;
		return sem->num;
	}

	acl_ring_detach(&ready->me);
	acl_fiber_ready(ready);

	return sem->num;
}
Example #8
0
void acl_fiber_mutex_unlock(ACL_FIBER_MUTEX *lk)
{
	ACL_FIBER *ready, *curr = acl_fiber_running();
	
	if (lk->owner == NULL)
		acl_msg_fatal("%s(%d), %s: qunlock: owner NULL",
			__FILE__, __LINE__, __FUNCTION__);
	if (lk->owner != curr)
		acl_msg_fatal("%s(%d), %s: invalid owner=%p, %p",
			__FILE__, __LINE__, __FUNCTION__, lk->owner, curr);

	acl_ring_detach(&lk->me);
	ready = FIRST_FIBER(&lk->waiting);

	if ((lk->owner = ready) != NULL) {
		acl_ring_detach(&ready->me);
		acl_fiber_ready(ready);
	}
}
Example #9
0
ACL_FIBER *acl_fiber_create(void (*fn)(ACL_FIBER *, void *),
	void *arg, size_t size)
{
	ACL_FIBER *fiber = fiber_alloc(fn, arg, size);

	__thread_fiber->count++;

	if (__thread_fiber->slot >= __thread_fiber->size) {
		__thread_fiber->size += 128;
		__thread_fiber->fibers = (ACL_FIBER **) acl_myrealloc(
			__thread_fiber->fibers, 
			__thread_fiber->size * sizeof(ACL_FIBER *));
	}

	fiber->slot = __thread_fiber->slot;
	__thread_fiber->fibers[__thread_fiber->slot++] = fiber;

	acl_fiber_ready(fiber);

	return fiber;
}
Example #10
0
static void alt_exec(FIBER_ALT *a)
{
	FIBER_ALT_ARRAY *ar;
	FIBER_ALT *other;
	ACL_CHANNEL *c;
	int i;

	c = a->c;
	ar = channel_array(c, otherop(a->op));

	if (ar && ar->n) {
		i = rand() % ar->n;
		other = ar->a[i];
		alt_copy(a, other);
		alt_all_dequeue(other->xalt);
		other->xalt[0].xalt = other;

		acl_fiber_ready(other->fiber);
	 } else
		alt_copy(a, NULL);
}