1// Windows/Synchronization.cpp
2
3#include "StdAfx.h"
4
5#ifndef _WIN32
6
7#include "Synchronization.h"
8
9namespace NWindows {
10namespace NSynchronization {
11
12/*
13#define INFINITE  0xFFFFFFFF
14#define MAXIMUM_WAIT_OBJECTS 64
15#define STATUS_ABANDONED_WAIT_0 ((NTSTATUS)0x00000080L)
16#define WAIT_ABANDONED   ((STATUS_ABANDONED_WAIT_0 ) + 0 )
17#define WAIT_ABANDONED_0 ((STATUS_ABANDONED_WAIT_0 ) + 0 )
18// WINAPI
19DWORD WaitForMultipleObjects(DWORD count, const HANDLE *handles, BOOL wait_all, DWORD timeout);
20*/
21
22/* clang: we need to place some virtual functions in cpp file to rid off the warning:
23   'CBaseHandle_WFMO' has no out-of-line virtual method definitions;
24   its vtable will be emitted in every translation unit */
25CBaseHandle_WFMO::~CBaseHandle_WFMO()
26{
27}
28
29bool CBaseEvent_WFMO::IsSignaledAndUpdate()
30{
31  if (this->_state == false)
32    return false;
33  if (this->_manual_reset == false)
34    this->_state = false;
35  return true;
36}
37
38bool CSemaphore_WFMO::IsSignaledAndUpdate()
39{
40  if (this->_count == 0)
41    return false;
42  this->_count--;
43  return true;
44}
45
46DWORD WINAPI WaitForMultiObj_Any_Infinite(DWORD count, const CHandle_WFMO *handles)
47{
48  if (count < 1)
49  {
50    // abort();
51    SetLastError(EINVAL);
52    return WAIT_FAILED;
53  }
54
55  CSynchro *synchro = handles[0]->_sync;
56  synchro->Enter();
57
58  // #ifdef DEBUG_SYNCHRO
59  for (DWORD i = 1; i < count; i++)
60  {
61    if (synchro != handles[i]->_sync)
62    {
63      // abort();
64      synchro->Leave();
65      SetLastError(EINVAL);
66      return WAIT_FAILED;
67    }
68  }
69  // #endif
70
71  for (;;)
72  {
73    for (DWORD i = 0; i < count; i++)
74    {
75      if (handles[i]->IsSignaledAndUpdate())
76      {
77        synchro->Leave();
78        return WAIT_OBJECT_0 + i;
79      }
80    }
81    synchro->WaitCond();
82  }
83}
84
85}}
86
87#endif
88