source: libcf++/trunk/include/cf/task.h@ 4

Last change on this file since 4 was 4, checked in by cheese, 9 years ago

#1 commit prototype

File size: 3.1 KB
Line 
1/**
2 * @file task.h
3 * @author myusgun@gmail.com
4 * @brief task
5 */
6#ifndef __task_h__
7#define __task_h__
8
9#include "cf/exception.h"
10
11#define SYNCHRONIZED(_s) for (cf::task::scopedLock _sl(&_s) ; _sl.synchronizer()->locked() ; _sl.synchronizer()->unlock())
12
13namespace cf
14{
15 /**
16 * task
17 */
18 namespace task
19 {
20 /**
21 * interface of synchronizer, like a mutex
22 */
23 class ISynchronizer
24 {
25 public:
26 /**
27 * lock
28 * @throw cf::exception
29 */
30 virtual cf::void_t lock() throw (cf::exception) = 0;
31
32 /**
33 * unlock
34 * @throw cf::exception
35 */
36 virtual cf::void_t unlock() throw (cf::exception) = 0;
37
38 /**
39 * is locked ?
40 * @return true; false
41 */
42 virtual cf::bool_t locked() const = 0;
43 };
44
45 /**
46 * mutex
47 */
48 class mutex : public ISynchronizer
49 {
50 private:
51 cf::bool_t mIsLocked;
52 cf::void_t * mMutex;
53
54 public:
55 /**
56 * constructor
57 * @throw cf::exception
58 */
59 mutex()
60 throw (cf::exception);
61
62 /**
63 * destructor
64 */
65 ~mutex();
66
67 /**
68 * lock
69 * @throw cf::exception
70 */
71 cf::void_t lock()
72 throw (cf::exception);
73
74 /**
75 * unlock
76 * @throw cf::exception
77 */
78 cf::void_t unlock()
79 throw (cf::exception);
80
81 /**
82 * trylock
83 * @return true; false
84 */
85 cf::bool_t trylock()
86 throw (cf::exception);
87
88 /**
89 * is locked ?
90 * @return true; false
91 */
92 cf::bool_t locked() const;
93 };
94
95 /**
96 * scoped lock
97 * @see SYNCHRONIZE
98 */
99 class scopedLock
100 {
101 private:
102 ISynchronizer * mSynchronizer;
103
104 public:
105 /**
106 * constructor for locking
107 * @throw cf::exception
108 */
109 scopedLock(ISynchronizer * inst)
110 throw (cf::exception);
111
112 /**
113 * destructor for unlocking
114 */
115 ~scopedLock();
116
117 /**
118 * get synchronizer
119 * @return an instance of synchronizer
120 */
121 ISynchronizer * synchronizer();
122 };
123
124 /**
125 * thread
126 */
127 class thread
128 {
129 public:
130 /** a prototype of worker function */
131 typedef cf::int32_t(*ThreadFunction)(cf::void_t *);
132
133 typedef struct _THREAD_CTX
134 {
135 cf::bool_t mIsRunning;
136 ThreadFunction mCallback;
137 cf::void_t * mArgument;
138 cf::int32_t mReturned;
139 } THREAD_CTX;
140
141 private:
142 THREAD_CTX mCtx;
143 cf::void_t * mThreadID;
144 mutex mMutex;
145
146 public:
147 /**
148 * constructor
149 * @param callback thread worker function
150 */
151 thread(const ThreadFunction callback)
152 throw (cf::exception);
153
154 /**
155 * destructor
156 */
157 ~thread();
158
159 /**
160 * start thread
161 * @param arg thread argument
162 * @throw cf::exception
163 */
164 cf::void_t start(cf::void_t * arg)
165 throw (cf::exception);
166
167 /**
168 * join thread
169 */
170 cf::void_t join();
171
172 /**
173 * is running ?
174 * @return true; false
175 */
176 cf::bool_t isRunning() const;
177
178 /**
179 * get thread id
180 * @return thread id
181 */
182 static cf::uint32_t id();
183
184 /**
185 * sleep
186 * @param milliseconds milliseconds
187 */
188 static cf::void_t sleep(const cf::int32_t milliseconds);
189 };
190
191 /**
192 * process
193 */
194 class process
195 {
196 public:
197 /**
198 * get process id
199 * @return process id
200 */
201 static cf::uint32_t id();
202 };
203 }
204}
205
206#endif // #ifndef __task_h__
Note: See TracBrowser for help on using the repository browser.