summaryrefslogtreecommitdiff
path: root/pjlib/src/pj++/os.hpp
blob: c38275285370688d310aa4ee5916b0ecfc5f6601 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* $Header: /pjproject/pjlib/src/pj++/os.hpp 2     2/24/05 11:23a Bennylp $ */
#ifndef __PJPP_OS_H__
#define __PJPP_OS_H__

#include <pj/os.h>
#include <pj++/types.hpp>
#include <pj++/pool.hpp>

class PJ_Thread
{
public:
    enum Flags
    {
	FLAG_SUSPENDED = PJ_THREAD_SUSPENDED
    };

    static PJ_Thread *create( PJ_Pool *pool, const char *thread_name,
			      pj_thread_proc *proc, void *arg,
			      pj_size_t stack_size, void *stack_ptr, 
			      unsigned flags)
    {
	return (PJ_Thread*) pj_thread_create( pool->pool_(), thread_name, proc, arg, stack_size, stack_ptr, flags);
    }

    static PJ_Thread *register_current_thread(const char *name, pj_thread_desc desc)
    {
	return (PJ_Thread*) pj_thread_register(name, desc);
    }

    static PJ_Thread *get_current_thread()
    {
	return (PJ_Thread*) pj_thread_this();
    }

    static pj_status_t sleep(unsigned msec)
    {
	return pj_thread_sleep(msec);
    }

    static pj_status_t usleep(unsigned usec)
    {
	return pj_thread_usleep(usec);
    }

    pj_thread_t *pj_thread_t_()
    {
	return (pj_thread_t*)this;
    }

    const char *get_name()
    {
	return pj_thread_get_name( this->pj_thread_t_() );
    }

    pj_status_t resume()
    {
	return pj_thread_resume( this->pj_thread_t_() );
    }

    pj_status_t join()
    {
	return pj_thread_join( this->pj_thread_t_() );
    }

    pj_status_t destroy()
    {
	return pj_thread_destroy( this->pj_thread_t_() );
    }
};


class PJ_Thread_Local
{
public:
    static PJ_Thread_Local *alloc()
    {
	long index = pj_thread_local_alloc();
	return index < 0 ? NULL : (PJ_Thread_Local*)index;
    }
    void free()
    {
	pj_thread_local_free( this->tls_() );
    }

    long tls_() const
    {
	return (long)this;
    }

    void set(void *value)
    {
	pj_thread_local_set( this->tls_(), value );
    }

    void *get()
    {
	return pj_thread_local_get( this->tls_() );
    }
};


class PJ_Atomic
{
public:
    static PJ_Atomic *create(PJ_Pool *pool, long initial)
    {
	return (PJ_Atomic*) pj_atomic_create(pool->pool_(), initial);
    }

    pj_atomic_t *pj_atomic_t_()
    {
	return (pj_atomic_t*)this;
    }

    pj_status_t destroy()
    {
	return pj_atomic_destroy( this->pj_atomic_t_() );
    }

    long set(long val)
    {
	return pj_atomic_set( this->pj_atomic_t_(), val);
    }

    long get()
    {
	return pj_atomic_get( this->pj_atomic_t_() );
    }

    long inc()
    {
	return pj_atomic_inc( this->pj_atomic_t_() );
    }

    long dec()
    {
	return pj_atomic_dec( this->pj_atomic_t_() );
    }
};


class PJ_Mutex
{
public:
    enum Type
    {
	DEFAULT = PJ_MUTEX_DEFAULT,
	SIMPLE = PJ_MUTEX_SIMPLE,
	RECURSE = PJ_MUTEX_RECURSE,
    };

    static PJ_Mutex *create( PJ_Pool *pool, const char *name, Type type)
    {
	return (PJ_Mutex*) pj_mutex_create( pool->pool_(), name, type);
    }

    pj_mutex_t *pj_mutex_()
    {
	return (pj_mutex_t*)this;
    }

