blob: 622560a486b51cd45eb1b9ceee98f40a6237d7ad [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FBL_CONDITION_VARIABLE_H_
#define FBL_CONDITION_VARIABLE_H_
#ifdef __cplusplus
// ConditionVariable is a C++ helper class intended to wrap a condition variable synchronization
// primitive. It is also responsible for automatically initializing and destroying the internal
// object.
//
// This object is currently only supported in userspace.
#ifndef _KERNEL
#include <threads.h>
#include <fbl/macros.h>
#include <fbl/mutex.h>
#include <zircon/compiler.h>
#include <zircon/types.h>
namespace fbl {
class ConditionVariable {
public:
ConditionVariable() { cnd_init(&cond_); }
~ConditionVariable() { cnd_destroy(&cond_); }
DISALLOW_COPY_ASSIGN_AND_MOVE(ConditionVariable);
void Wait(Mutex* mutex) __TA_REQUIRES(mutex) {
cnd_wait(&cond_, mutex->GetInternal());
}
void Signal() {
cnd_signal(&cond_);
}
void Broadcast() {
cnd_broadcast(&cond_);
}
private:
cnd_t cond_;
};
}
#endif // ifndef _KERNEL
#endif // ifdef __cplusplus
#endif // FBL_CONDITION_VARIABLE_H_