CPN
Computational Process Networks
PthreadBase.h
Go to the documentation of this file.
1 //=============================================================================
2 // PthreadBase 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 PthreadBase_h
24 #define PthreadBase_h
25 #pragma once
26 
27 #ifdef EXTERNAL_TEMPLATES
28 # pragma interface
29 #endif
30 
31 #include <cpn/common.h>
33 #ifdef _POSIX_THREADS
34 
38 #include <signal.h>
39 
40 
42  public:
43  PthreadBase(void); // use "self"
44  PthreadBase(pthread_t thread); // use an existing thread
45 
46  // Actually create threads...
47  // (You probably should sub-class Pthread instead)
48  PthreadBase(void* (*entryFunc)(void*), void* arg = 0);
49  PthreadBase(PthreadAttr& attr, void* (*entryFunc)(void*), void* arg = 0);
50 
51  operator pthread_t*(void) { return &theThread; }
52  operator pthread_t(void) { return theThread; }
53 
54  int operator == (pthread_t t2) { return pthread_equal(theThread, t2); }
55 
56  void* Join(void);
57 
58  int Detach(void) { return pthread_detach(theThread); }
59  int Cancel(void) { return pthread_cancel(theThread); }
60 
61  static pthread_t Self(void) { return pthread_self(); }
62 
63  static void TestCancel(void) { pthread_testcancel(); }
64  static void Exit(void* value) { pthread_exit(value); }
65 
66 #ifndef MERCURY
67  int SendSignal(int sig) { return pthread_kill(theThread, sig); }
68  int Kill(int sig) { return pthread_kill(theThread, sig); }
69 
70  static int SendSignal(pthread_t thd, int sig) { return pthread_kill(thd, sig); }
71  static int Kill(pthread_t thd, int sig) { return pthread_kill(thd, sig); }
72 
73  static int GetSignalMask(sigset_t* oldSet)
74  { return pthread_sigmask(SIG_SETMASK, 0, oldSet); }
75  static int SetSignalMask(const sigset_t* set, sigset_t* oldSet = 0)
76  { return pthread_sigmask(SIG_SETMASK, set, oldSet); }
77  static int BlockSignals(const sigset_t* set, sigset_t* oldSet = 0)
78  { return pthread_sigmask(SIG_BLOCK, set, oldSet); }
79  static int UnblockSignals(const sigset_t* set, sigset_t* oldSet = 0)
80  { return pthread_sigmask(SIG_UNBLOCK, set, oldSet); }
81 #endif
82 
83  static int CancelEnable(void);
84  static int CancelDisable(void);
85 
87  public:
88  CancelProtected(void) { pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldState); }
89  ~CancelProtected(void) { pthread_setcancelstate(oldState, &oldState); }
90  private:
91  int oldState;
92  };
93 
94  static int CancelDeferred(void);
95  static int CancelAsynchronous(void);
96 
97  #if defined(_POSIX_PRIORITY_SCHEDULING) || defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
98  int SchedulePolicy(void);
99  int SchedulePriority(void);
100 
101  static void Yield(void) { sched_yield(); }
102  #endif
103 
104  #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
105  // Thread Scheduling Policies
106  // SCHED_FIFO: A thread runs until it blocks (then end of priority line).
107  // SCHED_RR: Time-sliced threads (with fixed priorities).
108  // SCHED_OTHER: Implementation dependent (generally Unix-like)
109 
110  void GetScheduleParams(int& policy, int& priority);
111  void SetScheduleParams(int policy, int priority);
112 
113  void GetScheduleParams(int& policy, PthreadScheduleParam& sp);
114  void SetScheduleParams(int policy, PthreadScheduleParam& sp);
115  #endif
116 
117  #ifdef _POSIX_PRIORITY_SCHEDULING
118  int PriorityMax(void) { return sched_get_priority_max(SchedulePolicy()); }
119  int PriorityMin(void) { return sched_get_priority_min(SchedulePolicy()); }
120 
121  static int PriorityMax(int policy) { return sched_get_priority_max(policy); }
122  static int PriorityMin(int policy) { return sched_get_priority_min(policy); }
123  #endif
124 
125  protected:
126  pthread_t theThread;
127 };
128 
129 
130 #endif
131 #endif
int SendSignal(int sig)
Definition: PthreadBase.h:67
static int UnblockSignals(const sigset_t *set, sigset_t *oldSet=0)
Definition: PthreadBase.h:79
static void Exit(void *value)
Definition: PthreadBase.h:64
int operator==(pthread_t t2)
Definition: PthreadBase.h:54
static int CancelEnable(void)
Definition: PthreadBase.cc:76
static int CancelDeferred(void)
Definition: PthreadBase.cc:95
static int Kill(pthread_t thd, int sig)
Definition: PthreadBase.h:71
static void TestCancel(void)
Definition: PthreadBase.h:63
static int SendSignal(pthread_t thd, int sig)
Definition: PthreadBase.h:70
static int CancelDisable(void)
Definition: PthreadBase.cc:86
static int GetSignalMask(sigset_t *oldSet)
Definition: PthreadBase.h:73
void * Join(void)
Definition: PthreadBase.cc:66
static pthread_t Self(void)
Definition: PthreadBase.h:61
int Cancel(void)
Definition: PthreadBase.h:59
static int CancelAsynchronous(void)
Definition: PthreadBase.cc:105
int Detach(void)
Definition: PthreadBase.h:58
PthreadBase(void)
Definition: PthreadBase.cc:34
static int SetSignalMask(const sigset_t *set, sigset_t *oldSet=0)
Definition: PthreadBase.h:75
pthread_t theThread
Definition: PthreadBase.h:126
int Kill(int sig)
Definition: PthreadBase.h:68
static int BlockSignals(const sigset_t *set, sigset_t *oldSet=0)
Definition: PthreadBase.h:77