1 // Demand channels. See squint paper by McIlroy.
3 // TODO: Handle messy thread problems. What happens if a thread quits
4 // but then another tries to signal and read its channel?
5 // TODO: What if the continued fraction terminates?
14 struct channel_s
*next
;
16 typedef struct channel_s channel_t
[1];
17 typedef struct channel_s
*channel_ptr
;
20 // Each continued fraction is a separate thread.
22 // Helgrind prints warnings for these condition variables.
23 // Rewrite with semaphores?
24 // When queue is empty, and there is demand for the next term.
26 // When the queue was empty, and we just added to it.
27 pthread_cond_t read_cond
;
28 pthread_mutex_t chan_mu
;
29 channel_ptr chan
, next
;
35 typedef struct cf_s
*cf_t
;
37 void *cf_data(cf_t cf
) {
41 // A bit like cooperative multitasking. Continued fractions are expected
42 // to call this as often as practical, and on a return value of 0,
43 // to drop everything and stop.
44 int cf_wait(cf_t cf
) {
46 sem_wait(&cf
->demand_sem
);
51 pthread_mutex_lock(&cf
->chan_mu
);
52 // ... but we keep waiting unless the channel is empty.
54 pthread_mutex_unlock(&cf
->chan_mu
);
55 // The channel could be emptied in the meantime, but that
56 // implies at least one sem_post() call, so we'll notice next iteration.
58 pthread_mutex_unlock(&cf
->chan_mu
);
62 void cf_free(cf_t cf
) {
63 // These two statements force a thread out of its next/current cf_wait.
65 sem_post(&cf
->demand_sem
);
67 pthread_join(cf
->thread
, NULL
);
68 pthread_mutex_lock(&cf
->chan_mu
);
69 channel_ptr c
= cf
->chan
;
71 channel_ptr cnext
= c
->next
;
76 pthread_mutex_unlock(&cf
->chan_mu
);
77 sem_destroy(&cf
->demand_sem
);
81 void cf_put(cf_t cf
, mpz_t z
) {
82 // TODO: Block or something if there's a large backlog on the queue.
83 channel_ptr cnew
= malloc(sizeof(*cnew
));
85 unsigned char *uc
= malloc(4);
86 uc
[0] = uc
[1] = uc
[2] = uc
[3] = 0;
89 size_t count
= (mpz_sizeinbase(z
, 2) + 8 - 1) / 8;
90 unsigned char *uc
= malloc(count
+ 4);
92 uc
[0] = count
>> (8 * 3);
93 uc
[1] = (count
>> (8 * 2)) & 255;
94 uc
[2] = (count
>> 8) & 255;
96 mpz_export(uc
+ 4, NULL
, 1, 1, 1, 0, z
);
99 pthread_mutex_lock(&cf
->chan_mu
);
101 cf
->next
->next
= cnew
;
103 // Channel is empty. Now that we're populating it, send signal
104 // in case someone is waiting for data.
106 pthread_cond_signal(&cf
->read_cond
);
109 pthread_mutex_unlock(&cf
->chan_mu
);
112 void cf_get(mpz_t z
, cf_t cf
) {
113 pthread_mutex_lock(&cf
->chan_mu
);
115 // If channel is empty, send demand signal and wait for read signal.
116 sem_post(&cf
->demand_sem
);
117 pthread_cond_wait(&cf
->read_cond
, &cf
->chan_mu
);
119 channel_ptr c
= cf
->chan
;
121 pthread_mutex_unlock(&cf
->chan_mu
);
122 unsigned char *uc
= c
->data
;
126 + (uc
[0] << (8 * 3));
127 if (count
) mpz_import(z
, count
, 1, 1, 1, 0, uc
+ 4);
128 else mpz_set_ui(z
, 0);
133 cf_t
cf_new(void *(*func
)(cf_t
), void *data
) {
134 cf_t cf
= malloc(sizeof(*cf
));
140 pthread_attr_init(&attr
);
141 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_JOINABLE
);
142 pthread_mutex_init(&cf
->chan_mu
, NULL
);
143 sem_init(&cf
->demand_sem
, 0, 0);
144 pthread_cond_init(&cf
->read_cond
, NULL
);
145 pthread_create(&cf
->thread
, &attr
, (void*(*)(void *)) func
, cf
);
146 pthread_attr_destroy(&attr
);