Debian Codegraph @

protolib/src/common/protoThread.cpp

raw
1 #include "protoThread.h"
2 #include "protoDebug.h"
3
4 ProtoMutex::ProtoMutex(bool init)
5 {
6 if (init) Init();
7 }
8
9 ProtoMutex::~ProtoMutex()
10 {
11 Destroy();
12 }
13
14 void ProtoMutex::Init()
15 {
16 #ifdef WIN32
17 InitializeCriticalSection(&mutex);
18 #else
19 pthread_mutex_init(&mutex, NULL);
20 #endif
21 } // end ProtoMutex::Init()
22
23 void ProtoMutex::Destroy()
24 {
25 #ifdef WIN32
26 DeleteCriticalSection(&mutex);
27 #else
28 pthread_mutex_destroy(&mutex);
29 #endif
30 } // end ProtoMutex::Destroy()
31
32 void ProtoMutex::Lock()
33 {
34 #ifdef WIN32
35 EnterCriticalSection(&mutex);
36 #else
37 pthread_mutex_lock(&mutex);
38 #endif
39 } // end ProtoMutex::Lock()
40
41 void ProtoMutex::Unlock()
42 {
43 #ifdef WIN32
44 LeaveCriticalSection(&mutex);
45 #else
46 pthread_mutex_unlock(&mutex);
47 #endif
48 } // end ProtoMutex::Unlock()
49
50 ProtoThread::ProtoThread()
51 : thread_id((ThreadId)NULL), external_thread(false),
52 thread_running(false), exit_code(0)
53 {
54 }
55
56 ProtoThread::~ProtoThread()
57 {
58
59 }
60
61 #ifdef WIN32
62 unsigned int __stdcall ProtoThread::DoThreadStart(void* param)
63 #else
64 void* ProtoThread::DoThreadStart(void* param)
65 #endif // if/else WIN32/UNIX
66 {
67 ProtoThread* protoThread = reinterpret_cast<ProtoThread*>(param);
68 ASSERT(NULL != protoThread);
69 protoThread->thread_running = true;
70 protoThread->exit_status = protoThread->RunThread(); // TBD - should have Run() set exit_status internally instead???
71 DoThreadExit(protoThread->GetExitStatus());
72 return (protoThread->GetExitStatus());
73 } // end ProtoThread::DoThreadStart()
74
75 bool ProtoThread::StartThread(ThreadId threadId)
76 {
77 if ((ThreadId)NULL == threadId)
78 {
79 external_thread = false;
80 #ifdef WIN32
81 if (!(actual_thread_handle = (HANDLE)_beginthreadex(NULL, 0, DoThreadStart, this, 0, (unsigned*)&thread_id)))
82 #else
83 if (0 != pthread_create(&thread_id, NULL, DoThreadStart, this))
84 #endif // if/else WIN32/UNIX
85 {
86 PLOG(PL_ERROR, "ProtoThread::StartThread() create thread error: %s\n", GetErrorString());
87 thread_id = (ThreadId)NULL;
88 return false;
89 }
90 }
91 else
92 {
93 // Starting within externally created thread
94 ASSERT(threadId == GetCurrentThread());
95 thread_id = threadId; // externally created thread
96 external_thread = true;
97 // So must exec DoThreadStart() steps here
98 thread_running = true;
99 exit_status = RunThread();
100 }
101 return true;
102 } // end ProtoThread::StartThread()
103
104 void ProtoThread::CloseThread(int exitCode)
105 {
106 exit_code = exitCode;
107 if ((ThreadId)NULL != thread_id)
108 {
109 if (!external_thread)
110 {
111 #ifdef WIN32
112 if (!IsMyself()) WaitForSingleObject(actual_thread_handle, INFINITE);
113 #else
114 if (!IsMyself()) pthread_join(thread_id, NULL);
115 #endif // if/else WIN32/UNIX
116 }
117 thread_running = false;
118 thread_id = (ThreadId)NULL;
119 external_thread = false;
120 }
121 } // end ProtoThread::CloseThread()