Changeset 160ee4c


Ignore:
Timestamp:
May 3, 2022, 12:52:24 PM (2 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
db89655
Parents:
f56101f
Message:

Fix churn.go to use channels instead of semaphores.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/readyQ/churn.go

    rf56101f r160ee4c  
    22
    33import (
    4         "context"
    54        "flag"
    65        "fmt"
    76        "math/rand"
     7        "runtime"
    88        "sync"
    99        "sync/atomic"
    1010        "time"
    11         "golang.org/x/sync/semaphore"
    1211        "golang.org/x/text/language"
    1312        "golang.org/x/text/message"
    1413)
    1514
    16 func churner(result chan uint64, start *sync.WaitGroup, skip bool, spots [] * semaphore.Weighted) {
    17         ctx := context.TODO()
     15func churner(result chan uint64, start *sync.WaitGroup, spots [] chan struct {}) {
    1816        s := rand.NewSource(time.Now().UnixNano())
    1917        rng := rand.New(s)
     
    2422
    2523                sem := spots[ rng.Intn(100) % len(spots) ];
    26                 if !skip { sem.Release(1); };
    27                 sem.Acquire(ctx,1);
    28                 skip = false;
     24                sem <- (struct {}{})
     25                <- sem;
    2926
    3027                count += 1
     
    5249        wg.Add(1)
    5350
    54         spots := make([] * semaphore.Weighted, spot_cnt)
     51        spots := make([] chan struct {}, spot_cnt)
    5552        for i := range spots {
    56                 ctx := context.TODO()
    57                 spots[i] = semaphore.NewWeighted(20000)
    58                 spots[i].Acquire(ctx, 20000)
     53                spots[i] = make(chan struct {}, 1)
    5954        }
    6055
    6156        for i := 0; i < nthreads; i++ {
    62                 go churner(result, &wg, i < len(spots), spots)
     57                go churner(result, &wg, spots)
    6358        }
    6459        fmt.Printf("Starting\n");
     
    7469        fmt.Printf("\nDone\n")
    7570
    76         for i := range spots {
    77                 spots[i].Release(10000)
     71        for atomic.LoadInt64(&threads_left) != 0 {
     72                for i := range spots {
     73                        select {
     74                        case spots[i] <- (struct {}{}):
     75                        default:
     76                        }
     77                        runtime.Gosched()
     78                }
    7879        }
    7980
Note: See TracChangeset for help on using the changeset viewer.