diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | src/itc.c | 138 | ||||
| -rw-r--r-- | src/main.c | 5 | 
3 files changed, 144 insertions, 1 deletions
@@ -9,7 +9,7 @@ PP_CC := $(PP_BOLD)$(shell tput setf 6)CC$(PP_RESET)  PP_LD := $(PP_BOLD)$(shell tput setf 2)LD$(PP_RESET)  PP_RM := $(PP_BOLD)$(shell tput setf 4)RM$(PP_RESET) -SRC := src/main.c src/physics.c src/renderer.c src/ui.c +SRC := src/main.c src/physics.c src/renderer.c src/ui.c src/itc.c  OBJ := $(SRC:src/%.c=obj/%.o)  OUT := cem diff --git a/src/itc.c b/src/itc.c new file mode 100644 index 0000000..62e6208 --- /dev/null +++ b/src/itc.c @@ -0,0 +1,138 @@ +#include "common.h" +#include <unistd.h> + +typedef struct itc_message_s itc_message; +struct itc_message_s { +	int number; +	void *data; + +	itc_message *older, *newer; +}; + +typedef struct { +	itc_message *newest, *oldest; +	SDL_mutex *mutex; + +} itc_chan; + + +void itc_chan_create(itc_chan *chan) +{ +	memset(chan, 0, sizeof(itc_chan)); + +	chan->mutex = SDL_CreateMutex(); +	assert(chan->mutex); +} + +void itc_chan_destroy(itc_chan *chan) +{ +	// todo: free queue + +	SDL_DestroyMutex(chan->mutex); +} + +void itc_chan_push(itc_chan *chan, int number, void *data) +{ +	itc_message *msg; + +	msg = calloc(1, sizeof(itc_message)); +	assert(msg); + +	msg->number = number; +	msg->data = data; + +	SDL_LockMutex(chan->mutex); +	 +	if (!chan->oldest) {       +		chan->oldest = msg; +		chan->newest = msg; +	} else { +		/* +		 *   head <---> ... <---> tail +		 * +		 */ + +		chan->newest->newer = msg; +		msg->older = chan->newest; +		chan->newest = msg; +	} + +	SDL_UnlockMutex(chan->mutex); +} + +int itc_chan_pop(itc_chan *chan, int *number, void **data) +{ +	itc_message *msg; + +	SDL_LockMutex(chan->mutex); + +	if (!chan->oldest) { +		SDL_UnlockMutex(chan->mutex); +		return 1; +	} + +	msg = chan->oldest; + +	if (msg->newer) { +		msg->newer->older = NULL; +		chan->oldest = msg->newer; +	} else { +		chan->newest = NULL; +		chan->oldest = NULL; +	} + +	SDL_UnlockMutex(chan->mutex); + +	*number = msg->number; +	*data = msg->data; +	free(msg); + +	return 0; +} + + + +itc_chan gulg_chan; + +int gulgulator(void *unused) +{ +	int cmd_num; +	void *cmd_data; + +	while (1) { +		while (!itc_chan_pop(&gulg_chan, &cmd_num, &cmd_data)) { +			printf("gulgulator: pop %i %p\n", cmd_num, cmd_data); +		} + +		usleep(100 * 1000); +	} +} + +void itc_test(void) +{ +	SDL_Thread *thread; +	char line[512]; + +	itc_chan_create(&gulg_chan); +	thread = SDL_CreateThread(gulgulator, "gulgulator", NULL); + +	while (1) { +		int cmd_num; + +		printf("> "); +		fflush(stdout); + +		if (!fgets(line, sizeof(line), stdin)) +			break; + +		cmd_num = atoi(line); + +		printf("main: push %i %p\n", cmd_num, 0xDEADBEEF); +		itc_chan_push(&gulg_chan, cmd_num, (void*)0xDEADBEEF); +	} + +	printf("EOF\n"); + +	SDL_WaitThread(thread, NULL); +} + @@ -21,6 +21,8 @@ int64_t get_time(void)  	return ts.tv_sec * 1000000000 + ts.tv_nsec;  } +void itc_test(void); +  int main(void)  {  	int rv = 0; @@ -28,6 +30,9 @@ int main(void)  	phy_sim sim;  	SDL_Thread *sim_thread; +	itc_test(); +	return; +  	if (r_init()) {  		con_printf("fatal error: renderer initialization failed\n");  		rv = 1;  | 