    pj_status_t destroy()
    {
	return pj_mutex_destroy( this->pj_mutex_() );
    }

    pj_status_t lock()
    {
	return pj_mutex_lock( this->pj_mutex_() );
    }

    pj_status_t unlock()
    {
	return pj_mutex_unlock( this->pj_mutex_() );
    }

    pj_status_t trylock()
    {
	return pj_mutex_trylock( this->pj_mutex_() );
    }

#if PJ_DEBUG
    pj_status_t is_locked()
    {
	return pj_mutex_is_locked( this->pj_mutex_() );
    }
#endif
};


class PJ_Semaphore
{
public:
    static PJ_Semaphore *create( PJ_Pool *pool, const char *name, unsigned initial, unsigned max)
    {
	return (PJ_Semaphore*) pj_sem_create( pool->pool_(), name, initial, max);
    }

    pj_sem_t *pj_sem_t_()
    {
	return (pj_sem_t*)this;
    }

    pj_status_t destroy()
    {
	return pj_sem_destroy(this->pj_sem_t_());
    }

    pj_status_t wait()
    {
	return pj_sem_wait(this->pj_sem_t_());
    }

    pj_status_t lock()
    {
	return wait();
    }

    pj_status_t trywait()
    {
	return pj_sem_trywait(this->pj_sem_t_());
    }

    pj_status_t trylock()
    {
	return trywait();
    }

    pj_status_t post()
    {
	return pj_sem_post(this->pj_sem_t_());
    }

    pj_status_t unlock()
    {
	return post();
    }
};


class PJ_Event
{
public:
    static PJ_Event *create( PJ_Pool *pool, const char *name, bool manual_reset, bool initial)
    {
	return (PJ_Event*) pj_event_create(pool->pool_(), name, manual_reset, initial);
    }

    pj_event_t *pj_event_t_()
    {
	return (pj_event_t*)this;
    }

    pj_status_t destroy()
    {
	return pj_event_destroy(this->pj_event_t_());
    }

    pj_status_t wait()
    {
	return pj_event_wait(this->pj_event_t_());
    }

    pj_status_t trywait()
    {
	return pj_event_trywait(this->pj_event_t_());
    }

    pj_status_t set()
    {
	return pj_event_set(this->pj_event_t_());
    }

    pj_status_t pulse()
    {
	return pj_event_pulse(this->pj_event_t_());
    }

    pj_status_t reset()
    {
	return pj_event_reset(this->pj_event_t_());
    }
};

class PJ_OS
{
public:
    static pj_status_t gettimeofday( PJ_Time_Val *tv )
    {
	return pj_gettimeofday(tv);
    }

    static pj_status_t time_decode( const PJ_Time_Val *tv, pj_parsed_time *pt )
    {
	return pj_time_decode(tv, pt);
    }

    static pj_status_t time_encode(const pj_parsed_time *pt, PJ_Time_Val *tv)
    {
	return pj_time_encode(pt, tv);
    }

    static pj_status_t time_local_to_gmt( PJ_Time_Val *tv )
    {
	return pj_time_local_to_gmt( tv );
    }

    static pj_status_t time_gmt_to_local( PJ_Time_Val *tv) 
    {
	return pj_time_gmt_to_local( tv );
    }
};


inline pj_status_t PJ_Time_Val::gettimeofday()
{
    return PJ_OS::gettimeofday(this);
}

inline pj_parsed_time PJ_Time_Val::decode()
{
    pj_parsed_time pt;
    PJ_OS::time_decode(this, &pt);
    return pt;
}

inline pj_status_t PJ_Time_Val::encode(const pj_parsed_time *pt)
{
    return PJ_OS::time_encode(pt, this);
}

inline pj_status_t PJ_Time_Val::to_gmt()
{
    return PJ_OS::time_local_to_gmt(this);
}

inline pj_status_t PJ_Time_Val::to_local()
{
    return PJ_OS::time_gmt_to_local(this);
}

#endif	/* __PJPP_OS_H__ */