libcruft-util/job/event.hpp

62 lines
1.9 KiB
C++
Raw Normal View History

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_JOB_EVENT_HPP
#define CRUFT_UTIL_JOB_EVENT_HPP
#include <atomic>
namespace util::job {
/// a reusable synchronisation object that allows threads to wait until
/// notify is called.
///
/// there is no internal state so it is easy to create races between wait
/// and notify calls. this makes the class mostly suitable for recurring
/// events.
///
/// the user should ensure no callers are waiting at destruction time
/// otherwise they may remain blocked indefinitely.
///
/// the address of the object is important so it must _never_ be
/// relocated in any manner if any caller may be waiting. it may be safe
/// to do so if there are no callers waiting (but the relevant functions
/// are deleted for safety anyway).
class event {
public:
event ();
event (const event&) = delete;
event (event&&) = delete;
event& operator= (const event&) = delete;
event& operator= (event&&) = delete;
/// block until notified
void wait (void);
/// wake all threads that are waiting
int notify (void);
/// wait `count' threads that are waiting
int notify (int count);
private:
alignas (4) std::atomic<int> value;
};
}
#endif