1 /** 2 * Private turnstile implementation. 3 * 4 * Copyright: Copyright 2022 Garrett D'Amore 5 * Authors: Garrett D'Amore 6 * License: 7 * Distributed under the Boost Software License, Version 1.0. 8 * (See accompanying file LICENSE or https://www.boost.org/LICENSE_1_0.txt) 9 * SPDX-License-Identifier: BSL-1.0 10 */ 11 module dcell.turnstile; 12 13 import core.sync.condition; 14 15 package: 16 17 /** 18 * Turnstile implements a thread safe primitive -- applications can 19 * set or wait for a condition. 20 */ 21 class Turnstile 22 { 23 private Mutex m; 24 private Condition c; 25 private bool val; 26 27 this() 28 { 29 m = new Mutex(); 30 c = new Condition(m); 31 } 32 33 void set(this T)(bool b) if ((is(T == Turnstile) || is(T == shared Turnstile))) 34 { 35 synchronized (m) 36 { 37 val = b; 38 c.notifyAll(); 39 } 40 } 41 42 bool get(this T)() if ((is(T == Turnstile) || is(T == shared Turnstile))) 43 { 44 bool b; 45 synchronized (m) 46 { 47 b = val; 48 } 49 return b; 50 } 51 52 void wait(this T)(bool b) if (is(T == Turnstile) || is(T == shared Turnstile)) 53 { 54 synchronized (m) 55 { 56 while (val != b) 57 { 58 c.wait(); 59 } 60 } 61 } 62 }