Beispiel #1
0
void receiver2(chan ch, int expected, chan back) {
    int val = chr(ch, int);
    assert(val == expected);
    chclose(ch);
    chs(back, int, 0);
    chclose(back);
}
Beispiel #2
0
/* receive a value from a pipe and send it to the channel */
coroutine void recv_chs(mill_pipe p, chan ch, mill_wgroup wg) {
    mill_wgadd(wg);
    int done;
    while (1) {
        int k = *((int *)mill_piperecv(p, &done));
        if (done)
            break;
        chs(ch, int, k);
    }
    chclose(ch);
}
Beispiel #3
0
int main() {
    /* Test 'msleep'. */
    uint64_t ms = now();
    msleep(100);
    ms = now() - ms;
    assert(ms > 90 && ms < 110);

    /* Test 'after'. */
    ms = now();
    chan a = after(100);
    chr(a, int);
    ms = now() - ms;
    assert(ms > 90 && ms < 110);
    chclose(a);

    return 0;
}
Beispiel #4
0
int main(void) {
    pthread_t thrd[3];
    int done = 0;
    mill_pipe p[3];
    mill_init(-1, -1);
    chan ch = chmake(int, 1);
    mill_wgroup wg = mill_wgmake();
    int j;
    for (j = 0; j < 3; j++) { 
        p[j] = mill_pipemake(sizeof (int));
        go(recv_chs(p[j], chdup(ch), wg));
        pthread_create(&thrd[j], NULL, produce, mill_pipedup(p[j]));
    }

    /* wait for all reader coroutines to finish and then close the channel */
    go(wait_done(ch, wg));

    while (! done) {
    choose {
    in(ch, int, val):
        printf(" %d", val);
        done = ! val;
    otherwise:
        yield();
    end
    }
    }
    printf("\n");

    for (j = 0; j < 3; j++) {
        pthread_join(thrd[j], NULL);
        mill_pipefree(p[j]);
    }
    mill_wgfree(wg);
    chclose(ch);
    mill_fini();
    return 0;
}
Beispiel #5
0
int main() {
    int val;

    /* Receiver waits for sender. */
    chan ch1 = chmake(int, 0);
    go(sender(chdup(ch1), 1, 333));
    val = chr(ch1, int);
    assert(val == 333);
    chclose(ch1);

    /* Sender waits for receiver. */
    chan ch2 = chmake(int, 0);
    go(sender(chdup(ch2), 0, 444));
    val = chr(ch2, int);
    assert(val == 444);
    chclose(ch2);

    /* Test two simultaneous senders. */
    chan ch3 = chmake(int, 0);
    go(sender(chdup(ch3), 0, 888));
    go(sender(chdup(ch3), 0, 999));
    val = chr(ch3, int);
    assert(val == 888);
    yield();
    val = chr(ch3, int);
    assert(val == 999);
    chclose(ch3);

    /* Test two simultaneous receivers. */
    chan ch4 = chmake(int, 0);
    go(receiver(chdup(ch4), 333));
    go(receiver(chdup(ch4), 444));
    chs(ch4, int, 333);
    chs(ch4, int, 444);
    chclose(ch4);

    /* Test typed channels. */
    chan ch5 = chmake(char, 0);
    go(charsender(chdup(ch5), 111));
    char charval = chr(ch5, char);
    assert(charval == 111);
    chclose(ch5);
    chan ch6 = chmake(struct foo, 0);
    struct foo foo1 = {555, 222};
    go(structsender(chdup(ch6), foo1));
    struct foo foo2 = chr(ch6, struct foo);
    assert(foo2.first == 555 && foo2.second == 222);
    chclose(ch6);

    /* Test message buffering. */
    chan ch7 = chmake(int, 2);
    chs(ch7, int, 222);
    chs(ch7, int, 333);
    val = chr(ch7, int);
    assert(val == 222);
    val = chr(ch7, int);
    assert(val == 333);
    chs(ch7, int, 444);
    val = chr(ch7, int);
    assert(val == 444);
    chs(ch7, int, 555);
    chs(ch7, int, 666);
    val = chr(ch7, int);
    assert(val == 555);
    val = chr(ch7, int);
    assert(val == 666);
    chclose(ch7);

    /* Test simple chdone() scenarios. */
    chan ch8 = chmake(int, 0);
    chdone(ch8, int, 777);
    val = chr(ch8, int);
    assert(val == 777);
    val = chr(ch8, int);
    assert(val == 777);
    val = chr(ch8, int);
    assert(val == 777);
    chclose(ch8);
    chan ch9 = chmake(int, 10);
    chdone(ch9, int, 888);
    val = chr(ch9, int);
    assert(val == 888);
    val = chr(ch9, int);
    assert(val == 888);
    chclose(ch9);
    chan ch10 = chmake(int, 10);
    chs(ch10, int, 999);
    chdone(ch10, int, 111);
    val = chr(ch10, int);
    assert(val == 999);
    val = chr(ch10, int);
    assert(val == 111);
    val = chr(ch10, int);
    assert(val == 111);
    chclose(ch10);
    chan ch11 = chmake(int, 1);
    chs(ch11, int, 222);
    chdone(ch11, int, 333);
    val = chr(ch11, int);
    assert(val == 222);
    val = chr(ch11, int);
    assert(val == 333);
    chclose(ch11);

    /* Test whether chdone() unblocks all receivers. */
    chan ch12 = chmake(int, 0);
    chan ch13 = chmake(int, 0);
    go(receiver2(chdup(ch12), 444, chdup(ch13)));
    go(receiver2(chdup(ch12), 444, chdup(ch13)));
    chdone(ch12, int, 444);
    val = chr(ch13, int);
    assert(val == 0);
    val = chr(ch13, int);
    assert(val == 0);
    chclose(ch13);
    chclose(ch12);

    return 0;
}
Beispiel #6
0
void structsender(chan ch, struct foo val) {
    chs(ch, struct foo, val);
    chclose(ch);
}
Beispiel #7
0
void charsender(chan ch, char val) {
    chs(ch, char, val);
    chclose(ch);
}
Beispiel #8
0
void receiver(chan ch, int expected) {
    int val = chr(ch, int);
    assert(val == expected);
    chclose(ch);
}
Beispiel #9
0
void sender(chan ch, int doyield, int val) {
    if(doyield)
        yield();
    chs(ch, int, val);
    chclose(ch);
}
Beispiel #10
0
void sender2(chan ch, int val) {
    yield();
    chs(ch, int, val);
    chclose(ch);
}
Beispiel #11
0
void sender1(chan ch, int val) {
    chs(ch, int, val);
    chclose(ch);
}
Beispiel #12
0
int main() {
    int val;

    /* Receiver waits for sender. */
    chan ch1 = chmake(int, 0);
    go(sender(chdup(ch1), 1, 333));
    val = chr(ch1, int);
    assert(val == 333);
    chclose(ch1);

    /* Sender waits for receiver. */
    chan ch2 = chmake(int, 0);
    go(sender(chdup(ch2), 0, 444));
    val = chr(ch2, int);
    assert(val == 444);
    chclose(ch2);

    /* Test two simultaneous senders. */
    chan ch3 = chmake(int, 0);
    go(sender(chdup(ch3), 0, 888));
    go(sender(chdup(ch3), 0, 999));
    val = chr(ch3, int);
    assert(val == 888);
    yield();
    val = chr(ch3, int);
    assert(val == 999);
    chclose(ch3);

    /* Test two simultaneous receivers. */
    chan ch4 = chmake(int, 0);
    go(receiver(chdup(ch4), 333));
    go(receiver(chdup(ch4), 444));
    chs(ch4, int, 333);
    chs(ch4, int, 444);
    chclose(ch4);

    /* Test typed channels. */
    chan ch5 = chmake(char, 0);
    go(charsender(chdup(ch5), 111));
    char charval = chr(ch5, char);
    assert(charval == 111);
    chclose(ch5);
    chan ch6 = chmake(struct foo, 0);
    struct foo foo1 = {555, 222};
    go(structsender(chdup(ch6), foo1));
    struct foo foo2 = chr(ch6, struct foo);
    assert(foo2.first == 555 && foo2.second == 222);
    chclose(ch6);

    /* Test message buffering. */
    chan ch7 = chmake(int, 2);
    chs(ch7, int, 222);
    chs(ch7, int, 333);
    val = chr(ch7, int);
    assert(val == 222);
    val = chr(ch7, int);
    assert(val == 333);
    chs(ch7, int, 444);
    val = chr(ch7, int);
    assert(val == 444);
    chs(ch7, int, 555);
    chs(ch7, int, 666);
    val = chr(ch7, int);
    assert(val == 555);
    val = chr(ch7, int);
    assert(val == 666);
    chclose(ch7);

    /* Test simple chdone() scenarios. */
    chan ch8 = chmake(int, 0);
    chdone(ch8, int, 777);
    val = chr(ch8, int);
    assert(val == 777);
    val = chr(ch8, int);
    assert(val == 777);
    val = chr(ch8, int);
    assert(val == 777);
    chclose(ch8);
    chan ch9 = chmake(int, 10);
    chdone(ch9, int, 888);
    val = chr(ch9, int);
    assert(val == 888);
    val = chr(ch9, int);
    assert(val == 888);
    chclose(ch9);
    chan ch10 = chmake(int, 10);
    chs(ch10, int, 999);
    chdone(ch10, int, 111);
    val = chr(ch10, int);
    assert(val == 999);
    val = chr(ch10, int);
    assert(val == 111);
    val = chr(ch10, int);
    assert(val == 111);
    chclose(ch10);
    chan ch11 = chmake(int, 1);
    chs(ch11, int, 222);
    chdone(ch11, int, 333);
    val = chr(ch11, int);
    assert(val == 222);
    val = chr(ch11, int);
    assert(val == 333);
    chclose(ch11);

    /* Test whether chdone() unblocks all receivers. */
    chan ch12 = chmake(int, 0);
    chan ch13 = chmake(int, 0);
    go(receiver2(chdup(ch12), 444, chdup(ch13)));
    go(receiver2(chdup(ch12), 444, chdup(ch13)));
    chdone(ch12, int, 444);
    val = chr(ch13, int);
    assert(val == 0);
    val = chr(ch13, int);
    assert(val == 0);
    chclose(ch13);
    chclose(ch12);

    /* Test a combination of blocked sender and an item in the channel. */
    chan ch14 = chmake(int, 1);
    chs(ch14, int, 1);
    go(sender(chdup(ch14), 0, 2));
    val = chr(ch14, int);
    assert(val == 1);
    val = chr(ch14, int);
    assert(val == 2);
    chclose(ch14);

    pid_t pid;

    /* Test panic when chs will deadlock. */
    pid = fork();
    assert(pid >= 0);
    if (pid == 0) {
        alarm(1);
        chan ch = chmake(int, 0);
        signal(SIGABRT, expected_abort);
        chs(ch, int, 42);
        fail();
    }