Enums in Cilk

Our enum type ServerState has an underlying int type.

enum ServerState {
    IDLE,
    CONNECTED,
    ERROR,
    RETRYING
}

inline const char* toString(ServerState state) {
    switch (state) {
        case IDLE: return "idle";
        case CONNECTED: return "connected";
        case ERROR: return "error";
        case RETRYING: return "retrying";
        default: return "unknown";
    }
}

inline ServerState transition(ServerState state) {
    switch (state) {
        case IDLE:
            return CONNECTED;
        case CONNECTED:
        case RETRYING:
            return IDLE;
        case ERROR:
            return ERROR;
        default:
            __cilkrts_bug("unknown state: %d", state);
    }
}

int main() {
    ServerState ns = transition(IDLE);
    printf("%s\n", toString(ns));
    ServerState ns2 = transition(ns);
    printf("%s\n", toString(ns2));
    return 0;
}

To run this example, ensure you have the necessary Cilk runtime and compiler setup.

$ cilkc -o enums enums.c
$ ./enums
connected
idle