ngc 1.4.0
 
Loading...
Searching...
No Matches
ngcbTHREAD.hpp
Go to the documentation of this file.
1
7#ifndef ngcbTHREAD_H
8#define ngcbTHREAD_H
9
10
11#ifndef __cplusplus
12#error This is a C++ include file and cannot be used from plain C
13#endif
14
15#ifndef _REENTRANT
16#define _REENTRANT
17#endif
18
19/*
20 * System Headers
21 */
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <errno.h>
26#include <sys/socket.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
30#include <signal.h>
31#include <pthread.h>
32#include <fcntl.h>
33
37extern int ngcbInSignalHandler;
38
43{
45 pthread_mutex_t mutex_v;
46
48 pthread_cond_t lck;
49
51 size_t val;
52};
53
57typedef pthread_t ngcbTHREAD_T;
58
62extern "C" void *ngcbTHREAD_ExecProc(void *arg);
63
67class ngcbSEM {
68public:
70 ngcbSEM();
71
73 explicit ngcbSEM(unsigned int count);
74
76 ngcbSEM(unsigned int count, int check);
77
79 virtual ~ngcbSEM();
80
82 int Create(ngcbSEM_T *sp, unsigned int count, int check, char *erms = NULL);
83
85 int Create(ngcbSEM_T *sp, unsigned int count, char *erms = NULL);
86
88 int Destroy(ngcbSEM_T *sp, char *erms = NULL);
89
91 int Lock(ngcbSEM_T *sp);
92
94 int Unlock(ngcbSEM_T *sp);
95
97 int TryLock(ngcbSEM_T *sp);
98
100 int Post(ngcbSEM_T *sp);
101
103 int Set(ngcbSEM_T *sp, unsigned int cnt);
104
106 size_t WaitCnt(ngcbSEM_T *sp, size_t cnt);
107
109 int Wait(ngcbSEM_T *sp);
110
112 int TryWait(ngcbSEM_T *sp);
113
115 size_t Cnt(ngcbSEM_T *sp);
116
118 int Lock();
119
121 int Unlock();
122
124 int TryLock();
125
127 int Post();
128
130 int Set(unsigned int cnt);
131
133 size_t WaitCnt(size_t cnt);
134
136 int Wait();
137
139 int TryWait();
140
142 size_t Cnt();
143
144protected:
145
146private:
148 int self_;
149
151 ngcbSEM_T sem_;
152};
153
157class ngcbSEMC: public ngcbSEM
158{
159public:
161 explicit ngcbSEMC(unsigned int count) : ngcbSEM(count, 1) {}
162
164 virtual ~ngcbSEMC() {}
165
166protected:
167
168private:
169
170};
171
175class ngcbTHREAD;
176
180typedef void (ngcbTHREAD::*ngcbPROCESS_T)(ngcbTHREAD *thread, void *arg);
181
186public:
188 ngcbTHREAD();
189
191 ngcbTHREAD(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg,
192 size_t stack = 0);
193
195 virtual ~ngcbTHREAD();
196
198 virtual ngcbTHREAD *NewThread(ngcbTHREAD *app, ngcbPROCESS_T process,
199 void *arg, size_t stack = 0);
200
202 int ThrCreate(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg,
203 ngcbTHREAD_T *threadId, size_t stack, char *erms = NULL);
204
206 void ThrExit();
207
209 int ThrKill(ngcbTHREAD_T threadId, int sig, char *erms = NULL);
210
212 int ThrKill(int sig, char *erms = NULL);
213
215 int ThrJoin(ngcbTHREAD_T threadId, char *erms = NULL);
216
218 int ThrJoin(char *erms = NULL);
219
222
225
227 int Initialized();
228
230 int FdOut();
231
233 int FdIn();
234
236 void *ExecProc();
237
238protected:
239
240private:
242 ngcbPROCESS_T appProc_;
243
245 ngcbTHREAD *appClass_;
246
248 void *appArg_;
249
251 ngcbTHREAD_T thread_;
252
254 int self_;
255
257 int joined_;
258
260 int initialized_;
261
263 int pipe_[2];
264};
265
270{
271public:
273 ngcbTIMER &operator=(const ngcbTIMER &other);
274
276 ngcbTIMER(const ngcbTIMER &other);
277
279 explicit ngcbTIMER(int ms);
280
282 ngcbTIMER(int ms, int id);
283
285 ngcbTIMER(int periodic, int ms, int id);
286
288 ngcbTIMER(int after, int periodic, int ms, int id);
289
291 virtual ~ngcbTIMER();
292
294 int Fd();
295
297 int RecvId();
298
299protected:
300private:
302 int wakeup_;
303
305 unsigned char id_;
306
308 int periodic_;
309
311 int after_;
312
314 ngcbTHREAD *thr_;
315
317 ngcbTHREAD *Start_();
318
320 void Timer_(ngcbTHREAD *thr, void *arg);
321};
322
327{
328public:
330 explicit ngcbPTIMER(int ms):ngcbTIMER(1, ms, 0) {}
331
333 ngcbPTIMER(int ms, int id):ngcbTIMER(1, ms, id) {}
334
336 ngcbPTIMER(int after, int ms, int id):ngcbTIMER(after, 1, ms, id) {}
337
339 virtual ~ngcbPTIMER() {}
340protected:
341private:
342};
343
344#endif
ngcbPTIMER(int ms, int id)
Constructor with timer value and id.
Definition ngcbTHREAD.hpp:333
ngcbPTIMER(int after, int ms, int id)
Constructor with start delay (milliseconds), timer value and id.
Definition ngcbTHREAD.hpp:336
ngcbPTIMER(int ms)
Constructor with timer value (milliseconds)
Definition ngcbTHREAD.hpp:330
virtual ~ngcbPTIMER()
Destructor.
Definition ngcbTHREAD.hpp:339
ngcbSEMC(unsigned int count)
Constructor.
Definition ngcbTHREAD.hpp:161
virtual ~ngcbSEMC()
Destructor.
Definition ngcbTHREAD.hpp:164
int Lock()
Lock this semaphore.
Definition ngcbSEM.cpp:331
int Destroy(ngcbSEM_T *sp, char *erms=NULL)
Destroy a semaphore.
Definition ngcbSEM.cpp:133
int TryWait()
Try to wait for this semaphore.
Definition ngcbSEM.cpp:422
int Wait()
Wait for this semaphore.
Definition ngcbSEM.cpp:409
ngcbSEM()
Constructor.
Definition ngcbSEM.cpp:26
int TryLock()
Try to lock this semaphore.
Definition ngcbSEM.cpp:357
int Set(ngcbSEM_T *sp, unsigned int cnt)
Set semaphore counter.
Definition ngcbSEM.cpp:229
size_t Cnt()
Get actual value of this semaphore counter.
Definition ngcbSEM.cpp:435
virtual ~ngcbSEM()
Destructor.
Definition ngcbSEM.cpp:53
size_t WaitCnt(ngcbSEM_T *sp, size_t cnt)
Wait until counter is reached exactly or becomes zereo.
Definition ngcbSEM.cpp:247
int Unlock()
Unlock this semaphore.
Definition ngcbSEM.cpp:344
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:71
int Post()
Post this semaphore.
Definition ngcbSEM.cpp:370
Definition ngcbTHREAD.hpp:185
int FdOut()
Return file descrioptor to wakeup a thread.
Definition ngcbTHREAD.cpp:313
ngcbTHREAD()
Normal constructor.
Definition ngcbTHREAD.cpp:52
void ThrExit()
Thread exit.
Definition ngcbTHREAD.cpp:212
virtual ~ngcbTHREAD()
Destructor.
Definition ngcbTHREAD.cpp:128
int ThrJoin(ngcbTHREAD_T threadId, char *erms=NULL)
Wait for a thread.
Definition ngcbTHREAD.cpp:259
ngcbTHREAD_T ThreadId()
Get own thread-Id.
Definition ngcbTHREAD.cpp:309
int ThrEqual(ngcbTHREAD_T t1, ngcbTHREAD_T t2)
Test the equality of the two threads t1 and t2.
Definition ngcbTHREAD.cpp:304
int ThrKill(ngcbTHREAD_T threadId, int sig, char *erms=NULL)
Cancel thread by thread-id.
Definition ngcbTHREAD.cpp:221
int FdIn()
Return file descriptor to be used by the thread for wakeup call.
Definition ngcbTHREAD.cpp:315
void * ExecProc()
Entry point for c-linkage thread.
Definition ngcbTHREAD.cpp:317
int Initialized()
Return flag for successful initialization.
Definition ngcbTHREAD.cpp:311
int ThrCreate(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg, ngcbTHREAD_T *threadId, size_t stack, char *erms=NULL)
Thread creation.
Definition ngcbTHREAD.cpp:175
virtual ngcbTHREAD * NewThread(ngcbTHREAD *app, ngcbPROCESS_T process, void *arg, size_t stack=0)
Process to be overloaded.
Definition ngcbTHREAD.cpp:168
int Fd()
Return timer file descriptor for select call.
Definition ngcbTIMER.cpp:86
ngcbTIMER & operator=(const ngcbTIMER &other)
Operator =.
Definition ngcbTIMER.cpp:27
virtual ~ngcbTIMER()
Destructor.
Definition ngcbTIMER.cpp:81
ngcbTIMER(const ngcbTIMER &other)
Copy constructor.
Definition ngcbTIMER.cpp:40
int RecvId()
Receive timer id.
Definition ngcbTIMER.cpp:181
void(ngcbTHREAD::* ngcbPROCESS_T)(ngcbTHREAD *thread, void *arg)
Definition ngcbTHREAD.hpp:180
int ngcbInSignalHandler
Definition ngcbTHREAD.cpp:25
pthread_t ngcbTHREAD_T
Definition ngcbTHREAD.hpp:57
void * ngcbTHREAD_ExecProc(void *arg)
Definition ngcbTHREAD.cpp:30
Definition ngcbTHREAD.hpp:43
pthread_mutex_t mutex_v
Protection mutex.
Definition ngcbTHREAD.hpp:45
pthread_cond_t lck
Condition variable.
Definition ngcbTHREAD.hpp:48
size_t val
Semaphore counter value.
Definition ngcbTHREAD.hpp:51