/usr/include/pj++/os.hpp is in libpjproject-dev 2.1.0.0.ast20130823-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | /* $Id: os.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
/*
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __PJPP_OS_HPP__
#define __PJPP_OS_HPP__
#include <pj/os.h>
#include <pj/string.h>
#include <pj++/types.hpp>
#include <pj++/pool.hpp>
class Pj_Thread;
//
// Thread API.
//
class Pj_Thread_API
{
public:
//
// Create a thread.
//
static pj_status_t create( Pj_Pool *pool, pj_thread_t **thread,
pj_thread_proc *proc, void *arg,
unsigned flags = 0,
const char *name = NULL,
pj_size_t stack_size = 0 )
{
return pj_thread_create(pool->pool_(), name, proc, arg, stack_size,
flags, thread);
}
//
// Register a thread.
//
static pj_status_t register_this_thread( pj_thread_desc desc,
pj_thread_t **thread,
const char *name = NULL )
{
return pj_thread_register( name, desc, thread );
}
//
// Get current thread.
// Will return pj_thread_t (sorry folks, not Pj_Thread).
//
static pj_thread_t *this_thread()
{
return pj_thread_this();
}
//
// Get thread name.
//
static const char *get_name(pj_thread_t *thread)
{
return pj_thread_get_name(thread);
}
//
// Resume thread.
//
static pj_status_t resume(pj_thread_t *thread)
{
return pj_thread_resume(thread);
}
//
// Sleep.
//
static pj_status_t sleep(unsigned msec)
{
return pj_thread_sleep(msec);
}
//
// Join the specified thread.
//
static pj_status_t join(pj_thread_t *thread)
{
return pj_thread_join(thread);
}
//
// Destroy thread
//
static pj_status_t destroy(pj_thread_t *thread)
{
return pj_thread_destroy(thread);
}
};
//
// Thread object.
//
// How to use:
// Derive a class from this class, then override main().
//
class Pj_Thread : public Pj_Object
{
public:
enum Flags
{
FLAG_SUSPENDED = PJ_THREAD_SUSPENDED
};
//
// Default constructor.
//
Pj_Thread()
: thread_(NULL)
{
}
//
// Destroy thread.
//
~Pj_Thread()
{
destroy();
}
//
// This is the main thread function.
//
virtual int main() = 0;
//
// Start a thread.
//
pj_status_t create( Pj_Pool *pool,
unsigned flags = 0,
const char *thread_name = NULL,
pj_size_t stack_size = PJ_THREAD_DEFAULT_STACK_SIZE)
{
destroy();
return Pj_Thread_API::create( pool, &thread_, &thread_proc, this,
flags, thread_name, stack_size);
}
//
// Get pjlib compatible thread object.
//
pj_thread_t *pj_thread_t_()
{
return thread_;
}
//
// Get thread name.
//
const char *get_name()
{
return Pj_Thread_API::get_name(thread_);
}
//
// Resume a suspended thread.
//
pj_status_t resume()
{
return Pj_Thread_API::resume(thread_);
}
//
// Join this thread.
//
pj_status_t join()
{
return Pj_Thread_API::join(thread_);
}
//
// Destroy thread.
//
pj_status_t destroy()
{
if (thread_) {
Pj_Thread_API::destroy(thread_);
thread_ = NULL;
}
}
protected:
pj_thread_t *thread_;
static int PJ_THREAD_FUNC thread_proc(void *obj)
{
Pj_Thread *thread_class = (Pj_Thread*)obj;
return thread_class->main();
}
};
//
// External Thread
// (threads that were started by external means, i.e. not
// with Pj_Thread::create).
//
// This class will normally be defined as local variable in
// external thread's stack, normally inside thread's main proc.
// But be aware that the handle will be destroyed on destructor!
//
class Pj_External_Thread : public Pj_Thread
{
public:
Pj_External_Thread()
{
}
//
// Register external thread so that pjlib functions can work
// in that thread.
//
pj_status_t register_this_thread( const char *name=NULL )
{
return Pj_Thread_API::register_this_thread(desc_, &thread_,name);
}
private:
pj_thread_desc desc_;
};
//
// Thread specific data/thread local storage/TLS.
//
class Pj_Thread_Local_API
{
public:
//
// Allocate thread local storage (TLS) index.
//
static pj_status_t alloc(long *index)
{
return pj_thread_local_alloc(index);
}
//
// Free TLS index.
//
static void free(long index)
{
pj_thread_local_free(index);
}
//
// Set thread specific data.
//
static pj_status_t set(long index, void *value)
{
return pj_thread_local_set(index, value);
}
//
// Get thread specific data.
//
static void *get(long index)
{
return pj_thread_local_get(index);
}
};
//
// Atomic variable
//
// How to use:
// Pj_Atomic_Var var(pool, 0);
// var.set(..);
//
class Pj_Atomic_Var : public Pj_Object
{
public:
//
// Default constructor, initialize variable with NULL.
//
Pj_Atomic_Var()
: var_(NULL)
{
}
//
// Construct atomic variable.
//
Pj_Atomic_Var(Pj_Pool *pool, pj_atomic_value_t value)
: var_(NULL)
{
create(pool, value);
}
//
// Destructor.
//
~Pj_Atomic_Var()
{
destroy();
}
//
// Create atomic variable.
//
pj_status_t create( Pj_Pool *pool, pj_atomic_value_t value)
{
destroy();
return pj_atomic_create(pool->pool_(), value, &var_);
}
//
// Destroy.
//
void destroy()
{
if (var_) {
pj_atomic_destroy(var_);
var_ = NULL;
}
}
//
// Get pjlib compatible atomic variable.
//
pj_atomic_t *pj_atomic_t_()
{
return var_;
}
//
// Set the value.
//
void set(pj_atomic_value_t val)
{
pj_atomic_set(var_, val);
}
//
// Get the value.
//
pj_atomic_value_t get()
{
return pj_atomic_get(var_);
}
//
// Increment.
//
void inc()
{
pj_atomic_inc(var_);
}
//
// Increment and get the result.
//
pj_atomic_value_t inc_and_get()
{
return pj_atomic_inc_and_get(var_);
}
//
// Decrement.
//
void dec()
{
pj_atomic_dec(var_);
}
//
// Decrement and get the result.
//
pj_atomic_value_t dec_and_get()
{
return pj_atomic_dec_and_get(var_);
}
//
// Add the variable.
//
void add(pj_atomic_value_t value)
{
pj_atomic_add(var_, value);
}
//
// Add the variable and get the value.
//
pj_atomic_value_t add_and_get(pj_atomic_value_t value)
{
return pj_atomic_add_and_get(var_, value );
}
private:
pj_atomic_t *var_;
};
//
// Mutex
//
class Pj_Mutex : public Pj_Object
{
public:
//
// Mutex type.
//
enum Type
{
DEFAULT = PJ_MUTEX_DEFAULT,
SIMPLE = PJ_MUTEX_SIMPLE,
RECURSE = PJ_MUTEX_RECURSE,
};
//
// Default constructor will create default mutex.
//
explicit Pj_Mutex(Pj_Pool *pool, Type type = DEFAULT,
const char *name = NULL)
: mutex_(NULL)
{
create(pool, type, name);
}
//
// Destructor.
//
~Pj_Mutex()
{
destroy();
}
//
// Create mutex.
//
pj_status_t create( Pj_Pool *pool, Type type, const char *name = NULL)
{
destroy();
return pj_mutex_create( pool->pool_(), name, type,
&mutex_ );
}
//
// Create simple mutex.
//
pj_status_t create_simple( Pj_Pool *pool,const char *name = NULL)
{
return create(pool, SIMPLE, name);
}
//
// Create recursive mutex.
//
pj_status_t create_recursive( Pj_Pool *pool, const char *name = NULL )
{
return create(pool, RECURSE, name);
}
//
// Get pjlib compatible mutex object.
//
pj_mutex_t *pj_mutex_t_()
{
return mutex_;
}
//
// Destroy mutex.
//
void destroy()
{
if (mutex_) {
pj_mutex_destroy(mutex_);
mutex_ = NULL;
}
}
//
// Lock mutex.
//
pj_status_t acquire()
{
return pj_mutex_lock(mutex_);
}
//
// Unlock mutex.
//
pj_status_t release()
{
return pj_mutex_unlock(mutex_);
}
//
// Try locking the mutex.
//
pj_status_t tryacquire()
{
return pj_mutex_trylock(mutex_);
}
private:
pj_mutex_t *mutex_;
};
//
// Semaphore
//
class Pj_Semaphore : public Pj_Object
{
public:
//
// Construct semaphore
//
Pj_Semaphore(Pj_Pool *pool, unsigned max,
unsigned initial = 0, const char *name = NULL)
: sem_(NULL)
{
create(pool, max, initial, name);
}
//
// Destructor.
//
~Pj_Semaphore()
{
destroy();
}
//
// Create semaphore
//
pj_status_t create( Pj_Pool *pool, unsigned max,
unsigned initial = 0, const char *name = NULL )
{
destroy();
return pj_sem_create( pool->pool_(), name, initial, max, &sem_);
}
//
// Destroy semaphore.
//
void destroy()
{
if (sem_) {
pj_sem_destroy(sem_);
sem_ = NULL;
}
}
//
// Get pjlib compatible semaphore object.
//
pj_sem_t *pj_sem_t_()
{
return (pj_sem_t*)this;
}
//
// Wait semaphore.
//
pj_status_t wait()
{
return pj_sem_wait(this->pj_sem_t_());
}
//
// Wait semaphore.
//
pj_status_t acquire()
{
return wait();
}
//
// Try wait semaphore.
//
pj_status_t trywait()
{
return pj_sem_trywait(this->pj_sem_t_());
}
//
// Try wait semaphore.
//
pj_status_t tryacquire()
{
return trywait();
}
//
// Post semaphore.
//
pj_status_t post()
{
return pj_sem_post(this->pj_sem_t_());
}
//
// Post semaphore.
//
pj_status_t release()
{
return post();
}
private:
pj_sem_t *sem_;
};
//
// Event object.
//
class Pj_Event
{
public:
//
// Construct event object.
//
Pj_Event( Pj_Pool *pool, bool manual_reset = false,
bool initial = false, const char *name = NULL )
: event_(NULL)
{
create(pool, manual_reset, initial, name);
}
//
// Destructor.
//
~Pj_Event()
{
destroy();
}
//
// Create event object.
//
pj_status_t create( Pj_Pool *pool, bool manual_reset = false,
bool initial = false, const char *name = NULL)
{
destroy();
return pj_event_create(pool->pool_(), name, manual_reset, initial,
&event_);
}
//
// Get pjlib compatible event object.
//
pj_event_t *pj_event_t_()
{
return event_;
}
//
// Destroy event object.
//
void destroy()
{
if (event_) {
pj_event_destroy(event_);
event_ = NULL;
}
}
//
// Wait.
//
pj_status_t wait()
{
return pj_event_wait(event_);
}
//
// Try wait.
//
pj_status_t trywait()
{
return pj_event_trywait(event_);
}
//
// Set event state to signalled.
//
pj_status_t set()
{
return pj_event_set(this->pj_event_t_());
}
//
// Release one waiting thread.
//
pj_status_t pulse()
{
return pj_event_pulse(this->pj_event_t_());
}
//
// Set a non-signalled.
//
pj_status_t reset()
{
return pj_event_reset(this->pj_event_t_());
}
private:
pj_event_t *event_;
};
//
// Timestamp
//
class Pj_Timestamp
{
public:
pj_status_t get_timestamp()
{
return pj_get_timestamp(&ts_);
}
Pj_Timestamp& operator += (const Pj_Timestamp &rhs)
{
pj_add_timestamp(&ts_, &rhs.ts_);
return *this;
}
Pj_Timestamp& operator -= (const Pj_Timestamp &rhs)
{
pj_sub_timestamp(&ts_, &rhs.ts_);
return *this;
}
Pj_Time_Val to_time() const
{
Pj_Timestamp zero;
pj_memset(&zero, 0, sizeof(zero));
return Pj_Time_Val(pj_elapsed_time(&zero.ts_, &ts_));
}
pj_uint32_t to_msec() const
{
Pj_Timestamp zero;
pj_memset(&zero, 0, sizeof(zero));
return pj_elapsed_msec(&zero.ts_, &ts_);
}
pj_uint32_t to_usec() const
{
Pj_Timestamp zero;
pj_memset(&zero, 0, sizeof(zero));
return pj_elapsed_usec(&zero.ts_, &ts_);
}
pj_uint32_t to_nanosec() const
{
Pj_Timestamp zero;
pj_memset(&zero, 0, sizeof(zero));
return pj_elapsed_nanosec(&zero.ts_, &ts_);
}
pj_uint32_t to_cycle() const
{
Pj_Timestamp zero;
pj_memset(&zero, 0, sizeof(zero));
return pj_elapsed_cycle(&zero.ts_, &ts_);
}
private:
pj_timestamp ts_;
};
//
// OS abstraction.
//
class Pj_OS_API
{
public:
//
// Get current time.
//
static pj_status_t gettimeofday( Pj_Time_Val *tv )
{
return pj_gettimeofday(tv);
}
//
// Parse to time of day.
//
static pj_status_t time_decode( const Pj_Time_Val *tv,
pj_parsed_time *pt )
{
return pj_time_decode(tv, pt);
}
//
// Parse from time of day.
//
static pj_status_t time_encode( const pj_parsed_time *pt,
Pj_Time_Val *tv)
{
return pj_time_encode(pt, tv);
}
//
// Convert to GMT.
//
static pj_status_t time_local_to_gmt( Pj_Time_Val *tv )
{
return pj_time_local_to_gmt( tv );
}
//
// Convert time to local.
//
static pj_status_t time_gmt_to_local( Pj_Time_Val *tv)
{
return pj_time_gmt_to_local( tv );
}
};
//
// Timeval inlines.
//
inline pj_status_t Pj_Time_Val::gettimeofday()
{
return Pj_OS_API::gettimeofday(this);
}
inline pj_parsed_time Pj_Time_Val::decode()
{
pj_parsed_time pt;
Pj_OS_API::time_decode(this, &pt);
return pt;
}
inline pj_status_t Pj_Time_Val::encode(const pj_parsed_time *pt)
{
return Pj_OS_API::time_encode(pt, this);
}
inline pj_status_t Pj_Time_Val::to_gmt()
{
return Pj_OS_API::time_local_to_gmt(this);
}
inline pj_status_t Pj_Time_Val::to_local()
{
return Pj_OS_API::time_gmt_to_local(this);
}
#endif /* __PJPP_OS_HPP__ */
|