summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/thread_test_base.hpp
blob: eb994537f6e7f1b84f16be3efea8e9ea44f427e7 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <chrono>
#include <thread>

static std::atomic<int64_t> number;
#define INCREASE_NUMBER_AMOUNT 10000

using namespace std::chrono_literals;

class ThreadTestBase : public BaseTest, public FastOS_Runnable
{
private:
   std::mutex printMutex;

public:
   ThreadTestBase(void)
     : printMutex()
   {
   }
   virtual ~ThreadTestBase() {}

   void PrintProgress (char *string) override {
      std::lock_guard<std::mutex> guard(printMutex);
      BaseTest::PrintProgress(string);
   }

   void Run (FastOS_ThreadInterface *thread, void *arg) override;

   void WaitForThreadsToFinish (Job *jobs, int count) {
      int i;

      Progress(true, "Waiting for threads to finish...");
      for(;;) {
         bool threadsFinished=true;

         for (i=0; i<count; i++) {
            if (jobs[i].result == -1) {
               threadsFinished = false;
               break;
            }
         }

         std::this_thread::sleep_for(1us);

         if(threadsFinished)
            break;
      }

      Progress(true, "Threads finished");
   }
};


void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg)
{
   if(arg == nullptr)
      return;

   Job *job = static_cast<Job *>(arg);
   char someStack[15*1024];

   memset(someStack, 0, 15*1024);

   switch(job->code)
   {
      case SILENTNOP:
      {
         job->result = 1;
         break;
      }

      case NOP:
      {
         Progress(true, "Doing NOP");
         job->result = 1;
         break;
      }

      case PRINT_MESSAGE_AND_WAIT3MSEC:
      {
         Progress(true, "Thread printing message: [%s]", job->message);
         job->result = strlen(job->message);

          std::this_thread::sleep_for(3ms);
         break;
      }

      case INCREASE_NUMBER:
      {
         int result;

         std::unique_lock<std::mutex> guard;
         if(job->mutex != nullptr) {
             guard = std::unique_lock<std::mutex>(*job->mutex);
         }

         result = static_cast<int>(number.load(std::memory_order_relaxed));

         int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000;
         for (int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) {
            number.fetch_add(2, std::memory_order_relaxed);

            if (i == sleepOn)
                std::this_thread::sleep_for(1ms);
         }

         guard = std::unique_lock<std::mutex>();

         job->result = result;  // This marks the end of the thread

         break;
      }

      case WAIT_FOR_BREAK_FLAG:
      {
         for(;;) {
             std::this_thread::sleep_for(1us);

            if (thread->GetBreakFlag()) {
               Progress(true, "Thread %p got breakflag", thread);
               break;
            }
         }
         break;
      }

      case WAIT_FOR_THREAD_TO_FINISH:
      {
          std::unique_lock<std::mutex> guard;
          if (job->mutex != nullptr) {
              guard = std::unique_lock<std::mutex>(*job->mutex);
          }

         if (job->otherThread != nullptr)
             job->otherThread->Join();

         break;
      }

      case TEST_ID:
      {
         job->mutex->lock();          // Initially the parent threads owns the lock
         job->mutex->unlock();        // It is unlocked when we should start

         FastOS_ThreadId currentId = FastOS_Thread::GetCurrentThreadId();

         if(currentId == job->_threadId)
            job->result = 1;
         else
            job->result = -1;
         break;
      }

      default:
         Progress(false, "Unknown jobcode");
         break;
   }
}