ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
trequest.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2019 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7
8#if !defined(HPP_AWORX_ALIB_THREADS_MODEL_THREADMANAGER)
10#endif
11
12#if !defined(ALIB_DOX) // todo remove
13
14namespace alib::threads::model {
15
16TRequest::TRequest(void *pData)
17{
18 // save parameters
19 this->data= pData;
20 data2 = nullptr;
21 status = 0;
22
23 // initialize semaphore, it is local and has count 0 (wake up condition is > 0)
24 sem_init(&event, 0, 0);
25}
26
27TRequest::~TRequest()
28{
29 waitUntilProcessed();
30
31 if (sem_destroy(&event)==-1 && errno==EBUSY)
32 ALIB_MESSAGE("ATHR", "Can't destroy semaphore")
33}
34
35void TRequest::setProcessed()
36{
37 // set processed: semaphore is increased
38 sem_post(&event);
39}
40
41bool TRequest::isProcessed()
42{
43 int retVal;
44
45 // read count from the semaphore
46 lock.Acquire(ALIB_CALLER_PRUNED);
47 sem_getvalue(&event, &retVal);
48 lock.Release();
49
50 return (retVal != 0);
51}
52
53bool TRequest::waitUntilProcessed(int millisecs)
54{
55 bool retval;
56
57 lock.Acquire(ALIB_CALLER_PRUNED);
58 if (millisecs >= 0)
59 {
60 // calc breaktime
61 Ticks breakTime;
62 breakTime+= Ticks::Duration::FromAbsoluteMilliseconds( millisecs );
63
64 // wait until timeout
65 retval= true;
66 while (sem_trywait(&event) == -1 && errno == EAGAIN)
67 {
68 Ticks now;
69 if( breakTime > now )
70 Thread::SleepMicros( (breakTime - now).InAbsoluteMicroseconds() );
71 else
72 {
73 retval= false;
74 break;
75 }
76 }
77 }
78 else
79 {
80 // wait for an unlimited time (millisecs < 0)
81 // until semaphore is increased to > 0
82 sem_wait(&event);
83 retval= true;
84 }
85
86 // set semaphore to signaled again
87 // to maintain the signal for other waiters or for
88 // deletion
89 sem_post(&event);
90 lock.Release();
91
92 return retval;
93}
94
95
96} // namespace [alib::threads::model]
97
98#endif // !defined(ALIB_DOX)
99
#define ALIB_MESSAGE(...)
Definition alib.hpp:982
#define ALIB_CALLER_PRUNED
Definition alib.hpp:845
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.hpp:114