File: TestCryptThreading.cpp

package info (click to toggle)
kodi 2%3A19.1%2Bdfsg2-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 105,508 kB
  • sloc: cpp: 655,071; xml: 64,287; ansic: 37,640; sh: 8,574; python: 7,322; javascript: 2,325; makefile: 1,752; perl: 969; java: 513; cs: 390; objc: 340
file content (79 lines) | stat: -rw-r--r-- 2,166 bytes parent folder | download
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
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "utils/CryptThreading.h"
#if 0
#include "threads/SingleLock.h"

#include <atomic>
#include <set>
#include <thread>
#include <vector>

#include <gtest/gtest.h>

TEST(TestCryptThreadingInitializer, General)
{
  std::cout << "g_cryptThreadingInitializer address: " <<
    testing::PrintToString(&g_cryptThreadingInitializer) << "\n";
}

#define PVTID_NUM_THREADS 10

TEST(TestCryptThreadingInitializer, ProducesValidThreadIds)
{
  std::thread testThreads[PVTID_NUM_THREADS];

  std::vector<unsigned long> gatheredIds;
  CCriticalSection gatheredIdsMutex;

  std::atomic<unsigned long> threadsWaiting{0};
  std::atomic<bool> gate{false};

  for (int i = 0; i < PVTID_NUM_THREADS; i++)
  {
    testThreads[i] = std::thread([&gatheredIds, &gatheredIdsMutex, &threadsWaiting, &gate]() {
      threadsWaiting++;

      while (!gate);

      unsigned long myTid = g_cryptThreadingInitializer.GetCurrentCryptThreadId();

      {
        CSingleLock gatheredIdsLock(gatheredIdsMutex);
        gatheredIds.push_back(myTid);
      }
    });        
  }

  gate = true;

  for (int i = 0; i < PVTID_NUM_THREADS; i++)
    // This is somewhat dangerous but C++ doesn't have a join with timeout or a way to check
    // if a thread is still running.
    testThreads[i].join();

  // Verify that all of the thread id's are unique, and that there are 10 of them, and that none
  // of them is zero
  std::set<unsigned long> checkIds;
  for (std::vector<unsigned long>::const_iterator i = gatheredIds.begin(); i != gatheredIds.end(); ++i)
  {
    unsigned long curId = *i;
    // Thread ID isn't zero (since the sequence is pre-incremented and starts at 0)
    ASSERT_TRUE(curId != 0);

    // Make sure the ID isn't duplicated
    ASSERT_TRUE(checkIds.find(curId) == checkIds.end());
    checkIds.insert(curId);
  }

  // Make sure there's exactly PVTID_NUM_THREADS of them
  ASSERT_EQ(PVTID_NUM_THREADS, gatheredIds.size());
  ASSERT_EQ(PVTID_NUM_THREADS, checkIds.size());
}
#endif