/** * The connection thread serves a client for the duration of the * socket lifetime. */ static void *connection_main(void *arg_p) { struct http_server_connection_t *connection_p = arg_p; struct http_server_t *self_p = connection_p->self_p; uint32_t mask; /* thrd_init_env(buf, sizeof(buf)); */ /* thrd_set_env("CWD", self_p->root_path_p); */ thrd_set_name(connection_p->thrd.name_p); /* Wait for a connection from the listener. */ while (1) { log_object_print(NULL, LOG_DEBUG, FSTR("Connection thread '%s' waiting for a new connection.\r\n"), thrd_get_name()); mask = 0x1; event_read(&connection_p->events, &mask, sizeof(mask)); if (mask & 0x1) { handle_request(self_p, connection_p); socket_close(&connection_p->socket); /* Add thread to the free list. */ sys_lock(); connection_p->state = http_server_connection_state_free_t; sys_unlock(); mask = 0x1; event_write(&self_p->events, &mask, sizeof(mask)); } } return (NULL); }
static void *suspend_resume_main(void *arg_p) { thrd_set_name("resumer"); thrd_resume(arg_p, 3); return (NULL); }
void *preemptive_main(void *arg_p) { thrd_set_name("preemptive"); while (1); return (NULL); }
/** * The listener thread main function. The listener listens for * connections from clients. */ static void *listener_main(void *arg_p) { struct http_server_t *self_p = arg_p; struct http_server_listener_t *listener_p; struct http_server_connection_t *connection_p; struct inet_addr_t addr; thrd_set_name(self_p->listener_p->thrd.name_p); listener_p = self_p->listener_p; if (socket_open_tcp(&listener_p->socket) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to open socket.")); return (NULL); } if (inet_aton(listener_p->address_p, &addr.ip) != 0) { return (NULL); } addr.port = listener_p->port; if (socket_bind(&listener_p->socket, &addr) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to bind socket.")); return (NULL); } if (socket_listen(&listener_p->socket, 3) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to listen on socket.")); return (NULL); } /* Wait for clients to connect. */ while (1) { /* Allocate a connection. */ connection_p = allocate_connection(self_p); /* Wait for a client to connect. */ socket_accept(&listener_p->socket, &connection_p->socket, &addr); handle_accept(self_p, connection_p); } return (NULL); }
/** * The main thread of the music player. Handles events and coverts * samples to play a song. */ static void *music_player_main(struct music_player_t *self_p) { uint32_t mask; struct time_t timeout; thrd_set_name("music_player"); /* Start the periodic fill timer. */ timeout.seconds = 0; timeout.nanoseconds = 10000000; timer_init(&self_p->timer, &timeout, (void (*)(void *))fill_timer_cb, self_p, TIMER_PERIODIC); timer_start(&self_p->timer); /* Start the main loop of the music player. */ while (1) { mask = (EVENT_PLAY | EVENT_PAUSE | EVENT_STOP | EVENT_TIMEOUT); event_read(&self_p->event, &mask, sizeof(mask)); if (mask & EVENT_STOP) { handle_event_stop(self_p); } if (mask & EVENT_PAUSE) { handle_event_pause(self_p); } if (mask & EVENT_PLAY) { handle_event_play(self_p); } /* Play if the state in playing, eyy! */ if (self_p->state == STATE_PLAYING) { play_chunk(self_p); } } return (NULL); }
static int outputHandlerThread(void *stateArg) { if (stateArg == NULL) { return (thrd_error); } flowOutputState state = stateArg; switch (state->mode) { case OF_OUT_UART: thrd_set_name(SUBSYSTEM_UART); break; case OF_OUT_FILE: thrd_set_name(SUBSYSTEM_FILE); break; case OF_OUT_BOTH: thrd_set_name("FLOW_OUTPUT"); break; case OF_OUT_NONE: break; } struct timespec sleepTime = { .tv_sec = 0, .tv_nsec = 500000 }; // Wait until the buffer is initialized while (!atomic_load_explicit(&state->running, memory_order_relaxed)) { thrd_sleep(&sleepTime, NULL); } // Main thread loop while (atomic_load_explicit(&state->running, memory_order_relaxed)) { FlowEventPacket packet = getPacketFromTransferBuffer(state->buffer); if (packet == NULL) { // no data: sleep for a while thrd_sleep(&sleepTime, NULL); } else { if (state->mode == OF_OUT_UART || state->mode == OF_OUT_BOTH) { if (!sendFlowEventPacketUart(packet)) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "A flow packet was not sent."); } } if (state->mode == OF_OUT_FILE || state->mode == OF_OUT_BOTH) { if (!writeFlowEventPacketFile(state,packet)) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_FILE, "A flow packet was not written."); } } flowEventPacketFree(packet); } } // If shutdown: empty buffer before closing thread FlowEventPacket packet; while ((packet = getPacketFromTransferBuffer(state->buffer)) != NULL) { if (state->mode == OF_OUT_UART || state->mode == OF_OUT_BOTH) { if (!sendFlowEventPacketUart(packet)) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "A flow packet was not sent."); } } if (state->mode == OF_OUT_FILE || state->mode == OF_OUT_BOTH) { if (!writeFlowEventPacketFile(state,packet)) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_FILE, "A flow packet was not written."); } } flowEventPacketFree(packet); } return (thrd_success); }