aboutsummaryrefslogtreecommitdiff
path: root/CPP/Windows/Synchronization.cpp
blob: d5542af4864be336695ed7153f70c4f9bba83816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Windows/Synchronization.cpp

#include "StdAfx.h"

#ifndef _WIN32

#include "Synchronization.h"

namespace NWindows {
namespace NSynchronization {

/*
#define INFINITE  0xFFFFFFFF
#define MAXIMUM_WAIT_OBJECTS 64
#define STATUS_ABANDONED_WAIT_0 ((NTSTATUS)0x00000080L)
#define WAIT_ABANDONED   ((STATUS_ABANDONED_WAIT_0 ) + 0 )
#define WAIT_ABANDONED_0 ((STATUS_ABANDONED_WAIT_0 ) + 0 )
// WINAPI
DWORD WaitForMultipleObjects(DWORD count, const HANDLE *handles, BOOL wait_all, DWORD timeout);
*/

/* clang: we need to place some virtual functions in cpp file to rid off the warning:
   'CBaseHandle_WFMO' has no out-of-line virtual method definitions;
   its vtable will be emitted in every translation unit */
CBaseHandle_WFMO::~CBaseHandle_WFMO()
{
}

bool CBaseEvent_WFMO::IsSignaledAndUpdate()
{
  if (this->_state == false)
    return false;
  if (this->_manual_reset == false)
    this->_state = false;
  return true;
}

bool CSemaphore_WFMO::IsSignaledAndUpdate()
{
  if (this->_count == 0)
    return false;
  this->_count--;
  return true;
}

DWORD WINAPI WaitForMultiObj_Any_Infinite(DWORD count, const CHandle_WFMO *handles)
{
  if (count < 1)
  {
    // abort();
    SetLastError(EINVAL);
    return WAIT_FAILED;
  }

  CSynchro *synchro = handles[0]->_sync;
  synchro->Enter();
  
  // #ifdef DEBUG_SYNCHRO
  for (DWORD i = 1; i < count; i++)
  {
    if (synchro != handles[i]->_sync)
    {
      // abort();
      synchro->Leave();
      SetLastError(EINVAL);
      return WAIT_FAILED;
    }
  }
  // #endif

  for (;;)
  {
    for (DWORD i = 0; i < count; i++)
    {
      if (handles[i]->IsSignaledAndUpdate())
      {
        synchro->Leave();
        return WAIT_OBJECT_0 + i;
      }
    }
    synchro->WaitCond();
  }
}

}}

#endif