My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
#ifndef _DEBUG
inline void AssertST() {}
#endif

#ifdef _MULTITHREADED

#ifdef COMPILER_MSC
#ifdef flagDLL
#define thread__
#else
#define thread__ __declspec(thread)
#endif
#else
#define thread__ __thread
#endif

#ifdef flagPROFILEMT
class Mutex;
class StaticMutex;

struct MtInspector {
const char *name;
int number;
int locked;
int blocked;

static MtInspector *Dumi();

MtInspector(const char *s, int n = -1) { name = s; number = n; locked = blocked = 0; }
~MtInspector();
};

#define PROFILEMT(mutex) \
{ static MtInspector MK__s(__FILE__, __LINE__); mutex.Set(MK__s); }

#define PROFILEMT_(mutex, id) \
{ static MtInspector MK__s(id); mutex.Set(MK__s); }

#else

#define PROFILEMT(mutex)
#define PROFILEMT_(mutex, id)

#endif

class Callback;

class Thread : NoCopy {
#ifdef PLATFORM_WIN32
HANDLE handle;
DWORD thread_id;
#endif
#ifdef PLATFORM_POSIX
pthread_t handle;
#endif
public:
bool Run(Callback cb);

void Detach();
int Wait();

bool IsOpen() const { return handle; }

#ifdef PLATFORM_WIN32
typedef HANDLE Handle;
typedef DWORD Id;

Id GetId() const { return thread_id; }
#endif
#ifdef PLATFORM_POSIX
typedef pthread_t Handle;
typedef pthread_t Id;

Id GetId() const { return handle; }
#endif

Handle GetHandle() const { return handle; }

void Priority(int percent); // 0 = lowest, 100 = normal

static void Start(Callback cb);

static void Sleep(int ms);

static bool IsST();
static bool IsMain();
static int GetCount();
static void ShutdownThreads();
static bool IsShutdownThreads();
static void (*AtExit(void (*exitfn)()))();

#ifdef PLATFORM_WIN32
static Handle GetCurrentHandle() { return GetCurrentThread(); }
static inline Id GetCurrentId() { return ::GetCurrentThreadId(); };
#endif
#ifdef PLATFORM_POSIX
static Handle GetCurrentHandle() { return pthread_self(); }
static inline Id GetCurrentId() { return pthread_self(); };
#endif

Thread();
~Thread();

private:
void operator=(const Thread&);
Thread(const Thread&);
};

#ifdef _DEBUG
inline void AssertST() { ASSERT(Thread::IsST()); }
#endif

void ReadMemoryBarrier();
void WriteMemoryBarrier();

#ifdef CPU_SSE2
inline void ReadMemoryBarrier()
{
#ifdef CPU_AMD64
#ifdef COMPILER_MSC
_mm_lfence();
#else
__asm__("lfence");
#endif
#else
#ifdef COMPILER_MSC
__asm lfence;
#else
__asm__("lfence");
#endif
#endif
}

inline void WriteMemoryBarrier() {
#ifdef CPU_AMD64
#ifdef COMPILER_MSC
_mm_sfence();
#else
__asm__("sfence");
#endif
#else
#ifdef COMPILER_MSC
__asm sfence;
#else
__asm__("sfence");
#endif
#endif
}
#endif

#ifdef CPU_BLACKFIN
inline void ReadMemoryBarrier() {} //placing in Mt.cpp somehow yields 'undefined reference'
inline void WriteMemoryBarrier() {}
#endif

template <class U>
inline U ReadWithBarrier(const volatile U& b)
{
/*volatile*/ U tmp = b;
ReadMemoryBarrier();
return tmp;
}

template <class U, class V>
inline void BarrierWrite(volatile U& dest, V data)
{
WriteMemoryBarrier();
dest = data;
}

class Semaphore : NoCopy {
#ifdef PLATFORM_WIN32
HANDLE handle;
#else
sem_t sem;
#endif

public:
void Wait();
void Release();
#ifdef PLATFORM_WIN32
void Release(int n);
#endif

Semaphore();
~Semaphore();
};

