forked from birdwyx/phpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_mutex.h
49 lines (41 loc) · 1.08 KB
/
go_mutex.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
#pragma once
#include "co_recursive_mutex.h"
/*
* mutex
*/
class GoMutex{
public:
static void* Create(bool signaled){
return new ::co::CoRecursiveMutex(signaled);
}
static void Lock(void* mutex){
auto mutex_obj = (::co::CoRecursiveMutex*)mutex;
// if it's not in any go routine,
// run the scheduler (so that the go routines will run and may
// release lock) until the lock is obtained
if( !co_sched.IsCoroutine() ){
while( !mutex_obj->try_lock() ) {
co_sched.Run();
}
return;
}
// in a go routine, it's safe to just lock
mutex_obj->lock();
}
static void Unlock(void* mutex){
auto mutex_obj = (::co::CoRecursiveMutex*)mutex;
mutex_obj->unlock();
}
static bool TryLock(void* mutex){
auto mutex_obj = (::co::CoRecursiveMutex*)mutex;
return mutex_obj->try_lock();
}
static bool IsLock(void* mutex){
auto mutex_obj = (::co::CoRecursiveMutex*)mutex;
return mutex_obj->is_lock();
}
static void Destroy(void* mutex){
auto mutex_obj = (::co::CoRecursiveMutex*)mutex;
delete mutex_obj;
}
};