summaryrefslogtreecommitdiffstats
path: root/src/simpletransaction.cpp
blob: 46874c3ee704f86dab29526fa33382f2162a24df (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
#include "simpletransaction.h"
#include <utility>

#ifdef QT_DEBUG
#include "debughelper.h"
#endif

using std::pair;
using namespace Enums;

/**
 * @brief simpleTransaction::transactionStart
 */
void simpleTransaction::transactionStart() {
#ifdef QT_DEBUG
  dbg() << "START" << transactionDepth;
#endif
  transactionDepth++;
}

/**
 * @brief simpleTransaction::transactionAdd
 * @param id
 */
void simpleTransaction::transactionAdd(PROCESS id) {
#ifdef QT_DEBUG
  dbg() << "ADD" << transactionDepth << id;
#endif
  if (transactionDepth > 0) {
    lastInTransaction = id;
  } else {
    transactionQueue.push(pair<PROCESS, PROCESS>(id, id));
  }
}

/**
 * @brief simpleTransaction::transactionEnd
 * @param pid
 */
void simpleTransaction::transactionEnd(PROCESS pid) {
#ifdef QT_DEBUG
  dbg() << "END" << transactionDepth;
#endif
  if (transactionDepth > 0) {
    transactionDepth--;
    if (transactionDepth == 0 && lastInTransaction != INVALID) {
      transactionQueue.push(pair<PROCESS, PROCESS>(lastInTransaction, pid));
      lastInTransaction = INVALID;
    }
  }
}

/**
 * @brief simpleTransaction::transactionIsOver
 * @param id
 * @return
 */
PROCESS simpleTransaction::transactionIsOver(PROCESS id) {
#ifdef QT_DEBUG
  dbg() << "OVER" << transactionDepth << id;
#endif
  if (!transactionQueue.empty() && id == transactionQueue.front().first) {
    PROCESS ret = transactionQueue.front().second;
    transactionQueue.pop();
    return ret;
  }
  return INVALID;
}