Wait for event by polling with timeout

There is a polling API that checks whether an event has happened. I need to let a task wait for the event, and be able to timeout.

bool HasEventHappend();
bool WaitForEvent(TickType_t xTicksToWait) {
	TimeOut_t xTimeOut;
	vTaskSetTimeOutState(&xTimeOut);
	bool eventHappened;
	for (;;) {
		eventHappened = HasEventHappend();
		if (eventHappened) {
			break;
		} else if (xTaskCheckForTimeOut(&xTimeOut, &xTicksToWait) == pdTRUE) {
			break;
		} else {
			vTaskDelay(1);
		}
	}
	return eventHappened;
}

I notice the example code xTaskCheckForTimeOut() - FreeRTOS™ uses ulTaskNotifyTake with xTaskCheckForTimeOut, which seems odd to me, so I’m not sure whether I have used TimeOut functions correctly. Is my code correct? Thanks very much.

Yes, it seems correct. You may consider increasing the delay to avoid polling too often.