SuperTuxKart
Loading...
Searching...
No Matches
can_be_deleted.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2010-2015 Joerg Henrichs
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_CAN_BE_DELETED
20#define HEADER_CAN_BE_DELETED
21
22#include "utils/log.hpp"
23#include "utils/time.hpp"
24
25#include <atomic>
26
38{
39private:
40 std::atomic_bool m_can_be_deleted;
41public:
43 CanBeDeleted() { m_can_be_deleted.store(false); }
44 // ------------------------------------------------------------------------
46 void setCanBeDeleted() { m_can_be_deleted.store(true); }
47 // ------------------------------------------------------------------------
48 void resetCanBeDeleted() { m_can_be_deleted.store(false); }
49 // ------------------------------------------------------------------------
50 bool canBeDeletedNow() { return m_can_be_deleted.load(); }
51 // ------------------------------------------------------------------------
55 bool waitForReadyToDeleted(float waiting_time)
56 {
57 if (m_can_be_deleted.load()) return true;
58 double start = StkTime::getRealTime();
59 Log::verbose("Thread", "Start waiting %lf", start);
60 while(1)
61 {
62 if(m_can_be_deleted.load())
63 {
64 Log::verbose("Thread",
65 "Waited %lf seconds for thread to become deleteable.",
66 StkTime::getRealTime()-start);
67 Log::verbose("Thread", "Stop waiting %lf", StkTime::getRealTime());
68 return true;
69 }
71 if(StkTime::getRealTime() - start > waiting_time)
72 {
73 Log::verbose("Thread", "Stop waiting %lf", StkTime::getRealTime());
74 Log::verbose("Thread", "Waited for more than %f seconds for "
75 "thread to become deleteable",
76 waiting_time);
77 return false;
78 }
79 } // while 1
80 } // waitForReadyToDeleted
81
82 // ------------------------------------------------------------------------
83}; // CanBeDeleted
84#endif
A simple class that a adds a function to wait with a timeout for a class to be ready to be deleted.
Definition: can_be_deleted.hpp:38
CanBeDeleted()
Set this instance to be not ready to be deleted.
Definition: can_be_deleted.hpp:43
void setCanBeDeleted()
Sets this instance to be ready to be deleted.
Definition: can_be_deleted.hpp:46
bool waitForReadyToDeleted(float waiting_time)
Waits at most t seconds for this class to be ready to be deleted.
Definition: can_be_deleted.hpp:55
static double getRealTime(long startAt=0)
Returns a time based on an arbitrary 'epoch' (e.g.
Definition: time.cpp:95
static void sleep(int msec)
Sleeps for the specified amount of time.
Definition: time.hpp:134