示例#1
0
文件: sema.c 项目: 32bitmicro/zephyr
/**
 *
 * @brief Test semaphore signaling from fiber
 *
 * Routine starts a fiber and does the following tests:
 * - fiber signals the semaphore N times, task checks that task_sem_count_get is N
 * - task waits on a semaphore and fiber signals it
 * - task waits on a semaphore group and fiber signals each of them once. Task
 *   checks which of the semaphores has been signaled
 *
 * See also: testFiber.c
 *
 * @return TC_PASS on success or TC_FAIL on failure
 */
static int simpleFiberSemTest(void)
{
	int signalCount;
	int status;
	int i;
	ksem_t  sema;

	task_sem_reset(simpleSem);
	task_sem_group_reset(semList);

	/* let the fiber signal the semaphore and wait on it */
	releaseTestFiber();
	status = task_sem_take(simpleSem, OBJ_TIMEOUT);
	if (status != RC_OK) {
		TC_ERROR("task_sem_take() error.  Expected %d, got %d\n",
				 RC_OK, status);
		return TC_FAIL;
	}

	/* release the fiber and let it signal the semaphore N_TESTS times */
	releaseTestFiber();
	signalCount = task_sem_count_get(simpleSem);
	if (signalCount != N_TESTS) {
		TC_ERROR("<signalCount> error.  Expected %d, got %d\n",
				 N_TESTS, signalCount);
		return TC_FAIL;
	}

	/* wait on the semaphore group while the fiber signals each semaphore in it */
	for (i = 0; semList[i] != ENDLIST; i++) {
		releaseTestFiber();
		sema = task_sem_group_take(semList, OBJ_TIMEOUT);
		if (sema != semList[i]) {
			TC_ERROR("task_sem_group_take() error.  Expected %d, not %d\n",
					 (int) semList[i], (int) sema);
			return TC_FAIL;
		}
	}
	return TC_PASS;
}
示例#2
0
文件: sema.c 项目: 32bitmicro/zephyr
int simpleSemaTest(void)
{
	int  signalCount;
	int  i;
	int  status;

	/*
	 * Signal the semaphore several times from an ISR.  After each signal,
	 * check the signal count.
	 */

	for (i = 0; i < 5; i++) {
		trigger_isrSemaSignal(simpleSem);

		task_sleep(10);     /* Time for low priority task to run. */
		signalCount = task_sem_count_get(simpleSem);
		if (signalCount != (i + 1)) {
			TC_ERROR("<signalCount> error.  Expected %d, got %d\n",
					 i + 1, signalCount);
			return TC_FAIL;
		}
	}

	/*
	 * Signal the semaphore several times from a task.  After each signal,
	 * check the signal count.
	 */

	for (i = 5; i < 10; i++) {
		task_sem_give(simpleSem);

		signalCount = task_sem_count_get(simpleSem);
		if (signalCount != (i + 1)) {
			TC_ERROR("<signalCount> error.  Expected %d, got %d\n",
					 i + 1, signalCount);
			return TC_FAIL;
		}
	}

	/*
	 * Test the semaphore without wait.  Check the signal count after each
	 * attempt (it should be decrementing by 1 each time).
	 */

	for (i = 9; i >= 4; i--) {
		status = task_sem_take(simpleSem, TICKS_NONE);
		if (status != RC_OK) {
			TC_ERROR("task_sem_take(SIMPLE_SEM) error.  Expected %d, not %d.\n",
					 RC_OK, status);
			return TC_FAIL;
		}

		signalCount = task_sem_count_get(simpleSem);
		if (signalCount != i) {
			TC_ERROR("<signalCount> error.  Expected %d, not %d\n",
					 i, signalCount);
			return TC_FAIL;
		}
	}

	task_sem_reset(simpleSem);

	/*
	 * The semaphore's signal count should now be zero (0).  Test the
	 * semaphore without wait.  It should fail.
	 */

	for (i = 0; i < 10; i++) {
		status = task_sem_take(simpleSem, TICKS_NONE);
		if (status != RC_FAIL) {
			TC_ERROR("task_sem_take(SIMPLE_SEM) error.  Expected %d, got %d.\n",
					 RC_FAIL, status);
			return TC_FAIL;
		}

		signalCount = task_sem_count_get(simpleSem);
		if (signalCount != 0) {
			TC_ERROR("<signalCount> error.  Expected %d, not %d\n",
					 0, signalCount);
			return TC_FAIL;
		}
	}

	return TC_PASS;
}
示例#3
0
文件: sema.c 项目: 32bitmicro/zephyr
int simpleGroupTest(void)
{
	int     i;
	int     j;
	int     status;
	ksem_t  value;

	/* Ensure that the semaphores in the group are reset */

	task_sem_group_reset(semList);

	for (i = 0; semList[i] != ENDLIST; i++) {
		status = task_sem_count_get(semList[i]);
		if (status != 0) {
			TC_ERROR("task_sem_count_get() returned %d not %d\n", status, 0);
			return TC_FAIL;
		}
	}

	/* Timeout while waiting for a semaphore from the group */

	value = task_sem_group_take(semList, OBJ_TIMEOUT);
	if (value != ENDLIST) {
		TC_ERROR("task_sem_group_take() returned %d not %d\n",
				 value, ENDLIST);
		return TC_FAIL;
	}

	/* Signal the semaphores in the group */

	for (i = 0; i < 10; i++) {
		task_sem_group_give(semList);

		for (j = 0; semList[j] != ENDLIST; j++) {
			status = task_sem_count_get(semList[j]);
			if (status != i + 1) {
				TC_ERROR("task_sem_count_get() returned %d not %d\n",
						 status, i + 1);
				return TC_FAIL;
			}
		}
	}

	/* Get the semaphores */

	for (i = 9; i >= 5; i--) {
		value = task_sem_group_take(semList, 0);

		for (j = 0; semList[j] != ENDLIST; j++) {
			status = task_sem_count_get(semList[j]);
			if (status != (value == semList[j] ? i : 10)) {
				TC_ERROR("task_sem_count_get(0x%x) returned %d not %d\n",
						 semList[j], status, (value == semList[j]) ? i : 10);
				return TC_FAIL;
			}
		}
	}

	/* Reset the semaphores in the group */

	task_sem_group_reset(semList);

	for (i = 0; semList[i] != ENDLIST; i++) {
		status = task_sem_count_get(semList[i]);
		if (status != 0) {
			TC_ERROR("task_sem_count_get() returned %d not %d\n", status, 0);
			return TC_FAIL;
		}
	}

	return TC_PASS;
}
示例#4
0
int pipePutTestWork(SIZE_EXPECT *singleItems, int nSingles,
					SIZE_EXPECT *manyItems, int nMany)
{
	int  rv;                     /* return code from task_pipe_put() */
	int  i;                      /* loop counter */
	int  bytesWritten;           /* # of bytes sent to pipe */
	int nitem;           /* expected item number processed by the other task */

	task_sem_reset(counterSem);

	for (i = 0; i < nSingles; i++) {
		rv = task_pipe_put(pipeId, txBuffer, singleItems[i].size,
						   &bytesWritten, singleItems[i].options);
		if (rv != singleItems[i].rcode) {
			TC_ERROR("task_pipe_put(%d) : Expected %d not %d.\n"
					 "    bytesWritten = %d, Iteration: %d\n",
					 singleItems[i].size, singleItems[i].rcode,
					 rv, bytesWritten, i + 1);
			return TC_FAIL;
		}

		if (bytesWritten != singleItems[i].sent) {
			TC_ERROR("task_pipe_put(%d) : "
					 "Expected %d bytes to be written, not %d\n",
					 singleItems[i].size, singleItems[i].sent, bytesWritten);
			return TC_FAIL;
		}

		task_sem_give(altSem);
		(void)task_sem_take_wait(regSem);

		nitem = task_sem_count_get(counterSem) - 1;
		if (nitem != i) {
			TC_ERROR("Expected item number is %d, not %d\n",
					 i, nitem);
			return TC_FAIL;
		}
	}

	/* This time, more than one item will be in the pipe at a time */

	task_sem_reset(counterSem);

	for (i = 0; i < nMany; i++) {
		rv = task_pipe_put(pipeId, txBuffer, manyItems[i].size,
						   &bytesWritten, manyItems[i].options);
		if (rv != manyItems[i].rcode) {
			TC_ERROR("task_pipe_put(%d) : Expected %d not %d.\n"
					 "    bytesWritten = %d, iteration: %d\n",
					 manyItems[i].size, manyItems[i].rcode,
					 rv, bytesWritten, i + 1);
			return TC_FAIL;
		}

		if (bytesWritten != manyItems[i].sent) {
			TC_ERROR("task_pipe_put(%d) : "
					 "Expected %d bytes to be written, not %d\n",
					 manyItems[i].size, manyItems[i].sent, bytesWritten);
			return TC_FAIL;
		}
	}

	task_sem_give(altSem);   /* Wake the alternate task */
	/* wait for other task reading all the items from pipe */
	(void)task_sem_take_wait(regSem);

	if (task_sem_count_get(counterSem) != nMany) {
		TC_ERROR("Expected number of items %d, not %d\n",
				 nMany, task_sem_count_get(counterSem));
		return TC_FAIL;
	}

	return TC_PASS;
}