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

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

#1 add new interfaces for bin and task

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.locked() ; _sl.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 virtual ~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 virtual ~scopedLock();
116
117 /**
118 * is locked ?
119 * @return true; false
120 */
121 cf::bool_t locked() const;
122
123 /**
124 * unlock
125 */
126 cf::void_t unlock()
127 throw (cf::exception);
128 };
129
130 /**
131 * thread
132 */
133 class thread
134 {
135 public:
136 /** a prototype of worker function */
137 typedef cf::int32_t(*ThreadFunction)(cf::void_t *);
138
139 typedef struct _THREAD_CTX
140 {
141 cf::bool_t mIsRunning;
142 ThreadFunction mCallback;
143 cf::void_t * mArgument;
144 cf::int32_t mReturned;
145 } THREAD_CTX;
146
147 private:
148 THREAD_CTX mCtx;
149 cf::void_t * mThreadID;
150 mutex mMutex;
151
152 public:
153 /**
154 * constructor
155 * @param callback thread worker function
156 */
157 thread(const ThreadFunction callback)
158 throw (cf::exception);
159
160 /**
161 * destructor
162 */
163 virtual ~thread();
164
165 /**
166 * start thread
167 * @param arg thread argument
168 * @throw cf::exception
169 */
170 cf::void_t start(cf::void_t * arg)
171 throw (cf::exception);
172
173 /**
174 * join thread
175 */
176 cf::void_t join();
177
178 /**
179 * is running ?
180 * @return true; false
181 */
182 cf::bool_t isRunning() const;
183
184 /**
185 * get thread id
186 * @return thread id
187 */
188 static cf::uint32_t id();
189
190 /**
191 * sleep
192 * @param milliseconds milliseconds
193 */
194 static cf::void_t sleep(const cf::int32_t milliseconds);
195 };
196
197 /**
198 * process
199 */
200 class process
201 {
202 public:
203 /**
204 * get process id
205 * @return process id
206 */
207 static cf::uint32_t id();
208 };
209 }
210}
211
212#endif // #ifndef __task_h__
Note: See TracBrowser for help on using the repository browser.