Fix typo in license headers
[oweals/cde.git] / cde / programs / dtmail / include / DtMail / Threads.hh
1 /*
2  *+SNOTICE
3  *
4  *
5  *      $TOG: Threads.hh /main/5 1997/09/03 17:27:34 mgreess $
6  *
7  *      RESTRICTED CONFIDENTIAL INFORMATION:
8  *      
9  *      The information in this document is subject to special
10  *      restrictions in a confidential disclosure agreement bertween
11  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
12  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
13  *      Sun's specific written approval.  This documment and all copies
14  *      and derivative works thereof must be returned or destroyed at
15  *      Sun's request.
16  *
17  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
18  *
19  *+ENOTICE
20  */
21
22 #ifndef _THREADS_HH
23 #define _THREADS_HH
24
25 #include <sys/time.h>
26 #include <DtMail/DtLanguages.hh>
27
28 // This mutex must be used before calling any Dt* functions. They
29 // are extremely MT-UNSAFE and must be protected.
30 //
31 extern void * _DtMutex;
32
33 // This function will create an initialized mutex.
34 //
35 void * MutexInit(void);
36 void MutexDestroy(void * mutex);
37
38 // This class locks the specified mutex when constructed, and
39 // releases the lock on destruction. It is useful for mutexing
40 // a region of code automatically by including it in the scope
41 // of a set of braces.
42 //
43 class MutexLock : public DtCPlusPlusAllocator {
44   public:
45     MutexLock(void * mutex);
46     ~MutexLock(void);
47
48     void unlock(void);
49     void unlock_and_destroy(void);
50   private:
51     void *      _mutex;
52     int         _locked;
53 };
54
55 // The SafeScalar class is designed to allow safe access to scalar values
56 // from within an MT app. It should only be used with the common 32 bit
57 // integral values.
58 //
59 class SafeScalarImpl : public DtCPlusPlusAllocator {
60   public:
61     SafeScalarImpl(void);
62     ~SafeScalarImpl(void);
63
64     long operator = (const long);
65     long operator += (const long);
66     long operator -= (const long);
67     long operator *= (const long);
68     long operator /= (const long);
69
70     int operator == (const long);
71     int operator <= (const long);
72     int operator < (const long);
73     int operator >= (const long);
74     int operator > (const long);
75     int operator != (const long);
76
77     operator long(void);
78
79   private:
80     void *      _mutex;
81     long        _value;
82 };
83
84 template <class Scalar>
85 class SafeScalar : public DtCPlusPlusAllocator {
86   public:
87     SafeScalar(void) : _scalar() { }
88     ~SafeScalar(void) { }
89
90     Scalar operator= (const Scalar val) { return((Scalar)(long)(_scalar = (long)val)); }
91     Scalar operator += (const Scalar val) { return((Scalar)(long)(_scalar += (long)val)); }
92     Scalar operator -= (const Scalar val) { return((Scalar)(long)(_scalar -= (long)val)); }
93     Scalar operator *= (const Scalar val) { return((Scalar)(long)(_scalar *= (long)val)); }
94     Scalar operator /= (const Scalar val) { return((Scalar)(long)(_scalar /= (long)val)); }
95
96     int operator == (const Scalar val) { return(_scalar == (long)val); }
97     int operator <= (const Scalar val) { return(_scalar <= (long)val); }
98     int operator < (const Scalar val) { return(_scalar < (long)val); }
99     int operator >= (const Scalar val) { return(_scalar >= (long)val); }
100     int operator > (const Scalar val) { return(_scalar > (long)val); }
101     int operator != (const Scalar val) { return(_scalar != (long)val); }
102
103     operator Scalar (void) { return((Scalar)(long)_scalar); }
104
105   private:
106     SafeScalarImpl      _scalar;
107 };
108
109 // The condition class is used to block an appliction until the
110 // condition has changed. This is typically used while creating or
111 // destroying a class.
112 //
113 class Condition : public DtCPlusPlusAllocator {
114   public:
115     Condition(void);
116     ~Condition(void);
117
118     void setTrue(void);
119     void setFalse(void);
120
121     int state(void);
122     int operator=(int);
123     int operator += (int);
124     operator int(void);
125
126     void wait(void);
127
128     void waitTrue(void);
129 #ifdef DEAD_WOOD
130     void waitFalse(void);
131     void waitFor(int);
132     void waitGT(int); // wait >
133     void waitLT(int); // wait <
134     void waitProcStatus(void);
135 #endif /* DEAD_WOOD */
136
137   private:
138     void *      _mutex;
139     void *      _condition;
140     int         _state;
141 };
142
143 typedef unsigned int Thread;
144
145 typedef void * (*ThreadEntryPoint)(void *);
146
147 Thread ThreadCreate(ThreadEntryPoint, void * client_data);
148 Thread ThreadSelf(void);
149 void ThreadPrio(Thread, const int prio);
150 void ThreadKill(Thread, const int signal);
151 void ThreadExit(const int status);
152 void ThreadJoin(Thread);
153 time_t ThreadSleep(time_t seconds);
154
155 #endif