ngc 1.5.0
 
Loading...
Searching...
No Matches
ngcbTHREAD.hpp
Go to the documentation of this file.
1
8#ifndef ngcbTHREAD_H
9#define ngcbTHREAD_H
10
11
12#ifndef __cplusplus
13#error This is a C++ include file and cannot be used from plain C
14#endif
15
16#ifndef _REENTRANT
17#define _REENTRANT
18#endif
19
20/*
21 * System Headers
22 */
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <errno.h>
27#include <sys/socket.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
31#include <signal.h>
32#include <pthread.h>
33#include <fcntl.h>
34
38extern int ngcbInSignalHandler;
39
44{
46 pthread_mutex_t mutex_v;
47
49 pthread_cond_t lck;
50
52 size_t val;
53};
54
58typedef pthread_t ngcbTHREAD_T;
59
63extern "C" void *ngcbTHREAD_ExecProc(void *arg);
64
68class ngcbSEM {
69public:
71 ngcbSEM();
72
74 explicit ngcbSEM(unsigned int count);
75
77 ngcbSEM(unsigned int count, int check);
78
80 virtual ~ngcbSEM();
81
83 int Create(ngcbSEM_T *sp, unsigned int count, int check, char *erms = NULL);
84
86 int Create(ngcbSEM_T *sp, unsigned int count, char *erms = NULL);
87
89 int Destroy(ngcbSEM_T *sp, char *erms = NULL);
90
92 int Lock(ngcbSEM_T *sp);
93
95 int Unlock(ngcbSEM_T *sp);
96
98 int TryLock(ngcbSEM_T *sp);
99
101 int Post(ngcbSEM_T *sp);
102
104 int Set(ngcbSEM_T *sp, unsigned int cnt);
105
107 size_t WaitCnt(ngcbSEM_T *sp, size_t cnt);
108
110 int Wait(ngcbSEM_T *sp);
111
113 int TryWait(ngcbSEM_T *sp);
114
116 size_t Cnt(ngcbSEM_T *sp);
117
119 int Lock();
120
122 int Unlock();
123
125 int TryLock();
126
128 int Post();
129
131 int Set(unsigned int cnt);
132
134 size_t WaitCnt(size_t cnt);
135
137 int Wait();
138
140 int TryWait();
141
143 size_t Cnt();
144
145protected:
146
147private:
149 int self_;
150
152 ngcbSEM_T sem_;
153};
154
158class ngcbSEMC: public ngcbSEM
159{
160public:
162 explicit ngcbSEMC(unsigned int count) : ngcbSEM(count, 1) {}
163
165 virtual ~ngcbSEMC() {}
166
167protected:
168
169private:
170
171};
172
176class ngcbTHREAD;
177
181typedef void (ngcbTHREAD::*ngcbPROCESS_T)(ngcbTHREAD *thread, void *arg);
182
187public:
189 ngcbTHREAD();
190
192 ngcbTHREAD(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg,
193 size_t stack = 0);
194
196 virtual ~ngcbTHREAD();
197
199 virtual ngcbTHREAD *NewThread(ngcbTHREAD *app, ngcbPROCESS_T process,
200 void *arg, size_t stack = 0);
201
203 int ThrCreate(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg,
204 ngcbTHREAD_T *threadId, size_t stack, char *erms = NULL);
205
207 void ThrExit();
208
210 int ThrKill(ngcbTHREAD_T threadId, int sig, char *erms = NULL);
211
213 int ThrKill(int sig, char *erms = NULL);
214
216 int ThrJoin(ngcbTHREAD_T threadId, char *erms = NULL);
217
219 int ThrJoin(char *erms = NULL);
220
223
226
228 int Initialized();
229
231 int FdOut();
232
234 int FdIn();
235
237 void *ExecProc();
238
239protected:
240
241private:
243 ngcbPROCESS_T appProc_;
244
246 ngcbTHREAD *appClass_;
247
249 void *appArg_;
250
252 ngcbTHREAD_T thread_;
253
255 int self_;
256
258 int joined_;
259
261 int initialized_;
262
264 int pipe_[2];
265};
266
271{
272public:
274 ngcbTIMER &operator=(const ngcbTIMER &other);
275
277 ngcbTIMER(const ngcbTIMER &other);
278
280 explicit ngcbTIMER(int ms);
281
283 ngcbTIMER(int ms, int id);
284
286 ngcbTIMER(int periodic, int ms, int id);
287
289 ngcbTIMER(int after, int periodic, int ms, int id);
290
292 virtual ~ngcbTIMER();
293
295 int Fd();
296
298 int RecvId();
299
300protected:
301private:
303 int wakeup_;
304
306 unsigned char id_;
307
309 int periodic_;
310
312 int after_;
313
315 ngcbTHREAD *thr_;
316
318 ngcbTHREAD *Start_();
319
321 void Timer_(ngcbTHREAD *thr, void *arg);
322};
323
328{
329public:
331 explicit ngcbPTIMER(int ms):ngcbTIMER(1, ms, 0) {}
332
334 ngcbPTIMER(int ms, int id):ngcbTIMER(1, ms, id) {}
335
337 ngcbPTIMER(int after, int ms, int id):ngcbTIMER(after, 1, ms, id) {}
338
340 virtual ~ngcbPTIMER() {}
341protected:
342private:
343};
344
345#endif
ngcbPTIMER(int ms, int id)
Constructor with timer value and id.
Definition ngcbTHREAD.hpp:334
ngcbPTIMER(int after, int ms, int id)
Constructor with start delay (milliseconds), timer value and id.
Definition ngcbTHREAD.hpp:337
ngcbPTIMER(int ms)
Constructor with timer value (milliseconds)
Definition ngcbTHREAD.hpp:331
virtual ~ngcbPTIMER()
Destructor.
Definition ngcbTHREAD.hpp:340
ngcbSEMC(unsigned int count)
Constructor.
Definition ngcbTHREAD.hpp:162
virtual ~ngcbSEMC()
Destructor.
Definition ngcbTHREAD.hpp:165
int Lock()
Lock this semaphore.
Definition ngcbSEM.cpp:332
int Destroy(ngcbSEM_T *sp, char *erms=NULL)
Destroy a semaphore.
Definition ngcbSEM.cpp:134
int TryWait()
Try to wait for this semaphore.
Definition ngcbSEM.cpp:423
int Wait()
Wait for this semaphore.
Definition ngcbSEM.cpp:410
ngcbSEM()
Constructor.
Definition ngcbSEM.cpp:27
int TryLock()
Try to lock this semaphore.
Definition ngcbSEM.cpp:358
int Set(ngcbSEM_T *sp, unsigned int cnt)
Set semaphore counter.
Definition ngcbSEM.cpp:230
size_t Cnt()
Get actual value of this semaphore counter.
Definition ngcbSEM.cpp:436
virtual ~ngcbSEM()
Destructor.
Definition ngcbSEM.cpp:54
size_t WaitCnt(ngcbSEM_T *sp, size_t cnt)
Wait until counter is reached exactly or becomes zereo.
Definition ngcbSEM.cpp:248
int Unlock()
Unlock this semaphore.
Definition ngcbSEM.cpp:345
int Create(ngcbSEM_T *sp, unsigned int count, int check, char *erms=NULL)
Create a semaphore of given type (error checking on or off)
Definition ngcbSEM.cpp:72
int Post()
Post this semaphore.
Definition ngcbSEM.cpp:371
Definition ngcbTHREAD.hpp:186
int FdOut()
Return file descrioptor to wakeup a thread.
Definition ngcbTHREAD.cpp:314
ngcbTHREAD()
Normal constructor.
Definition ngcbTHREAD.cpp:53
void ThrExit()
Thread exit.
Definition ngcbTHREAD.cpp:213
virtual ~ngcbTHREAD()
Destructor.
Definition ngcbTHREAD.cpp:129
int ThrJoin(ngcbTHREAD_T threadId, char *erms=NULL)
Wait for a thread.
Definition ngcbTHREAD.cpp:260
ngcbTHREAD_T ThreadId()
Get own thread-Id.
Definition ngcbTHREAD.cpp:310
int ThrEqual(ngcbTHREAD_T t1, ngcbTHREAD_T t2)
Test the equality of the two threads t1 and t2.
Definition ngcbTHREAD.cpp:305
int ThrKill(ngcbTHREAD_T threadId, int sig, char *erms=NULL)
Cancel thread by thread-id.
Definition ngcbTHREAD.cpp:222
int FdIn()
Return file descriptor to be used by the thread for wakeup call.
Definition ngcbTHREAD.cpp:316
void * ExecProc()
Entry point for c-linkage thread.
Definition ngcbTHREAD.cpp:318
int Initialized()
Return flag for successful initialization.
Definition ngcbTHREAD.cpp:312
int ThrCreate(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg, ngcbTHREAD_T *threadId, size_t stack, char *erms=NULL)
Thread creation.
Definition ngcbTHREAD.cpp:176
virtual ngcbTHREAD * NewThread(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg, size_t stack=0)
Process to be overloaded.
Definition ngcbTHREAD.cpp:169
int Fd()
Return timer file descriptor for select call.
Definition ngcbTIMER.cpp:87
ngcbTIMER & operator=(const ngcbTIMER &other)
Operator =.
Definition ngcbTIMER.cpp:28
virtual ~ngcbTIMER()
Destructor.
Definition ngcbTIMER.cpp:82
ngcbTIMER(const ngcbTIMER &other)
Copy constructor.
Definition ngcbTIMER.cpp:41
int RecvId()
Receive timer id.
Definition ngcbTIMER.cpp:182
void(ngcbTHREAD::* ngcbPROCESS_T)(ngcbTHREAD *thread, void *arg)
Definition ngcbTHREAD.hpp:181
int ngcbInSignalHandler
Definition ngcbTHREAD.cpp:26
pthread_t ngcbTHREAD_T
Definition ngcbTHREAD.hpp:58
void * ngcbTHREAD_ExecProc(void *arg)
Definition ngcbTHREAD.cpp:31
Definition ngcbTHREAD.hpp:44
pthread_mutex_t mutex_v
Protection mutex.
Definition ngcbTHREAD.hpp:46
pthread_cond_t lck
Condition variable.
Definition ngcbTHREAD.hpp:49
size_t val
Semaphore counter value.
Definition ngcbTHREAD.hpp:52