Debian Codegraph @

thread_linux.cpp

raw symbols SCIP
1 /*
2 * Licensed under CLIFE license. See LICENCE.TXT
3 *
4 * Produced by: Jeff Lait
5 *
6 * CLIFE Development
7 *
8 * NAME: thread_linux.h ( CLIFE, C++ )
9 *
10 * COMMENTS:
11 * Attempt at a threading class.
12 */
13
14 #include "thread_linux.h"
15
16 THREAD_LINUX::THREAD_LINUX()
17 {
18 myExists = false;
19 myRunning = false;
20 }
21
22 THREAD_LINUX::~THREAD_LINUX()
23 {
24 if (isActive())
25 join();
26
27 #ifndef ANDROID
28 if (myExists)
29 pthread_cancel(mySelf);
30 #endif
31 }
32
33 void
34 THREAD_LINUX::start(THREADmainFunc func, void *data)
35 {
36 // If we are still running, block until the current task completes.
37 if (isActive())
38 join();
39
40 myCB = func;
41 myCBData = data;
42 if (!myExists)
43 {
44 pthread_attr_t attr;
45
46 pthread_cond_init(&myStateChangeEvent, 0);
47 pthread_mutex_init(&myLock, 0);
48
49 pthread_attr_init(&attr);
50
51 myExists = true;
52
53 pthread_create(&mySelf, &attr, THREAD::wrapper, this);
54 pthread_attr_destroy(&attr);
55 }
56
57 jobsready();
58 }
59
60 void
61 THREAD_LINUX::jobsready()
62 {
63 pthread_mutex_lock(&myLock);
64 myRunning = true;
65 pthread_cond_broadcast(&myStateChangeEvent);
66 pthread_mutex_unlock(&myLock);
67 }
68
69 void
70 THREAD_LINUX::iamdonenow()
71 {
72 pthread_mutex_lock(&myLock);
73 myRunning = false;
74 pthread_cond_broadcast(&myStateChangeEvent);
75 pthread_mutex_unlock(&myLock);
76 }
77
78 void
79 THREAD_LINUX::waittillimready()
80 {
81 pthread_mutex_lock(&myLock);
82 while (!myRunning)
83 pthread_cond_wait(&myStateChangeEvent, &myLock);
84 pthread_mutex_unlock(&myLock);
85 }
86
87 void
88 THREAD_LINUX::join()
89 {
90 pthread_mutex_lock(&myLock);
91 while (myRunning)
92 pthread_cond_wait(&myStateChangeEvent, &myLock);
93 pthread_mutex_unlock(&myLock);
94 }
95
96 void
97 THREAD_LINUX::kill()
98 {
99 if (myExists)
100 {
101 #ifndef ANDROID
102 pthread_cancel(mySelf);
103 #endif
104 myExists = false;
105 }
106 }