CPN
Computational Process Networks
Main Page
Namespaces
Classes
Files
File List
File Members
include
cpn
threading
PthreadLib.h
Go to the documentation of this file.
1
//=============================================================================
2
// Pthread class
3
//-----------------------------------------------------------------------------
4
// POSIX Pthread class library
5
// Copyright (C) 1997-1999 The University of Texas
6
//
7
// This library is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Library General Public License as published
9
// by the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Library General Public License for more details.
16
//
17
// The GNU Public License is available in the file LICENSE, or you
18
// can write to the Free Software Foundation, Inc., 59 Temple Place -
19
// Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
20
// World Wide Web at http://www.fsf.org.
21
//=============================================================================
22
23
#ifndef Pthread_h
24
#define Pthread_h
25
#pragma once
26
27
#ifdef EXTERNAL_TEMPLATES
28
# pragma interface
29
#endif
30
#include <
cpn/common.h
>
31
#include <
cpn/threading/PthreadDefs.h
>
32
#ifdef _POSIX_THREADS
33
34
#include <
cpn/threading/PthreadBase.h
>
35
#include <
cpn/threading/PthreadMutex.h
>
36
#include <
cpn/threading/PthreadCondition.h
>
37
38
//=============================================================================
39
// WARNING: subclassing class Pthread can lead to race conditions.
40
// It is recommended that you instead use PthreadFunctional.
41
//-----------------------------------------------------------------------------
42
// This Pthread class was written in the style of the Java Thread class.
43
// Unfortunately, C++ has some behaviors that can cause problems when you
44
// use inheritance with a thread class. Some simple rules can mitigate:
45
// 1) NEVER call Start() in the constructor of a derived class. There are
46
// no virtual methods inside of C++ constructors, and Pthread::EntryPoint()
47
// must be virtual. This can lead to attempted execution before the
48
// vtables are initialized.
49
// 2) ALWAYS call Join() before doing anything else in the destructor of EVERY
50
// class that is an inheritance descendent of the Pthread class. Because of
51
// the calling order of destructors, your destructor could be executing at
52
// the same time as your spawned thread. This could lead to your thread
53
// operating on descructed data and deallocated memory.
54
//=============================================================================
55
56
class
Pthread
:
public
PthreadBase
{
57
public
:
58
Pthread
(
void
);
59
Pthread
(
const
PthreadAttr
& attr);
60
virtual
~Pthread
(
void
);
61
62
void
Start
(
void
);
63
64
int
Running
(
void
) {
65
PthreadMutexProtected
p(
mutex
);
66
return
state
==
running
;
67
}
68
72
int
Done
(
void
) {
73
PthreadMutexProtected
p(
mutex
);
74
return
state
==
done
||
state
==
joined
;
75
}
76
77
void
*
Join
(
void
);
78
79
protected
:
80
virtual
void
*
EntryPoint
(
void
) = 0;
81
// virtual void Cleanup(void) { }
82
83
private
:
84
PthreadMutex
mutex
;
85
PthreadCondition
cond
;
86
void
*
returnResult
;
87
bool
inJoin
;
88
89
enum
PthreadState
{
uninitialized
= 0,
created
,
started
,
running
,
done
,
joined
,
canceled
};
90
PthreadState
state
;
91
92
static
void
*
PthreadEntryPoint
(
void
* arg);
93
static
void
PthreadCleanup
(
void
* arg);
94
};
95
96
97
#endif
98
#endif
PthreadMutex.h
Pthread::uninitialized
Definition:
PthreadLib.h:89
Pthread::canceled
Definition:
PthreadLib.h:89
Pthread::started
Definition:
PthreadLib.h:89
Pthread::PthreadEntryPoint
static void * PthreadEntryPoint(void *arg)
Definition:
PthreadLib.cc:131
Pthread
Definition:
PthreadLib.h:56
Pthread::Pthread
Pthread(void)
Definition:
PthreadLib.cc:32
PthreadBase.h
Pthread::Running
int Running(void)
Definition:
PthreadLib.h:64
Pthread::PthreadCleanup
static void PthreadCleanup(void *arg)
Definition:
PthreadLib.cc:154
Pthread::Done
int Done(void)
Definition:
PthreadLib.h:72
Pthread::cond
PthreadCondition cond
Definition:
PthreadLib.h:85
Pthread::~Pthread
virtual ~Pthread(void)
Definition:
PthreadLib.cc:67
Pthread::state
PthreadState state
Definition:
PthreadLib.h:90
Pthread::mutex
PthreadMutex mutex
Definition:
PthreadLib.h:84
PthreadBase
Definition:
PthreadBase.h:41
Pthread::Join
void * Join(void)
Definition:
PthreadLib.cc:108
PthreadCondition.h
Pthread::running
Definition:
PthreadLib.h:89
Pthread::returnResult
void * returnResult
Definition:
PthreadLib.h:86
PthreadMutex
Definition:
PthreadMutex.h:40
PthreadAttr
Definition:
PthreadAttr.h:40
PthreadDefs.h
Pthread::PthreadState
PthreadState
Definition:
PthreadLib.h:89
PthreadMutexProtected
Definition:
PthreadMutex.h:61
PthreadCondition
Definition:
PthreadCondition.h:40
Pthread::done
Definition:
PthreadLib.h:89
Pthread::inJoin
bool inJoin
Definition:
PthreadLib.h:87
Pthread::created
Definition:
PthreadLib.h:89
common.h
Pthread::joined
Definition:
PthreadLib.h:89
Pthread::EntryPoint
virtual void * EntryPoint(void)=0
Pthread::Start
void Start(void)
Definition:
PthreadLib.cc:100
Generated by
1.8.5