class StaticSemaphore : NoCopy {
volatile Semaphore *semaphore;
byte buffer[sizeof(Semaphore)];

void Initialize();

public:
Semaphore& Get() { if(!ReadWithBarrier(semaphore)) Initialize(); return *const_cast<Semaphore *>(semaphore); }
operator Semaphore&() { return Get(); }
void Wait() { Get().Wait(); }
void Release() { Get().Release(); }
};

struct MtInspector;

#ifdef PLATFORM_WIN32

typedef LONG Atomic;

inline int AtomicInc(volatile Atomic& t) { return InterlockedIncrement((Atomic *)&t); }
inline int AtomicDec(volatile Atomic& t) { return InterlockedDecrement((Atomic *)&t); }
inline int AtomicXAdd(volatile Atomic& t, int incr) { return InterlockedExchangeAdd((Atomic *)&t, incr); }

class Mutex : NoCopy {
protected:
CRITICAL_SECTION section;
MtInspector *mti;

Mutex(int) {}

public:
bool TryEnter();
void Leave() { LeaveCriticalSection(&section); }

#ifdef flagPROFILEMT
void Enter() { if(!TryEnter()) { mti->blocked++; EnterCriticalSection(&section); }; mti->locked++; }
void Set(MtInspector& m) { mti = &m; }

Mutex() { mti = MtInspector::Dumi(); InitializeCriticalSection(&section); }
#else
void Enter() { EnterCriticalSection(&section); }

Mutex() { InitializeCriticalSection(&section); }
#endif

~Mutex() { DeleteCriticalSection(&section); }

class Lock;
};

/* Win32 RWMutex implementation by Chris Thomasson, cristom@comcast.net */

class RWMutex : NoCopy {
LONG m_count, m_rdwake;
HANDLE m_wrwset, m_rdwset;
CRITICAL_SECTION m_wrlock;

public:
void EnterWrite();
void LeaveWrite();

void EnterRead();
void LeaveRead();

RWMutex();
~RWMutex();

class ReadLock;
class WriteLock;
};

class ConditionVariable {
Mutex mutex;
friend struct sCVWaiter_;

struct sCVWaiter_ *head, *tail;

public:
void Wait(Mutex& m);
void Signal();
void Broadcast();

ConditionVariable();
~ConditionVariable();
};

#endif

#ifdef PLATFORM_POSIX

typedef _Atomic_word Atomic;

inline int AtomicXAdd(volatile Atomic& t, int incr) { using namespace __gnu_cxx; return __exchange_and_add(&t, incr); }

inline int AtomicInc(volatile Atomic& t) { return AtomicXAdd(t, +1) + 1; }
inline int AtomicDec(volatile Atomic& t) { return AtomicXAdd(t, -1) - 1; }

class Mutex : NoCopy {
pthread_mutex_t mutex[1];
#ifdef flagPROFILEMT
MtInspector *mti;
#endif
friend class ConditionVariable;

public:
#ifdef flagPROFILEMT
bool TryEnter() { bool b = pthread_mutex_trylock(mutex) == 0; mti->locked += b; return b; }
void Enter() { if(pthread_mutex_trylock(mutex) != 0) { mti->blocked++; pthread_mutex_lock(mutex); } mti->locked++; }
void Set(MtInspector& m) { mti = &m; }
#else
bool TryEnter() { return pthread_mutex_trylock(mutex) == 0; }
void Enter() { pthread_mutex_lock(mutex); }
#endif
void Leave() { pthread_mutex_unlock(mutex); }

class Lock;

Mutex();
~Mutex() { pthread_mutex_destroy(mutex); }
};

class RWMutex : NoCopy {
pthread_rwlock_t rwlock[1];

public:
void EnterWrite() { pthread_rwlock_wrlock(rwlock); }
void LeaveWrite() { pthread_rwlock_unlock(rwlock); }
void EnterRead() { pthread_rwlock_rdlock(rwlock); }
void LeaveRead() { pthread_rwlock_unlock(rwlock); }

RWMutex();
~RWMutex();

class ReadLock;
class WriteLock;
};

class ConditionVariable {
pthread_cond_t cv[1];

public:
void Wait(Mutex& m) { pthread_cond_wait(cv, m.mutex); }

void Signal() { pthread_cond_signal(cv); }
void Broadcast() { pthread_cond_broadcast(cv); }

ConditionVariable() { pthread_cond_init(cv, NULL); }
~ConditionVariable() { pthread_cond_destroy(cv); }
};

