Path: blob/master/thirdparty/embree/common/sys/barrier.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "intrinsics.h"6#include "sysinfo.h"7#include "atomic.h"89namespace embree10{11/*! system barrier using operating system */12class BarrierSys13{14public:1516/*! construction / destruction */17BarrierSys (size_t N = 0);18~BarrierSys ();1920private:21/*! class in non-copyable */22BarrierSys (const BarrierSys& other) DELETED; // do not implement23BarrierSys& operator= (const BarrierSys& other) DELETED; // do not implement2425public:26/*! initializes the barrier with some number of threads */27void init(size_t count);2829/*! lets calling thread wait in barrier */30void wait();3132private:33void* opaque;34};3536/*! fast active barrier using atomic counter */37struct BarrierActive38{39public:40BarrierActive ()41: cntr(0) {}4243void reset() {44cntr.store(0);45}4647void wait (size_t numThreads)48{49cntr++;50while (cntr.load() != numThreads)51pause_cpu();52}5354private:55std::atomic<size_t> cntr;56};5758/*! fast active barrier that does not require initialization to some number of threads */59struct BarrierActiveAutoReset60{61public:62BarrierActiveAutoReset ()63: cntr0(0), cntr1(0) {}6465void wait (size_t threadCount)66{67cntr0.fetch_add(1);68while (cntr0 != threadCount) pause_cpu();69cntr1.fetch_add(1);70while (cntr1 != threadCount) pause_cpu();71cntr0.fetch_add(-1);72while (cntr0 != 0) pause_cpu();73cntr1.fetch_add(-1);74while (cntr1 != 0) pause_cpu();75}7677private:78std::atomic<size_t> cntr0;79std::atomic<size_t> cntr1;80};8182class LinearBarrierActive83{84public:8586/*! construction and destruction */87LinearBarrierActive (size_t threadCount = 0);88~LinearBarrierActive();8990private:91/*! class in non-copyable */92LinearBarrierActive (const LinearBarrierActive& other) DELETED; // do not implement93LinearBarrierActive& operator= (const LinearBarrierActive& other) DELETED; // do not implement9495public:96/*! initializes the barrier with some number of threads */97void init(size_t threadCount);9899/*! thread with threadIndex waits in the barrier */100void wait (const size_t threadIndex);101102private:103volatile unsigned char* count0;104volatile unsigned char* count1;105volatile unsigned int mode;106volatile unsigned int flag0;107volatile unsigned int flag1;108volatile size_t threadCount;109};110}111112113114