1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 #include <cmajor/build/FiberExecutionContext.hpp>
 7 #include <cmajor/build/FiberConnection.hpp>
 8 #include <cmajor/build/BuildClient.hpp>
 9 #include <cmajor/build/BuildServer.hpp>
10 #include <cmajor/build/BuildServerMessage.hpp>
11 #include <soulng/util/Fiber.hpp>
12 #include <stdexcept>
13 
14 namespace cmajor { namespace build {
15 
16 void (*runServerFunction);
17 
18 FiberExecutionContext::FiberExecutionContext()
19 {
20     connection = CreateFiberConnection(GetLog());
21     client.reset(new BuildClient(connection.get()));
22     server.reset(new BuildServer(connection.get()));
23     SetServer(server.get());
24     void* clientFiber = ConvertThreadToFiber(nullptr);
25     runServerFunction = RunServer;
26     void* startAddress = reinterpret_cast<void*>(runServerFunction);
27     void* serverFiber = CreateFiber(1024 * 1024startAddressnullptr);
28     connection->SetFibers(serverFiberclientFiber);
29     SwitchToFiber(serverFiber);
30 }
31 
32 FiberExecutionContext::~FiberExecutionContext()
33 {
34     try
35     {
36         if (connection)
37         {
38             if (connection->ServerAlive())
39             {
40                 connection->SetServerAlive(false);
41                 if (connection->GetLog())
42                 {
43                     connection->GetLog()->Write("closing connection\n"nullptr);
44                 }
45                 CloseConnectionRequest closeConnectionRequest;
46                 closeConnectionRequest.SendTo(*connection);
47             }
48         }
49     }
50     catch (const std::exception&;)
51     {
52     }
53 }
54 
55 Connection* FiberExecutionContext::GetConnection()
56 {
57     return connection.get();
58 }
59 
60 } } // namespace cmajor::build