author | Peter D. Barnes, Jr. <barnes26@llnl.gov> |
Tue, 09 Dec 2014 13:17:49 -0800 | |
changeset 11097 | caafe12b0378 |
parent 7386 | 2310ed220a61 |
child 11538 | 397bd6465dd9 |
permissions | -rw-r--r-- |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
2 |
/* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
3 |
* Copyright (c) 2008 University of Washington |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
4 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License version 2 as |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
8 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
13 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
17 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
18 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
19 |
#ifndef SYSTEM_CONDITION_H |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
20 |
#define SYSTEM_CONDITION_H |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
21 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
22 |
#include "ptr.h" |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
23 |
|
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
24 |
/** |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
25 |
* @file |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
26 |
* @ingroup thread |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
27 |
* System-independent thread conditional wait. |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
28 |
*/ |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
29 |
|
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
30 |
namespace ns3 { |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
31 |
|
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
32 |
/** |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
33 |
* @ingroup system |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
34 |
* @defgroup thread Threading and Signaling. |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
35 |
* |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
36 |
* System-independent interfaces to threads, signal conditions, and mutex. |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
37 |
*/ |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
38 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
39 |
class SystemConditionPrivate; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
40 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
41 |
/** |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
42 |
* @ingroup thread |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
43 |
* @brief A class which provides a relatively platform-independent |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
44 |
* conditional-wait thread synchronization primitive. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
45 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
46 |
* It is often desirable to have a mechanism by which a thread can suspend its |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
47 |
* execution and relinquish the process until some condition to becomes true. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
48 |
* We provide platform-independent access to this OS-dependent capability with |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
49 |
* the SystemCondition class. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
50 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
51 |
* There are two ways to tell the underlying primitive that the condition has |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
52 |
* become true: Signal and Broadcast. Signal will only wake up one thread |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
53 |
* waiting on the condition (according to the OS scheduling policy); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
54 |
* Broadcast will wake up all of the threads waiting on the condition |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
55 |
* (cf. "The Thundering Herd"). |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
56 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
57 |
* In order to wait for the underlying condition, you also have two |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
58 |
* alternatives: Wait and TimedWait. The Wait call will wait forever for the |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
59 |
* condition to become true; but the TimedWait has a timeout. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
60 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
61 |
* The condition underlying this class is a simple boolean variable. It is |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
62 |
* set to false in each call to Wait and TimedWait. It is set to true in each |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
63 |
* call to Signal and Broadcast. This is a fairly simple-minded condition |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
64 |
* designed for |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
65 |
* |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
66 |
* A typical use case will be to call Wait() or TimedWait() in one thread |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
67 |
* context and put the processor to sleep until an event happens somewhere |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
68 |
* else that |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
69 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
70 |
class SystemCondition |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
71 |
{ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
72 |
public: |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
73 |
SystemCondition (); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
74 |
~SystemCondition (); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
75 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
76 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
77 |
* Set the value of the underlying condition. |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
78 |
* @param condition value |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
79 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
80 |
void SetCondition (bool condition); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
81 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
82 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
83 |
* Get the value of the underlying condition. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
84 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
85 |
bool GetCondition (void); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
86 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
87 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
88 |
* Release one thread if waiting for the condition to be true. If you want |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
89 |
* a waiting thread to return, you should have done a SetCondition (true) |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
90 |
* prior to calling. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
91 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
92 |
void Signal (void); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
93 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
94 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
95 |
* Release all threads waiting for the condition to be true. If you want |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
96 |
* all waiting threads to return, you should have done a SetCondition (true) |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
97 |
* prior to calling. |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
98 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
99 |
void Broadcast (void); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
100 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
101 |
/** |
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
102 |
* Wait, possibly forever, for the condition to be true. |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
103 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
104 |
void Wait (void); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
105 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
106 |
/** |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
107 |
* Wait a maximum of ns nanoseconds for the condition to be true. If the |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
108 |
* wait times out, return true else return false. |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
109 |
* @param ns maximum of nanoseconds to wait |
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
110 |
* @returns true if the timer expired, otherwise return false. |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
111 |
*/ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
112 |
bool TimedWait (uint64_t ns); |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
113 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
114 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
115 |
private: |
11097
caafe12b0378
[Doxygen] OS functions: filesystem, threading, wall clock.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7386
diff
changeset
|
116 |
/** The (system-dependent) implementation. */ |
7169
358f71a624d8
core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6821
diff
changeset
|
117 |
SystemConditionPrivate * m_priv; |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
118 |
}; |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
119 |
|
7386
2310ed220a61
standardize ns-3 namespace declaration format
Vedran Miletić <rivanvx@gmail.com>
parents:
7169
diff
changeset
|
120 |
} // namespace ns3 |
3425
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
121 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
122 |
#endif /* SYSTEM_CONDITION_H */ |
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
123 |
|
c69779f5e51e
add system threads and synchronization primitives
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
124 |