-
Notifications
You must be signed in to change notification settings - Fork 0
/
vxCommon.h
876 lines (712 loc) · 16.4 KB
/
vxCommon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
#ifndef __VXCOMMON_H__
#define __VXCOMMON_H__
#include <set>
#include <map>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
#ifdef _WIN32
#include <hash_map>
#else // _WIN32
#include <ext/hash_map>
#endif // _WIN32
#ifdef _WIN32
#include <Windows.h>
#else
#include <pthread.h>
#endif
///////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#define SELECTANY __declspec(selectany)
#else // _WIN32
#define SELECTANY __attribute__((weak))
#endif // _WIN32
#ifdef _WIN32
///////////////////////////////////////////////////////////////////////////
// vxSRWLock
class vxSRWLock
{
// constructor/destructor
public:
vxSRWLock(void)
: m_lreadcount(0)
{
InitializeCriticalSection(&m_cs_reader);
InitializeCriticalSection(&m_cs_writer);
m_hevt_enable_write = CreateEvent(NULL, TRUE, TRUE, NULL);
}
~vxSRWLock(void)
{
#ifdef _DEBUG
WaitForSingleObject(m_hevt_enable_write, INFINITE);
#endif // _DEBUG
CloseHandle(m_hevt_enable_write);
DeleteCriticalSection(&m_cs_reader);
DeleteCriticalSection(&m_cs_writer);
}
// public interfaces
public:
inline int RdLock(void)
{
EnterCriticalSection(&m_cs_writer);
EnterCriticalSection(&m_cs_reader);
if (1 == InterlockedIncrement(&m_lreadcount))
ResetEvent(m_hevt_enable_write);
LeaveCriticalSection(&m_cs_reader);
LeaveCriticalSection(&m_cs_writer);
return 0;
}
inline int TryRdLock(void)
{
if (!TryEnterCriticalSection(&m_cs_writer))
return FALSE;
EnterCriticalSection(&m_cs_reader);
if (1 == InterlockedIncrement(&m_lreadcount))
ResetEvent(m_hevt_enable_write);
LeaveCriticalSection(&m_cs_reader);
LeaveCriticalSection(&m_cs_writer);
return TRUE;
}
inline int WrLock(void)
{
EnterCriticalSection(&m_cs_writer);
WaitForSingleObject(m_hevt_enable_write, INFINITE);
return 0;
}
inline int TryWrLock(void)
{
if (!TryEnterCriticalSection(&m_cs_writer))
return FALSE;
if (WAIT_TIMEOUT == WaitForSingleObject(m_hevt_enable_write, 0))
return FALSE;
return TRUE;
}
inline int UnLock(void)
{
EnterCriticalSection(&m_cs_reader);
if (0 == InterlockedExchange(&m_lreadcount, m_lreadcount))
LeaveCriticalSection(&m_cs_writer);
else if (0 == InterlockedDecrement(&m_lreadcount))
SetEvent(m_hevt_enable_write);
LeaveCriticalSection(&m_cs_reader);
return 0;
}
// class data
private:
CRITICAL_SECTION m_cs_reader;
volatile LONG m_lreadcount;
CRITICAL_SECTION m_cs_writer;
HANDLE m_hevt_enable_write;
};
///////////////////////////////////////////////////////////////////////////
// vxSimpleLock
class vxSimpleLock
{
// constructor/destructor
public:
vxSimpleLock(void)
{
InitializeCriticalSection(&m_cs_lock);
}
~vxSimpleLock(void)
{
#ifdef _DEBUG
EnterCriticalSection(&m_cs_lock);
LeaveCriticalSection(&m_cs_lock);
#endif // _DEBUG
DeleteCriticalSection(&m_cs_lock);
}
// public interfaces
public:
inline void Lock(void)
{
EnterCriticalSection(&m_cs_lock);
}
inline void UnLock(void)
{
LeaveCriticalSection(&m_cs_lock);
}
// class data
protected:
CRITICAL_SECTION m_cs_lock;
};
///////////////////////////////////////////////////////////////////////////
// vxSemaphore
class vxSemaphore
{
// constructor/destructor
public:
vxSemaphore(LONG lInitialCount, LONG lMaximumCount)
{
m_hSemaphore = CreateSemaphore(NULL, lInitialCount, lMaximumCount, NULL);
assert(NULL != m_hSemaphore);
}
~vxSemaphore(void)
{
if (NULL != m_hSemaphore)
{
CloseHandle(m_hSemaphore);
m_hSemaphore = NULL;
}
}
// public interfaces
public:
void Wait(void)
{
if (NULL == m_hSemaphore)
{
return;
}
DWORD dwWait = WaitForSingleObject(m_hSemaphore, INFINITE);
}
void Release(void)
{
if (NULL == m_hSemaphore)
{
return;
}
LONG lPreviousCount = 0;
bool bRelease = ReleaseSemaphore(m_hSemaphore, 1, &lPreviousCount);
}
// class data
protected:
HANDLE m_hSemaphore;
};
///////////////////////////////////////////////////////////////////////////
#else // _WIN32
///////////////////////////////////////////////////////////////////////////
// vxSRWLock
class vxSRWLock
{
// constructor/destructor
public:
vxSRWLock(void)
{
int iRet = pthread_rwlock_init(&m_rwlock, NULL);
assert(0 == iRet);
}
~vxSRWLock(void)
{
int iRet = pthread_rwlock_destroy(&m_rwlock);
assert(0 == iRet);
}
// operator methods
public:
inline int RdLock(void) { return pthread_rwlock_rdlock(&m_rwlock); }
inline int TryRdLock(void) { return pthread_rwlock_tryrdlock(&m_rwlock); }
inline int WrLock(void) { return pthread_rwlock_wrlock(&m_rwlock); }
inline int TryWrLock(void) { return pthread_rwlock_trywrlock(&m_rwlock); }
inline int UnLock(void) { return pthread_rwlock_unlock(&m_rwlock); }
// class data
protected:
pthread_rwlock_t m_rwlock;
};
///////////////////////////////////////////////////////////////////////////
// vxSimpleLock
class vxSimpleLock
{
// constructor/destructor
public:
vxSimpleLock(void)
{
int iRet = pthread_rwlock_init(&m_rwlock, NULL);
assert(0 == iRet);
}
~vxSimpleLock(void)
{
int iRet = pthread_rwlock_destroy(&m_rwlock);
assert(0 == iRet);
}
// public interfaces
public:
inline void Lock(void)
{
pthread_rwlock_wrlock(&m_rwlock);
}
inline void UnLock(void)
{
pthread_rwlock_unlock(&m_rwlock);
}
// class data
protected:
pthread_rwlock_t m_rwlock;
};
#endif // _WIN32
///////////////////////////////////////////////////////////////////////////
// SyncAutoLock
template< class _Lock = vxSRWLock, bool bWrite = true, bool bLock = true >
class SyncAutoLockT
{
// constructor/destructor
public:
SyncAutoLockT(_Lock *ptSynObj)
: m_ptSynObj(ptSynObj)
{
assert(NULL != m_ptSynObj);
if (bLock)
{
if (bWrite)
m_ptSynObj->WrLock();
else
m_ptSynObj->RdLock();
}
}
~SyncAutoLockT(void)
{
if (bLock)
{
m_ptSynObj->UnLock();
}
}
// class data
protected:
_Lock * m_ptSynObj;
};
class vxAutoLock
{
// constructor/destructor
public:
vxAutoLock(vxSimpleLock * ptlock)
{
m_ptlock = ptlock;
m_ptlock->Lock();
}
~vxAutoLock(void)
{
m_ptlock->UnLock();
}
// class data
protected:
vxSimpleLock * m_ptlock;
};
///////////////////////////////////////////////////////////////////////////
typedef SyncAutoLockT< vxSRWLock, false > vxAutoReadLock;
typedef SyncAutoLockT< vxSRWLock, true > vxAutoWriteLock;
typedef SyncAutoLockT< vxSRWLock, true > vxAutoLockEx;
///////////////////////////////////////////////////////////////////////////
// vxVector
template< class _Ty, class _Lock = vxSRWLock, class _Ax = std::allocator< _Ty > >
class vxVector : public std::vector< _Ty, _Ax >, public _Lock
{
// constructor/destructor
public:
vxVector(void) { }
~vxVector(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxDeque
template< class _Ty, class _Lock = vxSRWLock, class _Ax = std::allocator< _Ty > >
class vxDeque : public std::deque< _Ty, _Ax >, public _Lock
{
// constructor/destructor
public:
vxDeque(void) { }
~vxDeque(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxList
template< class _Ty, class _Lock = vxSRWLock, class _Ax = std::allocator< _Ty > >
class vxList : public std::list< _Ty, _Ax >, public _Lock
{
// constructor/destructor
public:
vxList(void) { }
~vxList(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxQueue
template< class _Ty, class _Lock = vxSRWLock, class _Container = std::deque< _Ty > >
class vxQueue : public std::queue< _Ty, _Container >, public _Lock
{
// constructor/destructor
public:
vxQueue(void) { }
~vxQueue(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxStack
template< class _Ty, class _Lock = vxSRWLock, class _Container = std::deque< _Ty > >
class vxStack : public std::stack< _Ty, _Container >, public _Lock
{
// constructor/destructor
public:
vxStack(void) { }
~vxStack(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxSet
template< class _Kty, class _Lock = vxSRWLock, class _Pr = std::less< _Kty >, class _Alloc = std::allocator< _Kty > >
class vxSet : public std::set< _Kty, _Pr, _Alloc >, public _Lock
{
// constructor/destructor
public:
vxSet(void) { }
~vxSet(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxMap
template< class _Kty, class _Ty, class _Lock = vxSRWLock, class _Pr = std::less< _Kty >, class _Alloc = std::allocator< std::pair< const _Kty, _Ty > > >
class vxMap : public std::map< _Kty, _Ty, _Pr, _Alloc >, public _Lock
{
// constructor/destructor
public:
vxMap(void) { }
~vxMap(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxMultimap
template< class _Kty, class _Ty, class _Lock = vxSRWLock, class _Pr = std::less< _Kty >, class _Alloc = std::allocator< std::pair< const _Kty, _Ty > > >
class vxMultimap : public std::multimap< _Kty, _Ty, _Pr, _Alloc >, public _Lock
{
// constructor/destructor
public:
vxMultimap(void) { }
~vxMultimap(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxHash_map
template<class _Kty, class _Ty, class _Lock = vxSRWLock, class _Tr = hash_compare< _Kty, std::less< _Kty > >, class _Alloc = std::allocator< std::pair< const _Kty, _Ty > > >
class vxHash_map : public stdext::hash_map< _Kty, _Ty, _Tr, _Alloc >, public _Lock
{
// constructor/destructor
public:
vxHash_map(void) { }
~vxHash_map(void) { }
};
///////////////////////////////////////////////////////////////////////////
// vxCircularQueue
template< class _Ty >
class vxCircularQueue
{
// const value define
public:
typedef enum ConstValueID
{
DEF_CIRQUEUE_SIZE = 10,
} ConstValueID;
// constructor/destructor
public:
vxCircularQueue(size_t st_size = DEF_CIRQUEUE_SIZE)
: m_st_front_pos(0)
, m_st_size(0)
{
assert(st_size > 0);
m_vec_items.resize(st_size);
}
~vxCircularQueue(void)
{
m_vec_items.clear();
}
// public interfaces
public:
inline size_t capacity_size(void) const
{
return m_vec_items.size();
}
inline size_t size(void) const
{
return m_st_size;
}
inline bool empty(void) const
{
return (0 == m_st_size);
}
inline bool full(void) const
{
return (size() == capacity_size());
}
bool front(_Ty& _Val)
{
if (empty())
{
return false;
}
_Val = m_vec_items[m_st_front_pos];
return true;
}
bool pop_front(void)
{
if (empty())
{
return false;
}
m_st_front_pos = (m_st_front_pos + 1) % capacity_size();
--m_st_size;
return true;
}
bool push_back(const _Ty& _Val)
{
if (full())
{
return false;
}
m_vec_items[(m_st_front_pos + m_st_size) % capacity_size()] = _Val;
m_st_size = (m_st_size + 1) % (capacity_size() + 1);
return true;
}
void resize(size_t st_new_size)
{
assert(st_new_size > 0);
m_st_front_pos = m_st_size = 0;
m_vec_items.resize(st_new_size);
}
void clear(void)
{
m_st_front_pos = 0;
m_st_size = 0;
size_t st_vec_size = m_vec_items.size();
m_vec_items.clear();
m_vec_items.resize(st_vec_size);
}
const _Ty& operator [] (size_t st_index) const
{
assert((st_index >= 0) && (st_index < size()));
size_t st_pos = (m_st_front_pos + st_index) % capacity_size();
return m_vec_items[st_pos];
}
_Ty& operator [] (size_t st_index)
{
assert((st_index >= 0) && (st_index < size()));
size_t st_pos = (m_st_front_pos + st_index) % capacity_size();
return m_vec_items[st_pos];
}
_Ty& operator = (const typename vxCircularQueue< _Ty > & obj)
{
if (this != obj)
{
m_vec_items = obj.m_vec_items;
m_st_front_pos = obj.m_st_front_pos;
m_st_size = obj.m_st_size;
}
return this;
}
// class data
protected:
size_t m_st_front_pos;
size_t m_st_size;
typename std::vector< _Ty > m_vec_items;
};
///////////////////////////////////////////////////////////////////////////
// vxPCQueue : producer - consumer queue
template< class _Ty, const size_t _DefMaxSemaphoreCount = 100 >
class vxPCQueue
{
// constructor/destructor
public:
vxPCQueue(size_t st_max_semaphore = _DefMaxSemaphoreCount)
: m_semaphore_in(st_max_semaphore, st_max_semaphore)
, m_semaphore_out(0, st_max_semaphore)
{
}
~vxPCQueue(void)
{
clear();
}
// public interfaces
public:
void push(const _Ty &_Val)
{
m_semaphore_in.Wait();
m_queue.Lock();
m_queue.push_back(_Val);
m_queue.UnLock();
m_semaphore_out.Release();
}
bool pop(_Ty &_Val)
{
bool bVal = false;
m_semaphore_out.Wait();
m_queue.Lock();
if (m_queue.size() > 0)
{
_Val = m_queue.front();
m_queue.pop_front();
bVal = true;
}
m_queue.UnLock();
m_semaphore_in.Release();
return bVal;
}
size_t size(void)
{
m_queue.Lock();
size_t st_size = m_queue.size();
m_queue.UnLock();
return st_size;
}
void clear(void)
{
m_queue.Lock();
m_queue.clear();
m_queue.UnLock();
}
// class data
protected:
vxSemaphore m_semaphore_in;
vxSemaphore m_semaphore_out;
typename vxDeque< _Ty, vxSimpleLock > m_queue;
};
///////////////////////////////////////////////////////////////////////////
// vxObjectPool
template < class _Ty >
class ObjectAlloctor
{
public:
static _Ty * alloc(void * pv_param)
{
return new _Ty();
}
static void initial(_Ty * _obj_ptr, void * pv_param)
{
}
static void recyc(_Ty *_obj_ptr)
{
}
static void destroy(_Ty *& _obj_ptr)
{
delete _obj_ptr; _obj_ptr = NULL;
}
};
template< class _Ty, class _ObjAllocator = ObjectAlloctor< _Ty > >
class vxObjectPool
{
// constructor/destructor
public:
vxObjectPool(void)
{
}
~vxObjectPool(void)
{
clear();
}
// class property
public:
// public interfaces
public:
_Ty * alloc(void * pv_param)
{
vxAutoLockEx autoLock(&m_sync_lock);
_Ty * _obj_ptr = NULL;
if (m_lst_recyc.empty())
{
_obj_ptr = _ObjAllocator::alloc(pv_param);
if (NULL != _obj_ptr)
{
m_lst_alloc.push_back(_obj_ptr);
}
}
else
{
_obj_ptr = m_lst_recyc.front();
m_lst_recyc.pop_front();
_ObjAllocator::initial(_obj_ptr, pv_param);
}
return _obj_ptr;
}
void recyc(_Ty *_obj_ptr)
{
if (NULL != _obj_ptr)
{
vxAutoLockEx autoLock(&m_sync_lock);
m_lst_recyc.push_back(_obj_ptr);
_ObjAllocator::recyc(_obj_ptr);
}
}
size_t count(void)
{
vxAutoLockEx autoLock(&m_sync_lock);
return (m_lst_alloc.size() - m_lst_recyc.size());
}
size_t size(void)
{
vxAutoLockEx autoLock(&m_sync_lock);
return m_lst_alloc.size();
}
void clear(void)
{
vxAutoLockEx autoLock(&m_sync_lock);
std::list< _Ty * >::iterator itlst = m_lst_alloc.begin();
for (; itlst != m_lst_alloc.end(); ++itlst)
{
_ObjAllocator::destroy(*itlst);
}
m_lst_alloc.clear();
m_lst_recyc.clear();
}
// class data
protected:
vxSRWLock m_sync_lock;
typename std::list< _Ty * > m_lst_alloc;
typename std::list< _Ty * > m_lst_recyc;
};
//////////////////////////////////////////////////////////////////////////
// vxSington class
typedef void (*SIDestroy)(void);
class SingtonMangr
{
// static methods
public:
static void RegInstanceDestroy(SIDestroy sid)
{
s_setSID.insert(sid);
}
static void ReleaseSingtonInstance(void)
{
vxSet< SIDestroy >::iterator itset = s_setSID.begin();
for (; itset != s_setSID.end(); ++itset)
{
(*itset)();
}
}
protected:
static vxSet< SIDestroy > s_setSID;
};
SELECTANY vxSet< SIDestroy > SingtonMangr::s_setSID;
template< class _Ty, class _Lock = vxSimpleLock >
class vxSington
{
// constructor/destructor
protected:
vxSington() { }
virtual ~vxSington() { }
public:
static _Ty& Instance(void)
{
if (NULL == s_instance)
{
s_lock.Lock();
if (NULL == s_instance)
{
s_instance = new _Ty();
SingtonMangr::RegInstanceDestroy(vxSington< _Ty >::Destroy);
}
s_lock.UnLock();
}
return *s_instance;
}
private:
static void Destroy(void)
{
if (s_instance != NULL)
{
delete s_instance;
s_instance = NULL;
}
}
static _Lock s_lock;
static _Ty * volatile s_instance;
};
template< class _Ty, class _Lock > \
SELECTANY _Lock vxSington< _Ty, _Lock >::s_lock;
template< class _Ty, class _Lock > \
SELECTANY _Ty * volatile vxSington< _Ty, _Lock >::s_instance = NULL;
///////////////////////////////////////////////////////////////////////////
#endif //__VXCOMMON_H__