• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polserver / polserver / 12889193353

21 Jan 2025 02:33PM UTC coverage: 57.449% (+0.01%) from 57.435%
12889193353

push

github

web-flow
Add `allow_negatives` to module function `util::RandomDiceRoll` (#748)

* implementation

* tests

* docs, core-changes

* address review comments
- no need for negative check with clamping

7 of 7 new or added lines in 2 files covered. (100.0%)

11 existing lines in 3 files now uncovered.

41274 of 71845 relevant lines covered (57.45%)

394287.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.61
/pol-core/pol/polsem.cpp
1
/** @file
2
 *
3
 * @par History
4
 */
5

6

7
#include "polsem.h"
8

9
#include <time.h>
10

11
#include "../clib/logfacility.h"
12
#include "../clib/passert.h"
13
#include "../clib/threadhelp.h"
14
#include "../clib/tracebuf.h"
15

16
#ifdef _WIN32
17
#include <process.h>
18
#else
19
#include <pthread.h>
20
#include <sys/time.h>
21
#endif
22

23
namespace Pol
24
{
25
namespace Core
26
{
27
size_t locker;
28
#ifdef _WIN32
29
void polsem_lock()
30
{
31
  size_t tid = threadhelp::thread_pid();
32
  EnterCriticalSection( &cs );
33
  passert_always( locker == 0 );
34
  locker = tid;
35
}
36

37
void polsem_unlock()
38
{
39
  size_t tid = GetCurrentThreadId();
40
  passert_always( locker == tid );
41
  locker = 0;
42
  LeaveCriticalSection( &cs );
43
}
44
#else
45
void polsem_lock()
62,056,720✔
46
{
47
  size_t tid = threadhelp::thread_pid();
62,056,720✔
48
  int res = pthread_mutex_lock( &polsem );
62,056,721✔
49
  if ( res != 0 || locker != 0 )
62,056,727✔
50
  {
51
    POLLOGLN( "pthread_mutex_lock: res={}, tid={}, locker={}", res, tid, locker );
×
52
  }
53
  passert_always( res == 0 );
62,056,727✔
54
  passert_always( locker == 0 );
62,056,727✔
55
  locker = tid;
62,056,727✔
56
}
62,056,727✔
57
void polsem_unlock()
62,056,727✔
58
{
59
  size_t tid = threadhelp::thread_pid();
62,056,727✔
60
  passert_always( locker == tid );
62,056,727✔
61
  locker = 0;
62,056,727✔
62
  int res = pthread_mutex_unlock( &polsem );
62,056,727✔
63
  if ( res != 0 )
62,056,727✔
64
  {
65
    POLLOG( "pthread_mutex_unlock: res={},tid={}", res, tid );
×
66
  }
67
  passert_always( res == 0 );
62,056,727✔
68
}
62,056,727✔
69

70
#endif
71

72

73
#ifdef _WIN32
74
CRITICAL_SECTION cs;
75
HANDLE hEvPulse;
76

77
HANDLE hEvTasksThread;
78

79
CRITICAL_SECTION csThread;
80
HANDLE hSemThread;
81

82
void init_ipc_vars()
83
{
84
  InitializeCriticalSection( &cs );
85
  hEvPulse = CreateEvent( nullptr, TRUE, FALSE, nullptr );
86

87
  hEvTasksThread = CreateEvent( nullptr, FALSE, FALSE, nullptr );
88

89
  InitializeCriticalSection( &csThread );
90
  hSemThread = CreateSemaphore( nullptr, 0, 1, nullptr );
91
}
92

93
void deinit_ipc_vars()
94
{
95
  CloseHandle( hSemThread );
96
  DeleteCriticalSection( &csThread );
97

98
  CloseHandle( hEvTasksThread );
99
  hEvTasksThread = nullptr;
100

101
  CloseHandle( hEvPulse );
102
  DeleteCriticalSection( &cs );
103
}
104
void send_pulse()
105
{
106
  TRACEBUF_ADDELEM( "Pulse", 1 );
107
  PulseEvent( hEvPulse );
108
}
109

110
void wait_for_pulse( unsigned int millis )
111
{
112
  WaitForSingleObject( hEvPulse, millis );
113
}
114

115
void wake_tasks_thread()
116
{
117
  SetEvent( hEvTasksThread );
118
}
119

120
void tasks_thread_sleep( unsigned int millis )
121
{
122
  WaitForSingleObject( hEvTasksThread, millis );
123
}
124
#else
125

126
pthread_mutexattr_t polsem_attr;
127
pthread_mutex_t polsem;
128
// pthread_mutex_t polsem = PTHREAD_MUTEX_INITIALIZER;
129
// pthread_mutex_t polsem = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
130

131
pthread_mutex_t pulse_mut = PTHREAD_MUTEX_INITIALIZER;
132
pthread_cond_t pulse_cond = PTHREAD_COND_INITIALIZER;
133

134
pthread_mutex_t task_pulse_mut = PTHREAD_MUTEX_INITIALIZER;
135
pthread_cond_t task_pulse_cond = PTHREAD_COND_INITIALIZER;
136

137
pthread_mutex_t threadstart_mut = PTHREAD_MUTEX_INITIALIZER;
138
pthread_mutex_t threadstart_pulse_mut = PTHREAD_MUTEX_INITIALIZER;
139
pthread_cond_t threadstart_pulse_cond = PTHREAD_COND_INITIALIZER;
140
bool thread_started;
141

142

143
pthread_mutex_t polsemdbg_mut = PTHREAD_MUTEX_INITIALIZER;
144

145
pthread_attr_t thread_attr;
146

147
void init_ipc_vars()
3✔
148
{
149
  int res;
150
  res = pthread_mutexattr_init( &polsem_attr );
3✔
151
  passert_always( res == 0 );
3✔
152

153
  /*
154
      res = pthread_mutexattr_setkind_np( &polsem_attr, PTHREAD_MUTEX_ERRORCHECK_NP );
155
      passert_always( res == 0 );
156

157
      res = pthread_mutexattr_settype( &polsem_attr, PTHREAD_MUTEX_ERRORCHECK );
158
      passert_always( res == 0 );
159
      */
160

161
  res = pthread_mutex_init( &polsem, &polsem_attr );
3✔
162
  passert_always( res == 0 );
3✔
163

164
  pthread_attr_init( &thread_attr );
3✔
165
  pthread_attr_setdetachstate( &thread_attr, PTHREAD_CREATE_DETACHED );
3✔
166
}
3✔
167

168
void deinit_ipc_vars() {}
3✔
169

170
void send_pulse()
169✔
171
{
172
  pthread_mutex_lock( &pulse_mut );
169✔
173
  pthread_cond_broadcast( &pulse_cond );
169✔
174
  pthread_mutex_unlock( &pulse_mut );
169✔
175
}
169✔
176

177
void calc_abs_timeout( struct timespec* ptimeout, unsigned int millis )
85✔
178
{
179
  struct timeval now;
180
  struct timezone tz;
181

182
  gettimeofday( &now, &tz );
85✔
183
  int add_sec = 0;
85✔
184
  if ( millis > 1000 )
85✔
185
  {
186
    add_sec = millis / 1000;
×
187
    millis -= ( add_sec * 1000 );
×
188
  }
189
  ptimeout->tv_sec = now.tv_sec + add_sec;
85✔
190

191
  ptimeout->tv_nsec = now.tv_usec * 1000 + millis * 1000000L;
85✔
192
  if ( ptimeout->tv_nsec >= 1000000000 )
85✔
193
  {
194
    ++ptimeout->tv_sec;
76✔
195
    ptimeout->tv_nsec -= 1000000000;
76✔
196
  }
197
}
85✔
198

UNCOV
199
void wait_for_pulse( unsigned int millis )
×
200
{
201
  struct timespec timeout;
202

UNCOV
203
  pthread_mutex_lock( &pulse_mut );
×
204

UNCOV
205
  calc_abs_timeout( &timeout, millis );
×
206

UNCOV
207
  pthread_cond_timedwait( &pulse_cond, &pulse_mut, &timeout );
×
208

UNCOV
209
  pthread_mutex_unlock( &pulse_mut );
×
UNCOV
210
}
×
211

212
void wake_tasks_thread()
419✔
213
{
214
  pthread_mutex_lock( &task_pulse_mut );
419✔
215
  pthread_cond_broadcast( &task_pulse_cond );
419✔
216
  pthread_mutex_unlock( &task_pulse_mut );
419✔
217
}
419✔
218

219
void tasks_thread_sleep( unsigned int millis )
85✔
220
{
221
  struct timespec timeout;
222

223
  pthread_mutex_lock( &task_pulse_mut );
85✔
224

225
  calc_abs_timeout( &timeout, millis );
85✔
226

227
  pthread_cond_timedwait( &task_pulse_cond, &task_pulse_mut, &timeout );
85✔
228

229
  pthread_mutex_unlock( &task_pulse_mut );
85✔
230
}
85✔
231

232
#endif
233
}  // namespace Core
234
}  // namespace Pol
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc