library/src/events/BP-Events.cc
// Pickup event from main loop.
Pickup event from main loop.
Pickup event from main loop .
P_BP_EVENT BP_EventMgrPickupEventFromMainLoop
(
		P_BP_EVENT_MANAGER      mgr,
		BP_EVENT_MESSAGE_IN_OUT pickup_from_queue,
		BP_EVENT_PRIORITY       event_priority
)
{
	if(!mgr)
		return NULL;
	if(!pickup_from_queue)
		return NULL;
	if(!event_priority)
		return NULL;
	BP_GHT_ENTER_CRITICAL_SECTION;
	char * priority_selected = NULL;
	switch(event_priority)
	{
		case BP_EVENT_PRIORITY_VERY_LOW:
			priority_selected = "BP_EVENT_PRIORITY_VERY_LOW";
			break;
		case BP_EVENT_PRIORITY_LOW:
			priority_selected = "BP_EVENT_PRIORITY_LOW";
			break;
		case BP_EVENT_PRIORITY_MID_NORMAL:
			priority_selected = "BP_EVENT_PRIORITY_MID_NORMAL";
			break;
		case BP_EVENT_PRIORITY_NORMAL:
			priority_selected = "BP_EVENT_PRIORITY_NORMAL";
			break;
		case BP_EVENT_PRIORITY_MID_HIGH:
			priority_selected = "BP_EVENT_PRIORITY_MID_HIGH";
			break;
		case BP_EVENT_PRIORITY_HIGH:
			priority_selected = "BP_EVENT_PRIORITY_HIGH";
			break;
		case BP_EVENT_PRIORITY_SYSTEM:
			priority_selected = "BP_EVENT_PRIORITY_SYSTEM";
			break;
		case BP_EVENT_PRIORITY_CRITICAL_SYSTEM:
			priority_selected = "BP_EVENT_PRIORITY_CRITICAL_SYSTEM";
			break;
		default:
			BP_EVENTS_SEM_UNLOCK(mgr->lock);
			BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	if(!priority_selected)
	{
		BP_EVENTS_SEM_UNLOCK(mgr->lock);
		BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	char * table_path_actual[10] = {0};
	BP_ZERO_STACK(table_path_actual);
	BP_HASH_TABLE_TRAVERSAL_PATH table_path = (char **) &table_path_actual;
	table_path[0] = "system";
	table_path[1] = "event_managment";
	table_path[2] = mgr->name;
	switch(pickup_from_queue)
	{
		case BP_EVENT_MESSAGES_IN:
			table_path[3] = "events_in";
			break;
		case BP_EVENT_MESSAGES_OUT:
			table_path[3] = "events_out";
			break;
		default:
			BP_EVENTS_SEM_UNLOCK(mgr->lock);
			BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	table_path[4] = priority_selected;
	table_path[5] = NULL;
	P_BP_HASH_TABLE_KEYED_ENTRY lookup_entry = BP_HashRegLookupTableByPath
	(
		NULL,
		table_path
	);
	if(!lookup_entry)
	{
		BP_EVENTS_SEM_UNLOCK(mgr->lock);
		BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	P_BP_HASH_TABLE_KEYED_ENTRY lookup_table = (P_BP_HASH_TABLE_KEYED_ENTRY) lookup_entry->data;
	if(!lookup_table)
	{
			BP_EVENTS_SEM_UNLOCK(mgr->lock);
			BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	}
	P_BP_HASH_TABLE_KEYED_ENTRY entry       = NULL;
	P_BP_HASH_TABLE_KEYED_ENTRY tmp         = NULL;
	P_BP_HASH_TABLE_KEYED_ENTRY next_event = NULL;
	HASH_ITER(hh, lookup_table, entry, tmp)
	{
		if(entry->type != BP_HASH_TABLE_KEYED_ENTRY_TYPE_NAMED_HASH_TABLE)
		{
			next_event = (P_BP_HASH_TABLE_KEYED_ENTRY) entry;
			break;
		}
	}
	if(!next_event)
		BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(NULL);
	P_BP_EVENT picked_up_event = (P_BP_EVENT) bpcalloc(sizeof(BP_EVENT), 1);
	memcpy(picked_up_event, next_event->data, sizeof(BP_EVENT));
	BP_HashRegDestroyEntry(NULL, next_event);
	BP_GHT_LEAVE_CRITICAL_SECTION;
	return picked_up_event;
}
P_BP_EVENT BP_EventMgrPickupEventFromMainLoop ( P_BP_EVENT_MANAGER mgr , BP_EVENT_MESSAGE_IN_OUT pickup_from_queue , BP_EVENT_PRIORITY event_priority ) { if ( ! mgr ) return NULL ; if ( ! pickup_from_queue ) return NULL ; if ( ! event_priority ) return NULL ; BP_GHT_ENTER_CRITICAL_SECTION ; char * priority_selected = NULL ; switch ( event_priority ) { case BP_EVENT_PRIORITY_VERY_LOW : priority_selected = " " ; break ; case BP_EVENT_PRIORITY_LOW : priority_selected = " " ; break ; case BP_EVENT_PRIORITY_MID_NORMAL : priority_selected = " " ; break ; case BP_EVENT_PRIORITY_NORMAL : priority_selected = " " ; break ; case BP_EVENT_PRIORITY_MID_HIGH : priority_selected = " " ; break ; case BP_EVENT_PRIORITY_HIGH : priority_selected = " " ; break ; case BP_EVENT_PRIORITY_SYSTEM : priority_selected = " " ; break ; case BP_EVENT_PRIORITY_CRITICAL_SYSTEM : priority_selected = " " ; break ; default : BP_EVENTS_SEM_UNLOCK ( mgr -> lock ) ; BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } if ( ! priority_selected ) { BP_EVENTS_SEM_UNLOCK ( mgr -> lock ) ; BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } char * table_path_actual [ 10 ] = { 0 } ; BP_ZERO_STACK ( table_path_actual ) ; BP_HASH_TABLE_TRAVERSAL_PATH table_path = ( char * * ) & table_path_actual ; table_path [ 0 ] = " " ; table_path [ 1 ] = " " ; table_path [ 2 ] = mgr -> name ; switch ( pickup_from_queue ) { case BP_EVENT_MESSAGES_IN : table_path [ 3 ] = " " ; break ; case BP_EVENT_MESSAGES_OUT : table_path [ 3 ] = " " ; break ; default : BP_EVENTS_SEM_UNLOCK ( mgr -> lock ) ; BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } table_path [ 4 ] = priority_selected ; table_path [ 5 ] = NULL ; P_BP_HASH_TABLE_KEYED_ENTRY lookup_entry = BP_HashRegLookupTableByPath ( NULL , table_path ) ; if ( ! lookup_entry ) { BP_EVENTS_SEM_UNLOCK ( mgr -> lock ) ; BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } P_BP_HASH_TABLE_KEYED_ENTRY lookup_table = ( P_BP_HASH_TABLE_KEYED_ENTRY ) lookup_entry -> data ; if ( ! lookup_table ) { BP_EVENTS_SEM_UNLOCK ( mgr -> lock ) ; BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; } P_BP_HASH_TABLE_KEYED_ENTRY entry = NULL ; P_BP_HASH_TABLE_KEYED_ENTRY tmp = NULL ; P_BP_HASH_TABLE_KEYED_ENTRY next_event = NULL ; HASH_ITER ( hh , lookup_table , entry , tmp ) { if ( entry -> type != BP_HASH_TABLE_KEYED_ENTRY_TYPE_NAMED_HASH_TABLE ) { next_event = ( P_BP_HASH_TABLE_KEYED_ENTRY ) entry ; break ; } } if ( ! next_event ) BP_GHT_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( NULL ) ; P_BP_EVENT picked_up_event = ( P_BP_EVENT ) bpcalloc ( sizeof ( BP_EVENT ) , 1 ) ; memcpy ( picked_up_event , next_event -> data , sizeof ( BP_EVENT ) ) ; BP_HashRegDestroyEntry ( NULL , next_event ) ; BP_GHT_LEAVE_CRITICAL_SECTION ; return picked_up_event ; }
Pickup event from main loop.
Pickup event from main loop .
// ensure we have an event manager, a queue, and a priority // enter critical ght section // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Stringify The Event Priority (for lookup) %%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // reference which will contain a static string // switch on the event priority and // Unlock and exit with failure if we cannot // map a priority. // unlock and exit if there is no priority (should never be able // to get here, but still adding the check anyway) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Attempt to Lookup Events In Table %%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create table path on the stack // set a reference to the correct type // set the table path // switch to determine which queue to use // use messages in queue // messages out // if it's not one of the message queues, unlock and exit with // failure code. // set the 4th position as the selected priority // force truncation of table // attempt to lookup the newly created entry // exit if we couldn't look up the entry // set lookup table data // set lookup table // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Attempt to Iterate Events %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // temporary iterator values used during the HASH_ITER loop below // next event to process (set in loop below) // iterate through all hashes // if we have an entry, set next_event and exit immediately // if there is no next event to process, exit immediately // if we could pick up an event, set the returned event as the data (allocate first since // destroying the entry frees the associated memory) // copy in the data // Remove the event from the queue (aka destroy it from table).  This // free's the event, and removes the entry from the queue table. // leave the critical ght section // return the new entry