#endif

inline int AtomicRead(const volatile Atomic& t) { return ReadWithBarrier(t); }
inline void AtomicWrite(volatile Atomic& t, int val) { BarrierWrite(t, val); }

class Mutex::Lock : NoCopy {
Mutex& s;

public:
Lock(Mutex& s) : s(s) { s.Enter(); }
~Lock() { s.Leave(); }
};

class RWMutex::ReadLock : NoCopy {
RWMutex& s;

public:
ReadLock(RWMutex& s) : s(s) { s.EnterRead(); }
~ReadLock() { s.LeaveRead(); }
};

class RWMutex::WriteLock : NoCopy {
RWMutex& s;

public:
WriteLock(RWMutex& s) : s(s) { s.EnterWrite(); }
~WriteLock() { s.LeaveWrite(); }
};

class StaticMutex : NoCopy {
volatile Mutex *section;
byte buffer[sizeof(Mutex)];

void Initialize();

public:
Mutex& Get() { if(!ReadWithBarrier(section)) Initialize(); return *const_cast<Mutex *>(section); }
operator Mutex&() { return Get(); }
bool TryEnter() { return Get().TryEnter();}
void Enter() { Get().Enter();}
void Leave() { Get().Leave(); }
#ifdef flagPROFILEMT
void Set(MtInspector& mti) { Get().Set(mti); }
#endif
};

class StaticRWMutex : NoCopy {
volatile RWMutex *rw;
byte buffer[sizeof(RWMutex)];

void Initialize();

public:
RWMutex& Get() { if(!ReadWithBarrier(rw)) Initialize(); return *const_cast<RWMutex *>(rw); }
operator RWMutex&() { return Get(); }
void EnterRead() { Get().EnterRead();}
void LeaveRead() { Get().LeaveRead(); }
void EnterWrite() { Get().EnterWrite();}
void LeaveWrite() { Get().LeaveWrite(); }
};

class StaticConditionVariable : NoCopy {
volatile ConditionVariable *cv;
byte buffer[sizeof(ConditionVariable)];

void Initialize();

public:
ConditionVariable& Get() { if(!ReadWithBarrier(cv)) Initialize(); return *const_cast<ConditionVariable *>(cv); }
operator ConditionVariable&() { return Get(); }

void Wait(Mutex& m) { Get().Wait(m); }

void Signal() { Get().Signal(); }
void Broadcast() { Get().Broadcast(); }
};

#define INTERLOCKED \
for(bool i_b_ = true; i_b_;) \
for(static UPP::StaticMutex i_ss_; i_b_;) \
for(UPP::Mutex::Lock i_ss_lock__(i_ss_); i_b_; i_b_ = false)

struct H_l_ : Mutex::Lock {
bool b;
H_l_(Mutex& cs) : Mutex::Lock(cs) { b = true; }
};

#define INTERLOCKED_(cs) \
for(UPP::H_l_ i_ss_lock__(cs); i_ss_lock__.b; i_ss_lock__.b = false)

void Set__(volatile bool& b);

#define ONCELOCK \
for(static volatile bool o_b_; !ReadWithBarrier(o_b_);) \
for(static StaticMutex o_ss_; !o_b_;) \
for(Mutex::Lock o_ss_lock__(o_ss_); !o_b_; BarrierWrite(o_b_, true))

#define ONCELOCK_(o_b_) \
for(static StaticMutex o_ss_; !ReadWithBarrier(o_b_);) \
for(Mutex::Lock o_ss_lock__(o_ss_); !o_b_; BarrierWrite(o_b_, true))

#define ONCELOCK_PTR(ptr, init) \
if(!ReadWithBarrier(ptr)) { \
static StaticMutex cs; \
cs.Enter(); \
if(!ptr) \
BarrierWrite(ptr, init); \
cs.Leave(); \
}

class LazyUpdate {
mutable Mutex mutex;
mutable bool dirty;

public:
void Invalidate();
bool BeginUpdate() const;
void EndUpdate() const;

LazyUpdate();
};

inline bool IsMainThread() { return Thread::IsMain(); }

