第三方框架改为仓内子集并完善构建忽略规则

完整 NIM_Duilib_Framework 仓库移至父目录,third_party 仅保留实际使用的 base、duilib、bin/resources 与许可证;.gitignore 覆盖 MSBuild 与打包产物。
This commit is contained in:
tongtong committed 2026-11-15 08:54:17 +08:00
1 parent 249e8548de
commit e1e26acc49
391 files changed
+342850

No files matched your search

@@ -0,0 +1,93 @@
// Lock implementation and Auto lock implementation
#ifndef BASE_SYNCHRONIZATION_LOCK_H
#define BASE_SYNCHRONIZATION_LOCK_H
#include "base/base_export.h"
#include "base/base_config.h"
#if defined(OS_WIN)
#include <windows.h>
#elif defined(OS_POSIX)
#include <pthread.h>
#endif
#include <assert.h>
namespace nbase
{
class BASE_EXPORT NLock
{
public:
#if defined(OS_WIN)
typedef CRITICAL_SECTION OSLockType;
#elif defined(OS_POSIX)
typedef pthread_mutex_t OSLockType;
#endif
NLock();
~NLock();
// If the lock is not held, take it and return true. If the lock is already
// held by something else, immediately return false.
bool Try();
// Take the lock, blocking until it is available if necessary.
void Lock();
// Release the lock. This must only be called by the lock's holder: after
// a successful call to Try, or a call to Lock.
void Unlock();
// Return the native underlying lock. Not supported for Windows builds.
#if !defined(OS_WIN)
OSLockType* os_lock() { return &os_lock_; }
#endif
private:
OSLockType os_lock_;
};
class BASE_EXPORT NAutoLock
{
public:
NAutoLock(NLock * lock)
{
assert(lock);
lock_ = lock;
lock_->Lock();
}
~NAutoLock()
{
if (lock_)
lock_->Unlock();
}
private:
NLock *lock_;
};
class BASE_EXPORT NAutoUnlock
{
public:
NAutoUnlock(NLock * lock)
{
assert(lock);
lock_ = lock;
lock_->Unlock();
}
~NAutoUnlock()
{
if (lock_)
lock_->Lock();
}
private:
NLock *lock_;
};
} // namespace nbase
#endif // BASE_SYNCHRONIZATION_LOCK_H
@@ -0,0 +1,46 @@
// Copyright (c) 2011, NetEase Inc. All rights reserved.
//
// Author: rl
// Date: 2011/6/9
//
// Lock implemetation on posix like Mac OS X/Linux/FreeBSD
#include "lock.h"
#if defined(OS_POSIX)
#include <errno.h>
namespace nbase
{
NLock::NLock()
{
// In release, go with the default lock attributes.
pthread_mutex_init(&os_lock_, NULL);
}
NLock::~NLock()
{
pthread_mutex_destroy(&os_lock_);
}
bool NLock::Try()
{
int rv = pthread_mutex_trylock(&os_lock_);
return rv == 0;
}
void NLock::Lock()
{
pthread_mutex_lock(&os_lock_);
}
void NLock::Unlock()
{
pthread_mutex_unlock(&os_lock_);
}
} // namespace
#endif // OS_WIN
@@ -0,0 +1,48 @@
// Copyright (c) 2011, NetEase Inc. All rights reserved.
//
// Author: rl
// Date: 2011/6/9
//
// Lock implemetation on windows
#include "lock.h"
#if defined(OS_WIN)
namespace nbase
{
NLock::NLock()
{
// The second parameter is the spin count, for short-held locks it avoid the
// contending thread from going to sleep which helps performance greatly.
::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
}
NLock::~NLock()
{
::DeleteCriticalSection(&os_lock_);
}
bool NLock::Try()
{
if (::TryEnterCriticalSection(&os_lock_))
{
return true;
}
return false;
}
void NLock::Lock()
{
::EnterCriticalSection(&os_lock_);
}
void NLock::Unlock()
{
::LeaveCriticalSection(&os_lock_);
}
} // namespace
#endif // OS_WIN
@@ -0,0 +1,46 @@
// Semaphore implementation
#ifndef BASE_SYNCHRONIZATION_SEMAPHORE_H_
#define BASE_SYNCHRONIZATION_SEMAPHORE_H_
#include "base/base_export.h"
#include "base/base_types.h"
#if defined(OS_WIN)
#include <windows.h>
#else
#include <sys/semaphore.h>
#endif
namespace nbase
{
class BASE_EXPORT Semaphore
{
public:
#if defined(OS_WIN)
typedef HANDLE OSSemaphoreType;
#elif defined(OS_POSIX)
typedef sem_t *OSSemaphoreType;
#endif
Semaphore();
~Semaphore();
public:
bool CreateSemaphore(const char *sem_name, uint32_t init_value, uint32_t max_value);
/* wait the semaphore until some post one*/
bool WaitSemaphore();
/* wait the semaphore until some post one or timeout */
bool WaitSemaphore(uint32_t timeout_ms);
/* post some semaphore*/
bool PostSemaphore(uint32_t post_num);
private:
uint32_t init_value_;
OSSemaphoreType semaphore_;
};
} // namespace nbase
#endif // BASE_SYNCHRONIZATION_SEMAPHORE_H_
@@ -0,0 +1,119 @@
// Copyright (c) 2011, NetEase Inc. All rights reserved.
//
// Author: rl
// Date: 2011/6/10
//
// Semaphore implementation on posix system
#include "base/base_config.h"
#if defined(OS_POSIX)
#include "base/synchronization/semaphore.h"
#include <unistd.h>
#include <fcntl.h>
namespace nbase
{
Semaphore::Semaphore()
{
init_value_ = 0;
semaphore_ = NULL;
}
Semaphore::~Semaphore()
{
if (semaphore_ != NULL)
{
sem_destroy(semaphore_);
}
}
bool Semaphore::CreateSemaphore(const char *sem_name, uint32_t init_value, uint32_t max_value)
{
if (init_value > max_value)
return false;
init_value_ = init_value;
/* note : here we will use named semaphore because iOS not supported unamed semaphore*/
if (sem_name == NULL)
{
sem_unlink("netease_noname_semaphore");
semaphore_ = sem_open("netease_noname_semaphore", O_CREAT | O_EXCL, 0, max_value);
sem_unlink("netease_noname_semaphore");
if (semaphore_ == SEM_FAILED)
{
return false;
}
for (uint32_t i = 0; i < max_value - init_value; ++i)
sem_wait(semaphore_);
}
else
{
sem_unlink(sem_name);
semaphore_ = sem_open(sem_name, O_EXCL);
sem_unlink(sem_name);
if (semaphore_ == SEM_FAILED)
{
semaphore_ = sem_open(sem_name, O_CREAT, 0, max_value);
if (semaphore_ == SEM_FAILED)
{
return false;
}
for (uint32_t i = 0; i < max_value - init_value; ++i)
sem_wait(semaphore_);
}
}
return true;
}
bool Semaphore::WaitSemaphore()
{
if (semaphore_)
{
if (sem_wait(semaphore_) == 0)
return true;
}
return false;
}
bool Semaphore::WaitSemaphore(uint32_t timeout_ms)
{
if (semaphore_)
{
int time_count = 0;
int ret = -1;
while ((ret != 0) && (time_count < timeout_ms))
{
ret = sem_trywait(semaphore_);
usleep(1000);
time_count++;
}
if (ret == 0)
return true;
}
return false;
}
bool Semaphore::PostSemaphore(uint32_t post_num)
{
if (semaphore_)
{
for (uint32_t i = 0; i < post_num; i++)
{
int ret = sem_post(semaphore_);
if (ret != 0)
return false;
}
return true;
}
return false;
}
} // namespace nbase
#endif // OS_WIN
@@ -0,0 +1,75 @@
// Copyright (c) 2011, NetEase Inc. All rights reserved.
//
// Author: rl
// Date: 2011/6/10
//
// Semaphore implementation on windows
#include "base/base_config.h"
#if defined(OS_WIN)
#include "base/synchronization/semaphore.h"
namespace nbase
{
Semaphore::Semaphore()
{
init_value_ = 0;
semaphore_ = NULL;
}
Semaphore::~Semaphore()
{
if (semaphore_ != NULL)
{
CloseHandle(semaphore_);
}
}
bool Semaphore::CreateSemaphore(const char *sem_name, uint32_t init_value, uint32_t max_value)
{
if (init_value > max_value)
return false;
init_value_ = init_value;
semaphore_ = ::CreateSemaphoreA(NULL, init_value, max_value, sem_name);
return true;
}
bool Semaphore::WaitSemaphore()
{
if (semaphore_)
{
if (WAIT_OBJECT_0 == ::WaitForSingleObject(semaphore_, INFINITE))
return true;
}
return false;
}
bool Semaphore::WaitSemaphore(uint32_t timeout_ms)
{
if (semaphore_)
{
if (WAIT_OBJECT_0 == ::WaitForSingleObject(semaphore_, (DWORD)timeout_ms))
return true;
}
return false;
}
bool Semaphore::PostSemaphore(uint32_t post_num)
{
if (semaphore_)
{
::ReleaseSemaphore(semaphore_, post_num, NULL);
return true;
}
return false;
}
} // namespace nbase
#endif // OS_WIN
@@ -0,0 +1,142 @@
// The base class of a cross flatform waitable event
// NOTE: support Windows only currently
#ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_
#define BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_
#include "base/base_config.h"
#include "base/base_types.h"
#if defined(OS_WIN)
#include <windows.h>
#elif defined(OS_POSIX)
#include <list>
#include <utility>
#include "base/memory/ref_count.h"
#include "base/synchronization/lock.h"
#endif // OS_POSIX
namespace nbase
{
class TimeDelta;
// This replaces INFINITE from Win32
static const int kNoTimeout = -1;
class WaitableEvent
{
public:
WaitableEvent(bool manual_reset, bool initially_signaled);
#if defined(OS_WIN)
explicit WaitableEvent(HANDLE handle);
// Releases ownership of the handle from this object.
HANDLE Release();
operator HANDLE() const { return handle_; }
#endif
~WaitableEvent();
// Put the event in the un-signaled state.
void Reset();
// Put the event in the signaled state. Causing any thread blocked on Wait
// to be woken up.
void Signal();
// Returns true if the event is in the signaled state, else false. If this
// is not a manual reset event, then this test will cause a reset.
bool IsSignaled();
// Wait indefinitely for the event to be signaled. Returns true if the event
// was signaled, else false is returned to indicate that waiting failed.
bool Wait();
// Wait up until max_time has passed for the event to be signaled. Returns
// true if the event was signaled. If this method returns false, then it
// does not necessarily mean that max_time was exceeded.
bool WaitTimeout(const TimeDelta &timeout);
// Wait, synchronously, on multiple events.
static size_t WaitMultiple(WaitableEvent **events, size_t count);
// This is a private helper class. It's here because it's used by friends of
// this class (such as WaitableEventWatcher) to be able to enqueue elements
// of the wait-list
class Waiter
{
public:
// Signal the waiter to wake up.
//
// Consider the case of a Waiter which is in multiple WaitableEvent's
// wait-lists. Each WaitableEvent is automatic-reset and two of them are
// signaled at the same time. Now, each will wake only the first waiter in
// the wake-list before resetting. However, if those two waiters happen to
// be the same object (as can happen if another thread didn't have a chance
// to dequeue the waiter from the other wait-list in time), two auto-resets
// will have happened, but only one waiter has been signaled!
//
// Because of this, a Waiter may "reject" a wake by returning false. In
// this case, the auto-reset WaitableEvent shouldn't act as if anything has
// been notified.
virtual bool Fire(WaitableEvent *signaling_event) = 0;
// Waiters may implement this in order to provide an extra condition for
// two Waiters to be considered equal. In WaitableEvent::Dequeue, if the
// pointers match then this function is called as a final check. See the
// comments in ~Handle for why.
virtual bool Compare(void *tag) = 0;
protected:
virtual ~Waiter() {}
};
private:
#if defined(OS_WIN)
HANDLE handle_;
#else
struct WaitableEventKernel : public ThreadSafeRefCount
{
public:
WaitableEventKernel(bool manual_reset, bool initially_signaled);
virtual ~WaitableEventKernel();
bool Dequeue(Waiter *waiter, void *tag);
nbase::NLock lock_;
const bool manual_reset_;
bool signaled_;
std::list<Waiter*> waiters_;
};
typedef std::pair<WaitableEvent*, size_t> WaiterAndIndex;
// When dealing with arrays of WaitableEvent*, we want to sort by the address
// of the WaitableEvent in order to have a globally consistent locking order.
// In that case we keep them, in sorted order, in an array of pairs where the
// second element is the index of the WaitableEvent in the original,
// unsorted, array.
static size_t EnqueueMultiple(WaiterAndIndex *waitables,
size_t count, Waiter *waiter);
bool SignalAll();
bool SignalOne();
void Enqueue(Waiter *waiter);
scoped_refptr<WaitableEventKernel> kernel_;
#endif
DISALLOW_COPY_AND_ASSIGN(WaitableEvent);
};
}
#endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_H_
@@ -0,0 +1,444 @@
// Copyright (c) 2011, NetEase Inc. All rights reserved.
// All rights reserved.
//
// Author: wrt(guangguang)
// Date: 2011/6/24
//
// The base class of a cross flatform waitable event
#include "base/synchronization/waitable_event.h"
#if defined(OS_POSIX)
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/framework/message_loop.h"
#include <vector>
// -----------------------------------------------------------------------------
// A WaitableEvent on POSIX is implemented as a wait-list. Currently we don't
// support cross-process events (where one process can signal an event which
// others are waiting on). Because of this, we can avoid having one thread per
// listener in several cases.
//
// The WaitableEvent maintains a list of waiters, protected by a lock. Each
// waiter is either an async wait, in which case we have a Task and the
// MessageLoop to run it on, or a blocking wait, in which case we have the
// condition variable to signal.
//
// Waiting involves grabbing the lock and adding oneself to the wait list. Async
// waits can be canceled, which means grabbing the lock and removing oneself
// from the list.
//
// Waiting on multiple events is handled by adding a single, synchronous wait to
// the wait-list of many events. An event passes a pointer to itself when
// firing a waiter and so we can store that pointer to find out which event
// triggered.
// -----------------------------------------------------------------------------
namespace nbase
{
WaitableEvent::WaitableEvent(bool manual_reset, bool initially_signaled)
: kernel_(new WaitableEventKernel(manual_reset, initially_signaled))
{
}
WaitableEvent::~WaitableEvent()
{
}
void WaitableEvent::Reset()
{
nbase::NAutoLock locked(&kernel_->lock_);
kernel_->signaled_ = false;
}
void WaitableEvent::Signal()
{
nbase::NAutoLock locked(&kernel_->lock_);
if (kernel_->signaled_)
return;
if (kernel_->manual_reset_)
{
SignalAll();
kernel_->signaled_ = true;
}
else
{
// In the case of auto reset, if no waiters were woken, we remain
// signaled.
if (!SignalOne())
kernel_->signaled_ = true;
}
}
bool WaitableEvent::IsSignaled()
{
nbase::NAutoLock locked(&(kernel_->lock_));
const bool result = kernel_->signaled_;
if (result && !kernel_->manual_reset_)
kernel_->signaled_ = false;
return result;
}
// -----------------------------------------------------------------------------
// Synchronous waits
// -----------------------------------------------------------------------------
// This is a synchronous waiter. The thread is waiting on the given condition
// variable and the fired flag in this object.
// -----------------------------------------------------------------------------
class SyncWaiter : public WaitableEvent::Waiter
{
public:
SyncWaiter()
: fired_(false),
signaling_event_(NULL),
lock_(),
cv_(&lock_)
{
}
bool Fire(WaitableEvent* signaling_event)
{
nbase::NAutoLock locked(&lock_);
if (fired_)
return false;
fired_ = true;
signaling_event_ = signaling_event;
cv_.Broadcast();
// Unlike AsyncWaiter objects, SyncWaiter objects are stack-allocated on
// the blocking thread's stack. There is no |delete this;| in Fire. The
// SyncWaiter object is destroyed when it goes out of scope.
return true;
}
WaitableEvent* signaling_event() const
{
return signaling_event_;
}
// ---------------------------------------------------------------------------
// These waiters are always stack allocated and don't delete themselves. Thus
// there's no problem and the ABA tag is the same as the object pointer.
// ---------------------------------------------------------------------------
bool Compare(void* tag)
{
return this == tag;
}
// ---------------------------------------------------------------------------
// Called with lock held.
// ---------------------------------------------------------------------------
bool fired() const
{
return fired_;
}
// ---------------------------------------------------------------------------
// During a TimedWait, we need a way to make sure that an auto-reset
// WaitableEvent doesn't think that this event has been signaled between
// unlocking it and removing it from the wait-list. Called with lock held.
// ---------------------------------------------------------------------------
void Disable()
{
fired_ = true;
}
nbase::NLock* lock()
{
return &lock_;
}
nbase::ConditionVariable* cv()
{
return &cv_;
}
private:
bool fired_;
WaitableEvent* signaling_event_; // The WaitableEvent which woke us
nbase::NLock lock_;
nbase::ConditionVariable cv_;
};
bool WaitableEvent::Wait()
{
return WaitTimeout(TimeDelta::FromSeconds(-1));
}
bool WaitableEvent::WaitTimeout(const TimeDelta& timeout)
{
const Time end_time(Time::Now() + timeout);
const bool finite_time = timeout.ToInternalValue() >= 0;
kernel_->lock_.Lock();
if (kernel_->signaled_)
{
if (!kernel_->manual_reset_)
{
// In this case we were signaled when we had no waiters. Now that
// someone has waited upon us, we can automatically reset.
kernel_->signaled_ = false;
}
kernel_->lock_.Unlock();
return true;
}
SyncWaiter sw;
sw.lock()->Lock();
Enqueue(&sw);
kernel_->lock_.Unlock();
// We are violating locking order here by holding the SyncWaiter lock but not
// the WaitableEvent lock. However, this is safe because we don't lock @lock_
// again before unlocking it.
for (;;)
{
const Time current_time(Time::Now());
if (sw.fired() || (finite_time && current_time >= end_time))
{
const bool return_value = sw.fired();
// We can't acquire @lock_ before releasing the SyncWaiter lock (because
// of locking order), however, in between the two a signal could be fired
// and @sw would accept it, however we will still return false, so the
// signal would be lost on an auto-reset WaitableEvent. Thus we call
// Disable which makes sw::Fire return false.
sw.Disable();
sw.lock()->Unlock();
kernel_->lock_.Lock();
kernel_->Dequeue(&sw, &sw);
kernel_->lock_.Unlock();
return return_value;
}
if (finite_time)
{
const TimeDelta max_wait(end_time - current_time);
sw.cv()->TimedWait(max_wait);
}
else
{
sw.cv()->Wait();
}
}
}
static bool cmp_fst_addr(const std::pair<WaitableEvent*,
unsigned> &a,
const std::pair<WaitableEvent*,
unsigned> &b)
{
return a.first < b.first;
}
// static
size_t WaitableEvent::WaitMultiple(WaitableEvent **events, size_t count)
{
assert(count > 0);
// We need to acquire the locks in a globally consistent order. Thus we sort
// the array of waitables by address. We actually sort a pairs so that we can
// map back to the original index values later.
std::vector<std::pair<WaitableEvent*, size_t> > waitables;
waitables.reserve(count);
for (size_t i = 0; i < count; ++i)
waitables.push_back(std::make_pair(events[i], i));
assert(count == waitables.size());
sort(waitables.begin(), waitables.end(), cmp_fst_addr);
// The set of waitables must be distinct. Since we have just sorted by
// address, we can check this cheaply by comparing pairs of consecutive
// elements.
for (size_t i = 0; i < waitables.size() - 1; ++i)
{
assert(waitables[i].first != waitables[i+1].first);
}
SyncWaiter sw;
const size_t r = EnqueueMultiple(&waitables[0], count, &sw);
if (r) {
// One of the events is already signaled. The SyncWaiter has not been
// enqueued anywhere. EnqueueMany returns the count of remaining waitables
// when the signaled one was seen, so the index of the signaled event is
// @count - @r.
return waitables[count - r].second;
}
// At this point, we hold the locks on all the WaitableEvents and we have
// enqueued our waiter in them all.
sw.lock()->Lock();
// Release the WaitableEvent locks in the reverse order
for (size_t i = 0; i < count; ++i)
{
waitables[count - (1 + i)].first->kernel_->lock_.Unlock();
}
for (;;)
{
if (sw.fired())
break;
sw.cv()->Wait();
}
sw.lock()->Unlock();
// The address of the WaitableEvent which fired is stored in the SyncWaiter.
WaitableEvent *const signaled_event = sw.signaling_event();
// This will store the index of the raw_waitables which fired.
size_t signaled_index = 0;
// Take the locks of each WaitableEvent in turn (except the signaled one) and
// remove our SyncWaiter from the wait-list
for (size_t i = 0; i < count; ++i)
{
if (events[i] != signaled_event)
{
events[i]->kernel_->lock_.Lock();
// There's no possible ABA issue with the address of the SyncWaiter here
// because it lives on the stack. Thus the tag value is just the pointer
// value again.
events[i]->kernel_->Dequeue(&sw, &sw);
events[i]->kernel_->lock_.Unlock();
}
else
{
signaled_index = i;
}
}
return signaled_index;
}
// -----------------------------------------------------------------------------
// If return value == 0:
// The locks of the WaitableEvents have been taken in order and the Waiter has
// been enqueued in the wait-list of each. None of the WaitableEvents are
// currently signaled
// else:
// None of the WaitableEvent locks are held. The Waiter has not been enqueued
// in any of them and the return value is the index of the first WaitableEvent
// which was signaled, from the end of the array.
// -----------------------------------------------------------------------------
// static
size_t WaitableEvent::EnqueueMultiple(std::pair<WaitableEvent*, size_t> *waitables,
size_t count,
Waiter *waiter)
{
if (!count)
return 0;
waitables[0].first->kernel_->lock_.Lock();
if (waitables[0].first->kernel_->signaled_) {
if (!waitables[0].first->kernel_->manual_reset_)
waitables[0].first->kernel_->signaled_ = false;
waitables[0].first->kernel_->lock_.Unlock();
return count;
}
const size_t r = EnqueueMultiple(waitables + 1, count - 1, waiter);
if (r)
{
waitables[0].first->kernel_->lock_.Unlock();
}
else
{
waitables[0].first->Enqueue(waiter);
}
return r;
}
WaitableEvent::WaitableEventKernel::WaitableEventKernel(bool manual_reset, bool initially_signaled)
: manual_reset_(manual_reset),
signaled_(initially_signaled)
{
}
WaitableEvent::WaitableEventKernel::~WaitableEventKernel()
{
}
// -----------------------------------------------------------------------------
// Wake all waiting waiters. Called with lock held.
// -----------------------------------------------------------------------------
bool WaitableEvent::SignalAll()
{
bool signaled_at_least_one = false;
for (std::list<Waiter*>::iterator
i = kernel_->waiters_.begin(); i != kernel_->waiters_.end(); ++i)
{
if ((*i)->Fire(this))
signaled_at_least_one = true;
}
kernel_->waiters_.clear();
return signaled_at_least_one;
}
// ---------------------------------------------------------------------------
// Try to wake a single waiter. Return true if one was woken. Called with lock
// held.
// ---------------------------------------------------------------------------
bool WaitableEvent::SignalOne()
{
for (;;)
{
if (kernel_->waiters_.empty())
return false;
const bool r = (*kernel_->waiters_.begin())->Fire(this);
kernel_->waiters_.pop_front();
if (r)
return true;
}
}
// -----------------------------------------------------------------------------
// Add a waiter to the list of those waiting. Called with lock held.
// -----------------------------------------------------------------------------
void WaitableEvent::Enqueue(Waiter* waiter)
{
kernel_->waiters_.push_back(waiter);
}
// -----------------------------------------------------------------------------
// Remove a waiter from the list of those waiting. Return true if the waiter was
// actually removed. Called with lock held.
// -----------------------------------------------------------------------------
bool WaitableEvent::WaitableEventKernel::Dequeue(Waiter *waiter, void *tag)
{
for (std::list<Waiter*>::iterator
i = waiters_.begin(); i != waiters_.end(); ++i)
{
if (*i == waiter && (*i)->Compare(tag))
{
waiters_.erase(i);
return true;
}
}
return false;
}
}
#endif // OS_POSIX
@@ -0,0 +1,90 @@
// Copyright (c) 2011, NetEase Inc. All rights reserved.
//
// Author: wrt(guangguang)
// Date: 2011/6/24
//
// The base class of a cross flatform waitable event
#include "base/synchronization/waitable_event.h"
#if defined(OS_WIN)
#include <math.h>
#include <assert.h>
#include "base/time/time.h"
namespace nbase
{
WaitableEvent::WaitableEvent(bool manual_reset, bool initially_signaled)
{
handle_ = ::CreateEventW(NULL, manual_reset, initially_signaled, NULL);
}
WaitableEvent::WaitableEvent(HANDLE handle)
{
handle_ = handle;
}
HANDLE WaitableEvent::Release()
{
HANDLE old_handle = handle_;
handle_ = NULL;
return old_handle;
}
WaitableEvent::~WaitableEvent()
{
if (handle_)
::CloseHandle(handle_);
}
void WaitableEvent::Reset()
{
assert(handle_);
::ResetEvent(handle_);
}
void WaitableEvent::Signal()
{
assert(handle_);
::SetEvent(handle_);
}
bool WaitableEvent::IsSignaled()
{
assert(handle_);
return ::WaitForSingleObject(handle_, 0) == WAIT_OBJECT_0;
}
bool WaitableEvent::Wait()
{
assert(handle_);
return WAIT_OBJECT_0 == ::WaitForSingleObject(handle_, INFINITE);
}
bool WaitableEvent::WaitTimeout(const TimeDelta &timeout)
{
assert(handle_);
assert(timeout.ToMillisecondsF() >= 0.0);
DWORD wait_result = ::WaitForSingleObject(handle_, static_cast<DWORD>(ceil(timeout.ToMillisecondsF())));
return WAIT_OBJECT_0 == wait_result;
}
size_t WaitableEvent::WaitMultiple(WaitableEvent **events, size_t count)
{
HANDLE handles[MAXIMUM_WAIT_OBJECTS];
assert(count <= MAXIMUM_WAIT_OBJECTS);
for (size_t i = 0; i < count; i++)
handles[i] = events[i]->handle_;
DWORD wait_result = ::WaitForMultipleObjects(static_cast<DWORD>(count),
handles,
FALSE,
INFINITE);
if (wait_result >= WAIT_OBJECT_0 + count)
return 0;
return wait_result - WAIT_OBJECT_0;
}
} // namespace nbase
#endif // OS_WIN