72 #ifndef CGU_ASYNC_CHANNEL_H
73 #define CGU_ASYNC_CHANNEL_H
85 #ifdef CGU_USE_SCHED_YIELD
97 --(*
static_cast<std::size_t*
>(arg));
197 enum Status {normal, closed, destructing} status;
234 if (status == normal) {
264 bool waiting =
false;
266 if (status != normal)
return false;
277 while (size >= n && status == normal) {
285 pthread_cleanup_pop(
false);
289 Status local_status = status;
290 if (waiting) --waiters;
291 if (local_status != normal)
return false;
294 new (
static_cast<void*
>(buf + next)) T(obj);
326 bool waiting =
false;
338 while (!size && status == normal) {
346 pthread_cleanup_pop(
false);
348 if (status == destructing) {
351 if (waiting) --waiters;
358 if (idx == n) idx = 0;
366 if (waiting) --waiters;
370 if (waiting) --waiters;
375 if (waiting) --waiters;
408 buf(static_cast<T*>(std::malloc(sizeof(T) * n))) {
409 if (!buf)
throw std::bad_alloc();
427 status = destructing;
439 #ifdef CGU_USE_SCHED_YIELD
453 if (idx == n) idx = 0;
464 #ifndef DOXYGEN_PARSING
471 class AsyncChannel<T, 1> {
476 mutable Thread::Mutex mutex;
480 enum Status {normal, closed, destructing} status;
487 Thread::Mutex::Lock lock(mutex);
488 if (status == normal) {
495 bool waiting =
false;
496 Thread::Mutex::Lock lock(mutex);
497 if (status != normal)
return false;
508 while (full && status == normal) {
515 Thread::CancelBlock b;
516 pthread_cleanup_pop(
false);
520 Status local_status = status;
521 if (waiting) --waiters;
522 if (local_status != normal)
return false;
523 new (
static_cast<void*
>(datum)) T(obj);
530 bool waiting =
false;
531 Thread::Mutex::Lock lock(mutex);
542 while (!full && status == normal) {
549 Thread::CancelBlock b;
550 pthread_cleanup_pop(
false);
552 if (status == destructing) {
555 if (waiting) --waiters;
567 if (waiting) --waiters;
571 if (waiting) --waiters;
576 if (waiting) --waiters;
581 AsyncChannel(): waiters(0), full(false), status(normal),
582 datum(static_cast<T*>(std::malloc(sizeof(T)))) {
583 if (!datum)
throw std::bad_alloc();
588 status = destructing;
600 #ifdef CGU_USE_SCHED_YIELD
611 if (full) datum->~T();
618 #endif // DOXYGEN_PARSING
622 #endif // CGU_ASYNC_CHANNEL_H