static void * listen_thread(void *data) { long length; DNS_query *query; while (running) { if (queueIsEmpty(&queries_unused)) { if ((query = malloc(sizeof (*query))) == NULL) { syslog(LOG_ERR, "out of memory"); sleep(2); continue; } } else { query = queueDequeue(&queries_unused)->data; } do { length = socketReadFrom(service, (unsigned char *) &query->packet.header, UDP_PACKET_SIZE, &query->client); if (0 < debug && 0 < length) { char *from = socketAddressToString(&query->client); syslog(LOG_DEBUG, "from=%s length=%ld", from, length); free(from); } } while (length <= 0); query->node.data = query; query->packet.length = length; queueEnqueue(&queries_waiting, &query->node); } return NULL; }
void * consume(void *arg) { list *l = (list *)arg; while(1) { char buf[32]; int success; success = queueDequeue(l, &buf); if(!success) { break; } } return 0; }
static void * answer_thread(void *data) { DNS_query *query; while (running) { query = queueDequeue(&queries_waiting)->data; find_answer(query); socketWriteTo(service, (unsigned char *) &query->packet.header, query->packet.length, &query->client); queueEnqueue(&queries_unused, &query->node); } return NULL; }
int main() { int i = 0; //int *queuedata; void * dataout; //void * dummyqueuezero = &dataqueue[1]; //void * dummyqueueone = &dataqueue[1]; queueInit(&queueTest, QUEUESIZE-1); while( queueEnqueue(&queueTest, &dataqueue[i]) == noError) { i++; } while( queueDequeue(&queueTest, &dataout) == noError) { //queuedata = dataout; //blaat = *(int *) dataout; printf("dequeued element %d\n", *((int *) dataout)); } return 0; }
// to_goalがtrueのとき、ゴールまでの経路を探索する // to_goalがfalseのとき、未探索の地点までの経路を探索する enum action_t agent_explore(void) { queueInit(); for(int y = 0; y < size; ++y) { for(int x = 0; x < size; ++x) { if(check_goal(y, x)) { d[y][x] = 0; inQueue[y][x] = true; queueEnqueue(y, x); } else { d[y][x] = UCHAR_MAX - 1; inQueue[y][x] = false; } } } while(queueEmpty() == false) { int vy, vx; queueDequeue(&vy, &vx); inQueue[vy][vx] = false; for (int k = 0; k < 4; k++) { if (wall[vy][vx][k] == false) { int ny = vy + dy[k]; int nx = vx + dx[k]; if(d[ny][nx] > d[vy][vx] + 1) { d[ny][nx] = d[vy][vx] + 1; if(inQueue[ny][nx] == false) { inQueue[ny][nx] = true; queueEnqueue(ny, nx); } } } } } if(wall[curY][curX][(dir + 0)%4] == false && d[curY + dy[(dir + 0)%4]][curX + dx[(dir + 0)%4]] + 1 == d[curY][curX]) { // GoForward curY += dy[dir]; curX += dx[dir]; return GO_FORWARD; } if(wall[curY][curX][(dir + 1)%4] == false && d[curY + dy[(dir + 1)%4]][curX + dx[(dir + 1)%4]] + 1 == d[curY][curX]) { // TurnLeft dir = (dir + 1) % 4; return TURN_LEFT; } if(wall[curY][curX][(dir + 3)%4] == false && d[curY + dy[(dir + 3)%4]][curX + dx[(dir + 3)%4]] + 1 == d[curY][curX]) { // TurnRight dir = (dir + 3) % 4; return TURN_RIGHT; } if(wall[curY][curX][(dir + 2)%4] == false && d[curY + dy[(dir + 2)%4]][curX + dx[(dir + 2)%4]] + 1 == d[curY][curX]) { // TurnLeft dir = (dir + 1) % 4; return TURN_LEFT; } return NO_OPERATION; }