void reset() { ::reset(); object spot = get_work_spot(); if( !is_clone(this_object()) ) return; if( spot && !spot->query_num_employees() ) return; if( !last_spawn_time ) last_spawn_time = time(); while( last_spawn_time < time() ) { spawn_vegetables(); last_spawn_time += SPAWN_FREQUENCY; kid_spawn_time++; } // It takes about 1,000,000 seconds for a kid to grow up, // so to keep the rate reasonable, I want it to take about // a third that for them to spawn. 500 * 600 = 300,000. while( kid_spawn_time >= 500 ) { debug("Producing a kid", "gp"); spawn_kid(); kid_spawn_time -= 500; } // Spread out any food we have, although only if we have // employees. supply_food(); }
/* * Main event loop processing */ void event_loop( dm_sessid_t sid) { void *msgbuf; size_t bufsize, rlen; int error; dm_eventmsg_t *msg; /* * We take a swag at a buffer size. If it's wrong, we can * always resize it */ bufsize = sizeof(dm_eventmsg_t) + sizeof(dm_data_event_t) + HANDLE_LEN; bufsize *= 16; msgbuf = (void *)malloc(bufsize); if (msgbuf == NULL) { err_msg("Can't allocate memory for buffer"); goto out; } for (;;) { error = dm_get_events(sid, ALL_AVAIL_MSGS, DM_EV_WAIT, bufsize, msgbuf, &rlen); if (error == -1) { if (errno == E2BIG) { free(msgbuf); msgbuf = (void *)malloc(rlen); if (msgbuf == NULL) { err_msg("Can't resize msg buffer"); goto out; } continue; } errno_msg("Error getting events from DMAPI"); goto out; } /* * Walk thru the message buffer, pull out each individual * message, and dispatch the messages to child processes * with the sid, token, and data. The children will * respond to the events. */ msg = (dm_eventmsg_t *)msgbuf; while (msg != NULL ) { if (Verbose) { fprintf(stderr, "Received %s, token %d\n", (msg->ev_type == DM_EVENT_READ ? "read" : (msg->ev_type == DM_EVENT_WRITE ? "write" : "trunc")), msg->ev_token); } switch (msg->ev_type) { case DM_EVENT_READ: spawn_kid(sid, msg->ev_token, RESTORE_FILE); break; case DM_EVENT_WRITE: case DM_EVENT_TRUNCATE: spawn_kid(sid, msg->ev_token, INVAL_FILE); break; default: err_msg("Invalid msg type %d\n", msg->ev_type); break; } msg = DM_STEP_TO_NEXT(msg, dm_eventmsg_t *); } } out: if (msgbuf != NULL) free(msgbuf); migin_exit(0); }