blob: 7cead891dd890e80feca76f05870240a08bf214c (
plain)
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
|
/*
* MonoScopedThreadAttach.cpp
* AllTargets.workspace
*
* Created by Søren Christiansen on 8/23/11.
* Copyright 2011 Unity Technologies. All rights reserved.
*
*/
#include "UnityPrefix.h"
#include "MonoScopedThreadAttach.h"
#include "MonoIncludes.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/Threads/Mutex.h"
#if SUPPORT_MONO_THREADS
static attached_thread m_AttachedThreads[MAX_ATTACHED_THREADS] = {{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};
static Mutex mutex;
// call this from the thread you want to attach!
Thread::ThreadID AttachMonoThread(MonoDomain* domain)
{
Assert(!Thread::CurrentThreadIsMainThread());
Mutex::AutoLock lock( mutex );
for (int i=0; i < MAX_ATTACHED_THREADS; ++i)
{
if (m_AttachedThreads[i].threadID == Thread::GetCurrentThreadID())
{
m_AttachedThreads[i].refCount++;
return m_AttachedThreads[i].threadID;
}
else
if (m_AttachedThreads[i].threadID == 0)
{
m_AttachedThreads[i].threadID = Thread::GetCurrentThreadID();
m_AttachedThreads[i].thread = mono_thread_attach(domain);
m_AttachedThreads[i].refCount = 1;
return m_AttachedThreads[i].threadID;
}
}
return 0;
}
// call this from the thread you want to detach!
bool DetachMonoThread(Thread::ThreadID threadID)
{
Assert(!Thread::CurrentThreadIsMainThread());
Mutex::AutoLock lock( mutex );
for (int i=0; i < MAX_ATTACHED_THREADS; ++i)
{
if (m_AttachedThreads[i].threadID == Thread::GetCurrentThreadID())
{
m_AttachedThreads[i].refCount--;
if (m_AttachedThreads[i].refCount == 0)
{
mono_thread_detach(m_AttachedThreads[i].thread);
m_AttachedThreads[i].threadID = 0;
m_AttachedThreads[i].thread = 0;
m_AttachedThreads[i].refCount = 0;
}
return true;
}
}
return false;
}
#endif // SUPPORT_MONO_THREADS
|