github.com/puellanivis/breton@v0.2.16/lib/mapreduce/threadpool.go (about)

     1  package mapreduce
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  type threadPool struct {
     8  	ch chan struct{}
     9  }
    10  
    11  func newThreadPool(count int) *threadPool {
    12  	ch := make(chan struct{}, count)
    13  
    14  	for i := 0; i < count; i++ {
    15  		ch <- struct{}{}
    16  	}
    17  
    18  	return &threadPool{
    19  		ch: ch,
    20  	}
    21  }
    22  
    23  func (p *threadPool) wait(ctx context.Context) error {
    24  	select {
    25  	case <-p.ch:
    26  	case <-ctx.Done():
    27  		return ctx.Err()
    28  	}
    29  
    30  	return nil
    31  }
    32  
    33  func (p *threadPool) done(ctx context.Context) error {
    34  	select {
    35  	case p.ch <- struct{}{}:
    36  	case <-ctx.Done():
    37  		return ctx.Err()
    38  	}
    39  
    40  	return nil
    41  }