#else

inline bool IsMainThread() { return true; }

#define thread__

#define PROFILEMT(mutex)
#define PROFILEMT_(mutex, id)

typedef int Atomic;

inline int AtomicRead(const volatile Atomic& t) { return t; }
inline void AtomicWrite(volatile Atomic& t, int data) { t = data; }

inline int AtomicInc(volatile Atomic& t) { ++t; return t; }
inline int AtomicDec(volatile Atomic& t) { --t; return t; }
inline int AtomicXAdd(volatile Atomic& t, int incr) { Atomic x = t; t += incr; return x; }

class Mutex : NoCopy {
public:
bool TryEnter() { return true; }
void Enter() {}
void Leave() {}

class Lock;
};

typedef Mutex StaticMutex;

class Mutex::Lock : NoCopy {
public:
Lock(Mutex&) {}
~Lock() {}
};

class RWMutex : NoCopy {
public:
void EnterWrite() {}
void LeaveWrite() {}

void EnterRead() {}
void LeaveRead() {}

class ReadLock;
class WriteLock;
};

class RWMutex::ReadLock : NoCopy {
public:
ReadLock(RWMutex&) {}
~ReadLock() {}
};

class RWMutex::WriteLock : NoCopy {
public:
WriteLock(RWMutex&) {}
~WriteLock() {}
};

typedef RWMutex StaticRWMutex;

class LazyUpdate {
mutable bool dirty;

public:
void Invalidate() { dirty = true; }
bool BeginUpdate() const { return dirty; }
void EndUpdate() const { dirty = false; }

LazyUpdate() { dirty = true; }
};

#define INTERLOCKED
#define INTERLOCKED_(x) { x.Enter(); }

#define ONCELOCK \
for(static bool o_b_; !o_b_; o_b_ = true)

#define ONCELOCK_(o_b_) \
for(; !o_b_; o_b_ = true) \

#define ONCELOCK_PTR(ptr, init) \
if(!ptr) ptr = init;

inline void ReadMemoryBarrier() {}
inline void WriteMemoryBarrier() {}

#ifdef _DEBUG
inline void AssertST() {}
#endif

#endif

typedef Mutex CriticalSection; // deprecated
typedef StaticMutex StaticCriticalSection; // deprecated

// Auxiliary multithreading intended for use even in single-threaded applications
// to resolve some host platform issues. Raw threads cannot use U++ heap (nor indirectly)

#ifdef PLATFORM_WIN32
#define auxthread_t DWORD
#define auxthread__ WINAPI
#else
#define auxthread_t void *
#define auxthread__
#endif

#ifdef PLATFORM_WIN32
struct AuxMutex {
CRITICAL_SECTION cs;

void Enter() { EnterCriticalSection(&cs); }
void Leave() { LeaveCriticalSection(&cs); }

AuxMutex() { InitializeCriticalSection(&cs); }
~AuxMutex() { DeleteCriticalSection(&cs); }
};
#endif

#ifdef PLATFORM_POSIX
struct AuxMutex {
pthread_mutex_t mutex[1];

void Enter() { pthread_mutex_lock(mutex); }
void Leave() { pthread_mutex_unlock(mutex); }

AuxMutex() {
pthread_mutexattr_t mutex_attr[1];
pthread_mutexattr_init(mutex_attr);
pthread_mutexattr_settype(mutex_attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(mutex, mutex_attr);
}
~AuxMutex() { pthread_mutex_destroy(mutex); }
};
#endif

bool StartAuxThread(auxthread_t (auxthread__ *fn)(void *ptr), void *ptr);

Change log

r4791 by cxl on Apr 16, 2012   Diff
Core: inet docs
Go to: 
Project members, sign in to write a code review

Older revisions

r4781 by cxl on Apr 13, 2012   Diff
Core: Raw thread in win32 now using
CreateThread to avoid linking problems
r4761 by cxl on Apr 9, 2012   Diff
Core: TcpSocket, Thread::AtExit
r4759 by cxl on Apr 8, 2012   Diff
Core: Refactored Stream interface
(dword->int), fixed LauchWebBrowser
for certaion situations (thanks Koldo)
All revisions of this file

File info

Size: 13393 bytes, 599 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting