summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/thread_joinwait_test.cpp
blob: 6c7e8a7dc3cc9567b3ba6a5564de85c9b9c1247b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tests.h"
#include "job.h"
#include "thread_test_base.hpp"

class Thread_JoinWait_Test : public ThreadTestBase
{
   int Main () override;

   void SingleThreadJoinWaitMultipleTest(int variant)
   {
      bool rc=false;

      char testName[300];

      snprintf(testName, sizeof(testName), "Single Thread Join Wait Multiple Test %d", variant);
      TestHeader(testName);

      FastOS_ThreadPool pool;

      const int testThreads=5;
      int lastThreadNum = testThreads-1;
      int i;

      Job jobs[testThreads];

      std::mutex jobMutex;

      // The mutex is used to pause the first threads until we have created
      // the last one.
      jobMutex.lock();

      for(i=0; i<lastThreadNum; i++)
      {
         jobs[i].code = WAIT_FOR_THREAD_TO_FINISH;
         jobs[i].mutex = &jobMutex;
         jobs[i].ownThread = pool.NewThread(this, static_cast<void *>(&jobs[i]));

         rc = (jobs[i].ownThread != nullptr);
         Progress(rc, "Creating Thread %d", i+1);

         if(!rc)
            break;
      }

      if (rc)
      {
         jobs[lastThreadNum].code = (((variant & 2) != 0) ? NOP : PRINT_MESSAGE_AND_WAIT3MSEC);
         jobs[lastThreadNum].message = strdup("This is the thread that others wait for.");

         FastOS_ThreadInterface *lastThread;

         lastThread = pool.NewThread(this,
                                     static_cast<void *>
                                     (&jobs[lastThreadNum]));

         rc = (lastThread != nullptr);
         Progress(rc, "Creating last thread");

         if (rc)
         {
            for(i=0; i<lastThreadNum; i++) {
               jobs[i].otherThread = lastThread;
            }
         }
      }

      jobMutex.unlock();

      if ((variant & 1) != 0)
      {
         for (i=0; i<lastThreadNum; i++)
         {
            Progress(true, "Waiting for thread %d to finish using Join()", i+1);
            jobs[i].ownThread->Join();
            Progress(true, "Thread %d finished.", i+1);
         }
      }

      Progress(true, "Closing pool.");
      pool.Close();
      Progress(true, "Pool closed.");
      PrintSeparator();
   }

};

int Thread_JoinWait_Test::Main ()
{
   printf("grep for the string '%s' to detect failures.\n\n", failString);
   time_t before = time(0);

   SingleThreadJoinWaitMultipleTest(0);
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }
   SingleThreadJoinWaitMultipleTest(1);
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }
   SingleThreadJoinWaitMultipleTest(2);
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }
   SingleThreadJoinWaitMultipleTest(3);
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }
   SingleThreadJoinWaitMultipleTest(2);
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }
   SingleThreadJoinWaitMultipleTest(1);
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }
   SingleThreadJoinWaitMultipleTest(0);
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }

   printf("END OF TEST (%s)\n", _argv[0]);
   return allWasOk() ? 0 : 1;
}

int main (int argc, char **argv)
{
   Thread_JoinWait_Test app;
   setvbuf(stdout, nullptr, _IOLBF, 8192);
   return app.Entry(argc, argv);
}