Debian Codegraph @

Xpjproject/pjlib/src/pj/os_core_symbian.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <pj/os.h>
21 #include <pj/assert.h>
22 #include <pj/pool.h>
23 #include <pj/log.h>
24 #include <pj/rand.h>
25 #include <pj/string.h>
26 #include <pj/guid.h>
27 #include <pj/except.h>
28 #include <pj/errno.h>
29
30 #include "os_symbian.h"
31
32
33 #define PJ_MAX_TLS 32
34 #define DUMMY_MUTEX ((pj_mutex_t*)101)
35 #define DUMMY_SEMAPHORE ((pj_sem_t*)102)
36 #define THIS_FILE "os_core_symbian.c"
37
38 /* Default message slot number for RSocketServ::Connect().
39 * Increase it to 32 from the default 8 (KESockDefaultMessageSlots)
40 */
41 #ifndef PJ_SYMBIAN_SOCK_MSG_SLOTS
42 # define PJ_SYMBIAN_SOCK_MSG_SLOTS 32
43 #endif
44
45 /*
46 * Note:
47 *
48 * The Symbian implementation does not support threading!
49 */
50
51 struct pj_thread_t
52 {
53 char obj_name[PJ_MAX_OBJ_NAME];
54 void *tls_values[PJ_MAX_TLS];
55
56 #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0
57 pj_uint32_t stk_size;
58 pj_uint32_t stk_max_usage;
59 char *stk_start;
60 const char *caller_file;
61 int caller_line;
62 #endif
63
64 } main_thread;
65
66 struct pj_atomic_t
67 {
68 pj_atomic_value_t value;
69 };
70
71 struct pj_sem_t
72 {
73 int value;
74 int max;
75 };
76
77 /* Flag and reference counter for PJLIB instance */
78 static int initialized;
79
80 /* Flags to indicate which TLS variables have been used */
81 static int tls_vars[PJ_MAX_TLS];
82
83 /* atexit handlers */
84 static unsigned atexit_count;
85 static void (*atexit_func[32])(void);
86
87
88 /////////////////////////////////////////////////////////////////////////////
89 //
90 // CPjTimeoutTimer implementation
91 //
92
93 CPjTimeoutTimer::CPjTimeoutTimer()
94 : CActive(PJ_SYMBIAN_TIMER_PRIORITY), hasTimedOut_(PJ_FALSE)
95 {
96 }
97
98 CPjTimeoutTimer::~CPjTimeoutTimer()
99 {
100 Cancel();
101 timer_.Close();
102 }
103
104 void CPjTimeoutTimer::ConstructL()
105 {
106 hasTimedOut_ = PJ_FALSE;
107 timer_.CreateLocal();
108 CActiveScheduler::Add(this);
109 }
110
111 CPjTimeoutTimer *CPjTimeoutTimer::NewL()
112 {
113 CPjTimeoutTimer *self = new CPjTimeoutTimer;
114 CleanupStack::PushL(self);
115
116 self->ConstructL();
117
118 CleanupStack::Pop(self);
119 return self;
120
121 }
122
123 void CPjTimeoutTimer::StartTimer(TUint miliSeconds)
124 {
125 Cancel();
126
127 hasTimedOut_ = PJ_FALSE;
128 timer_.After(iStatus, miliSeconds * 1000);
129 SetActive();
130 }
131
132 bool CPjTimeoutTimer::HasTimedOut() const
133 {
134 return hasTimedOut_ != 0;
135 }
136
137 void CPjTimeoutTimer::RunL()
138 {
139 hasTimedOut_ = PJ_TRUE;
140 }
141
142 void CPjTimeoutTimer::DoCancel()
143 {
144 timer_.Cancel();
145 }
146
147 TInt CPjTimeoutTimer::RunError(TInt aError)
148 {
149 PJ_UNUSED_ARG(aError);
150 return KErrNone;
151 }
152
153
154
155 /////////////////////////////////////////////////////////////////////////////
156 //
157 // PjSymbianOS implementation
158 //
159
160 PjSymbianOS::PjSymbianOS()
161 : isConnectionUp_(false),
162 isSocketServInitialized_(false), isResolverInitialized_(false),
163 console_(NULL), selectTimeoutTimer_(NULL),
164 appSocketServ_(NULL), appConnection_(NULL), appHostResolver_(NULL),
165 appHostResolver6_(NULL)
166 {
167 }
168
169 // Set parameters
170 void PjSymbianOS::SetParameters(pj_symbianos_params *params)
171 {
172 appSocketServ_ = (RSocketServ*) params->rsocketserv;
173 appConnection_ = (RConnection*) params->rconnection;
174 appHostResolver_ = (RHostResolver*) params->rhostresolver;
175 appHostResolver6_ = (RHostResolver*) params->rhostresolver6;
176 }
177
178 // Get PjSymbianOS instance
179 PjSymbianOS *PjSymbianOS::Instance()
180 {
181 static PjSymbianOS instance_;
182 return &instance_;
183 }
184
185
186 // Initialize
187 TInt PjSymbianOS::Initialize()
188 {
189 TInt err;
190
191 selectTimeoutTimer_ = CPjTimeoutTimer::NewL();
192
193 #if 0
194 pj_assert(console_ == NULL);
195 TRAPD(err, console_ = Console::NewL(_L("PJLIB"),
196 TSize(KConsFullScreen,KConsFullScreen)));
197 return err;
198 #endif
199
200 /* Only create RSocketServ if application doesn't specify it
201 * in the parameters
202 */
203 if (!isSocketServInitialized_ && appSocketServ_ == NULL) {
204 err = socketServ_.Connect(PJ_SYMBIAN_SOCK_MSG_SLOTS);
205 if (err != KErrNone)
206 goto on_error;
207
208 isSocketServInitialized_ = true;
209 }
210
211 if (!isResolverInitialized_) {
212 if (appHostResolver_ == NULL) {
213 if (Connection())
214 err = hostResolver_.Open(SocketServ(), KAfInet, KSockStream,
215 *Connection());
216 else
217 err = hostResolver_.Open(SocketServ(), KAfInet, KSockStream);
218
219 if (err != KErrNone)
220 goto on_error;
221 }
222
223 #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0
224 if (appHostResolver6_ == NULL) {
225 if (Connection())
226 err = hostResolver6_.Open(SocketServ(), KAfInet6, KSockStream,
227 *Connection());
228 else
229 err = hostResolver6_.Open(SocketServ(), KAfInet6, KSockStream);
230
231 if (err != KErrNone)
232 goto on_error;
233 }
234 #endif
235
236
237 isResolverInitialized_ = true;
238 }
239
240 isConnectionUp_ = true;
241
242 return KErrNone;
243
244 on_error:
245 Shutdown();
246 return err;
247 }
248
249 // Shutdown
250 void PjSymbianOS::Shutdown()
251 {
252 isConnectionUp_ = false;
253
254 if (isResolverInitialized_) {
255 hostResolver_.Close();
256 #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0
257 hostResolver6_.Close();
258 #endif
259 isResolverInitialized_ = false;
260 }
261
262 if (isSocketServInitialized_) {
263 socketServ_.Close();
264 isSocketServInitialized_ = false;
265 }
266
267 delete console_;
268 console_ = NULL;
269
270 delete selectTimeoutTimer_;
271 selectTimeoutTimer_ = NULL;
272
273 appSocketServ_ = NULL;
274 appConnection_ = NULL;
275 appHostResolver_ = NULL;
276 appHostResolver6_ = NULL;
277 }
278
279 // Convert to Unicode
280 TInt PjSymbianOS::ConvertToUnicode(TDes16 &aUnicode, const TDesC8 &aForeign)
281 {
282 #if 0
283 pj_assert(conv_ != NULL);
284 return conv_->ConvertToUnicode(aUnicode, aForeign, convToUnicodeState_);
285 #else
286 return CnvUtfConverter::ConvertToUnicodeFromUtf8(aUnicode, aForeign);
287 #endif
288 }
289
290 // Convert from Unicode
291 TInt PjSymbianOS::ConvertFromUnicode(TDes8 &aForeign, const TDesC16 &aUnicode)
292 {
293 #if 0
294 pj_assert(conv_ != NULL);
295 return conv_->ConvertFromUnicode(aForeign, aUnicode, convToAnsiState_);
296 #else
297 return CnvUtfConverter::ConvertFromUnicodeToUtf8(aForeign, aUnicode);
298 #endif
299 }
300
301
302 /////////////////////////////////////////////////////////////////////////////
303 //
304 // PJLIB os.h implementation
305 //
306
307 PJ_DEF(pj_uint32_t) pj_getpid(void)
308 {
309 return 0;
310 }
311
312
313 /* Set Symbian specific parameters */
314 PJ_DEF(pj_status_t) pj_symbianos_set_params(pj_symbianos_params *prm)
315 {
316 PJ_ASSERT_RETURN(prm != NULL, PJ_EINVAL);
317 PjSymbianOS::Instance()->SetParameters(prm);
318 return PJ_SUCCESS;
319 }
320
321
322 /* Set connection status */
323 PJ_DEF(void) pj_symbianos_set_connection_status(pj_bool_t up)
324 {
325 PjSymbianOS::Instance()->SetConnectionStatus(up != 0);
326 }
327
328
329 /*
330 * pj_init(void).
331 * Init PJLIB!
332 */
333 PJ_DEF(pj_status_t) pj_init(void)
334 {
335 char stack_ptr;
336 pj_status_t status;
337
338 /* Check if PJLIB have been initialized */
339 if (initialized) {
340 ++initialized;
341 return PJ_SUCCESS;
342 }
343
344 pj_ansi_strxcpy(main_thread.obj_name, "pjthread",
345 sizeof(main_thread.obj_name));
346
347 // Init main thread
348 pj_memset(&main_thread, 0, sizeof(main_thread));
349
350 // Initialize PjSymbianOS instance
351 PjSymbianOS *os = PjSymbianOS::Instance();
352
353 PJ_LOG(4,(THIS_FILE, "Initializing PJLIB for Symbian OS.."));
354
355 TInt err;
356 err = os->Initialize();
357 if (err != KErrNone)
358 return PJ_RETURN_OS_ERROR(err);
359
360 /* Init logging */
361 pj_log_init();
362
363 /* Initialize exception ID for the pool.
364 * Must do so after critical section is configured.
365 */
366 status = pj_exception_id_alloc("PJLIB/No memory", &PJ_NO_MEMORY_EXCEPTION);
367 if (status != PJ_SUCCESS)
368 goto on_error;
369
370 #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0
371 main_thread.stk_start = &stack_ptr;
372 main_thread.stk_size = 0xFFFFFFFFUL;
373 main_thread.stk_max_usage = 0;
374 #else
375 stack_ptr = '\0';
376 #endif
377
378 /* Flag PJLIB as initialized */
379 ++initialized;
380 pj_assert(initialized == 1);
381
382 PJ_LOG(5,(THIS_FILE, "PJLIB initialized."));
383 return PJ_SUCCESS;
384
385 on_error:
386 pj_shutdown();
387 return PJ_RETURN_OS_ERROR(err);
388 }
389
390
391 PJ_DEF(pj_status_t) pj_atexit(pj_exit_callback func)
392 {
393 if (atexit_count >= PJ_ARRAY_SIZE(atexit_func))
394 return PJ_ETOOMANY;
395
396 atexit_func[atexit_count++] = func;
397 return PJ_SUCCESS;
398 }
399
400
401
402 PJ_DEF(void) pj_shutdown(void)
403 {
404 /* Only perform shutdown operation when 'initialized' reaches zero */
405 pj_assert(initialized > 0);
406 if (--initialized != 0)
407 return;
408
409 /* Call atexit() functions */
410 while (atexit_count > 0) {
411 (*atexit_func[atexit_count-1])();
412 --atexit_count;
413 }
414
415 /* Free exception ID */
416 if (PJ_NO_MEMORY_EXCEPTION != -1) {
417 pj_exception_id_free(PJ_NO_MEMORY_EXCEPTION);
418 PJ_NO_MEMORY_EXCEPTION = -1;
419 }
420
421 /* Clear static variables */
422 pj_errno_clear_handlers();
423
424 PjSymbianOS *os = PjSymbianOS::Instance();
425 os->Shutdown();
426 }
427
428 /////////////////////////////////////////////////////////////////////////////
429
430 class CPollTimeoutTimer : public CActive
431 {
432 public:
433 static CPollTimeoutTimer* NewL(int msec, TInt prio);
434 ~CPollTimeoutTimer();
435
436 virtual void RunL();
437 virtual void DoCancel();
438
439 private:
440 RTimer rtimer_;
441
442 explicit CPollTimeoutTimer(TInt prio);
443 void ConstructL(int msec);
444 };
445
446 CPollTimeoutTimer::CPollTimeoutTimer(TInt prio)
447 : CActive(prio)
448 {
449 }
450
451
452 CPollTimeoutTimer::~CPollTimeoutTimer()
453 {
454 rtimer_.Close();
455 }
456
457 void CPollTimeoutTimer::ConstructL(int msec)
458 {
459 rtimer_.CreateLocal();
460 CActiveScheduler::Add(this);
461 rtimer_.After(iStatus, msec*1000);
462 SetActive();
463 }
464
465 CPollTimeoutTimer* CPollTimeoutTimer::NewL(int msec, TInt prio)
466 {
467 CPollTimeoutTimer *self = new CPollTimeoutTimer(prio);
468 CleanupStack::PushL(self);
469 self->ConstructL(msec);
470 CleanupStack::Pop(self);
471
472 return self;
473 }
474
475 void CPollTimeoutTimer::RunL()
476 {
477 }
478
479 void CPollTimeoutTimer::DoCancel()
480 {
481 rtimer_.Cancel();
482 }
483
484
485 /*
486 * Wait the completion of any Symbian active objects.
487 */
488 PJ_DEF(pj_bool_t) pj_symbianos_poll(int priority, int ms_timeout)
489 {
490 CPollTimeoutTimer *timer = NULL;
491
492 if (priority==-1)
493 priority = EPriorityNull;
494
495 if (ms_timeout >= 0) {
496 timer = CPollTimeoutTimer::NewL(ms_timeout, priority);
497 }
498
499 PjSymbianOS::Instance()->WaitForActiveObjects(priority);
500
501 if (timer) {
502 bool timer_is_active = timer->IsActive();
503
504 timer->Cancel();
505
506 delete timer;
507
508 return timer_is_active ? PJ_TRUE : PJ_FALSE;
509
510 } else {
511 return PJ_TRUE;
512 }
513 }
514
515
516 /*
517 * pj_thread_is_registered()
518 */
519 PJ_DEF(pj_bool_t) pj_thread_is_registered(void)
520 {
521 return PJ_FALSE;
522 }
523
524
525 /*
526 * Get thread priority value for the thread.
527 */
528 PJ_DEF(int) pj_thread_get_prio(pj_thread_t *thread)
529 {
530 PJ_UNUSED_ARG(thread);
531 return 1;
532 }
533
534
535 /*
536 * Set the thread priority.
537 */
538 PJ_DEF(pj_status_t) pj_thread_set_prio(pj_thread_t *thread, int prio)
539 {
540 PJ_UNUSED_ARG(thread);
541 PJ_UNUSED_ARG(prio);
542 return PJ_SUCCESS;
543 }
544
545
546 /*
547 * Get the lowest priority value available on this system.
548 */
549 PJ_DEF(int) pj_thread_get_prio_min(pj_thread_t *thread)
550 {
551 PJ_UNUSED_ARG(thread);
552 return 1;
553 }
554
555
556 /*
557 * Get the highest priority value available on this system.
558 */
559 PJ_DEF(int) pj_thread_get_prio_max(pj_thread_t *thread)
560 {
561 PJ_UNUSED_ARG(thread);
562 return 1;
563 }
564
565
566 /*
567 * pj_thread_get_os_handle()
568 */
569 PJ_DEF(void*) pj_thread_get_os_handle(pj_thread_t *thread)
570 {
571 PJ_UNUSED_ARG(thread);
572 return NULL;
573 }
574
575 /*
576 * pj_thread_register(..)
577 */
578 PJ_DEF(pj_status_t) pj_thread_register ( const char *cstr_thread_name,
579 pj_thread_desc desc,
580 pj_thread_t **thread_ptr)
581 {
582 PJ_UNUSED_ARG(cstr_thread_name);
583 PJ_UNUSED_ARG(desc);
584 PJ_UNUSED_ARG(thread_ptr);
585 return PJ_EINVALIDOP;
586 }
587
588
589 /*
590 * pj_thread_create(...)
591 */
592 PJ_DEF(pj_status_t) pj_thread_create( pj_pool_t *pool,
593 const char *thread_name,
594 pj_thread_proc *proc,
595 void *arg,
596 pj_size_t stack_size,
597 unsigned flags,
598 pj_thread_t **ptr_thread)
599 {
600 PJ_UNUSED_ARG(pool);
601 PJ_UNUSED_ARG(thread_name);
602 PJ_UNUSED_ARG(proc);
603 PJ_UNUSED_ARG(arg);
604 PJ_UNUSED_ARG(stack_size);
605 PJ_UNUSED_ARG(flags);
606 PJ_UNUSED_ARG(ptr_thread);
607
608 /* Sorry mate, we don't support threading */
609 return PJ_ENOTSUP;
610 }
611
612 /*
613 * pj_thread-get_name()
614 */
615 PJ_DEF(const char*) pj_thread_get_name(pj_thread_t *p)
616 {
617 pj_assert(p == &main_thread);
618 return p->obj_name;
619 }
620
621 /*
622 * pj_thread_resume()
623 */
624 PJ_DEF(pj_status_t) pj_thread_resume(pj_thread_t *p)
625 {
626 PJ_UNUSED_ARG(p);
627 return PJ_EINVALIDOP;
628 }
629
630 /*
631 * pj_thread_this()
632 */
633 PJ_DEF(pj_thread_t*) pj_thread_this(void)
634 {
635 return &main_thread;
636 }
637
638 /*
639 * pj_thread_join()
640 */
641 PJ_DEF(pj_status_t) pj_thread_join(pj_thread_t *rec)
642 {
643 PJ_UNUSED_ARG(rec);
644 return PJ_EINVALIDOP;
645 }
646
647 /*
648 * pj_thread_destroy()
649 */
650 PJ_DEF(pj_status_t) pj_thread_destroy(pj_thread_t *rec)
651 {
652 PJ_UNUSED_ARG(rec);
653 return PJ_EINVALIDOP;
654 }
655
656 /*
657 * pj_thread_sleep()
658 */
659 PJ_DEF(pj_status_t) pj_thread_sleep(unsigned msec)
660 {
661 User::After(msec*1000);
662
663 return PJ_SUCCESS;
664 }
665
666
667 ///////////////////////////////////////////////////////////////////////////////
668 /*
669 * pj_thread_local_alloc()
670 */
671
672 PJ_DEF(pj_status_t) pj_thread_local_alloc(long *index)
673 {
674 unsigned i;
675
676 /* Find unused TLS variable */
677 for (i=0; i<PJ_ARRAY_SIZE(tls_vars); ++i) {
678 if (tls_vars[i] == 0)
679 break;
680 }
681
682 if (i == PJ_ARRAY_SIZE(tls_vars))
683 return PJ_ETOOMANY;
684
685 tls_vars[i] = 1;
686 *index = i;
687
688 return PJ_SUCCESS;
689 }
690
691 /*
692 * pj_thread_local_free()
693 */
694 PJ_DEF(void) pj_thread_local_free(long index)
695 {
696 PJ_ASSERT_ON_FAIL(index >= 0 && index < (int)PJ_ARRAY_SIZE(tls_vars) &&
697 tls_vars[index] != 0, return);
698
699 tls_vars[index] = 0;
700 }
701
702
703 /*
704 * pj_thread_local_set()
705 */
706 PJ_DEF(pj_status_t) pj_thread_local_set(long index, void *value)
707 {
708 pj_thread_t *rec = pj_thread_this();
709
710 PJ_ASSERT_RETURN(index >= 0 && index < (int)PJ_ARRAY_SIZE(tls_vars) &&
711 tls_vars[index] != 0, PJ_EINVAL);
712
713 rec->tls_values[index] = value;
714 return PJ_SUCCESS;
715 }
716
717 /*
718 * pj_thread_local_get()
719 */
720 PJ_DEF(void*) pj_thread_local_get(long index)
721 {
722 pj_thread_t *rec = pj_thread_this();
723
724 PJ_ASSERT_RETURN(index >= 0 && index < (int)PJ_ARRAY_SIZE(tls_vars) &&
725 tls_vars[index] != 0, NULL);
726
727 return rec->tls_values[index];
728 }
729
730
731 ///////////////////////////////////////////////////////////////////////////////
732 /*
733 * Create atomic variable.
734 */
735 PJ_DEF(pj_status_t) pj_atomic_create( pj_pool_t *pool,
736 pj_atomic_value_t initial,
737 pj_atomic_t **atomic )
738 {
739 *atomic = (pj_atomic_t*)pj_pool_alloc(pool, sizeof(struct pj_atomic_t));
740 (*atomic)->value = initial;
741 return PJ_SUCCESS;
742 }
743
744
745 /*
746 * Destroy atomic variable.
747 */
748 PJ_DEF(pj_status_t) pj_atomic_destroy( pj_atomic_t *atomic_var )
749 {
750 PJ_UNUSED_ARG(atomic_var);
751 return PJ_SUCCESS;
752 }
753
754
755 /*
756 * Set the value of an atomic type, and return the previous value.
757 */
758 PJ_DEF(void) pj_atomic_set( pj_atomic_t *atomic_var,
759 pj_atomic_value_t value)
760 {
761 atomic_var->value = value;
762 }
763
764
765 /*
766 * Get the value of an atomic type.
767 */
768 PJ_DEF(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var)
769 {
770 return atomic_var->value;
771 }
772
773
774 /*
775 * Increment the value of an atomic type.
776 */
777 PJ_DEF(void) pj_atomic_inc(pj_atomic_t *atomic_var)
778 {
779 ++atomic_var->value;
780 }
781
782
783 /*
784 * Increment the value of an atomic type and get the result.
785 */
786 PJ_DEF(pj_atomic_value_t) pj_atomic_inc_and_get(pj_atomic_t *atomic_var)
787 {
788 return ++atomic_var->value;
789 }
790
791
792 /*
793 * Decrement the value of an atomic type.
794 */
795 PJ_DEF(void) pj_atomic_dec(pj_atomic_t *atomic_var)
796 {
797 --atomic_var->value;
798 }
799
800
801 /*
802 * Decrement the value of an atomic type and get the result.
803 */
804 PJ_DEF(pj_atomic_value_t) pj_atomic_dec_and_get(pj_atomic_t *atomic_var)
805 {
806 return --atomic_var->value;
807 }
808
809
810 /*
811 * Add a value to an atomic type.
812 */
813 PJ_DEF(void) pj_atomic_add( pj_atomic_t *atomic_var,
814 pj_atomic_value_t value)
815 {
816 atomic_var->value += value;
817 }
818
819
820 /*
821 * Add a value to an atomic type and get the result.
822 */
823 PJ_DEF(pj_atomic_value_t) pj_atomic_add_and_get( pj_atomic_t *atomic_var,
824 pj_atomic_value_t value)
825 {
826 atomic_var->value += value;
827 return atomic_var->value;
828 }
829
830
831
832 /////////////////////////////////////////////////////////////////////////////
833
834 PJ_DEF(pj_status_t) pj_mutex_create( pj_pool_t *pool,
835 const char *name,
836 int type,
837 pj_mutex_t **mutex)
838 {
839 PJ_UNUSED_ARG(pool);
840 PJ_UNUSED_ARG(name);
841 PJ_UNUSED_ARG(type);
842
843 *mutex = DUMMY_MUTEX;
844 return PJ_SUCCESS;
845 }
846
847 /*
848 * pj_mutex_create_simple()
849 */
850 PJ_DEF(pj_status_t) pj_mutex_create_simple( pj_pool_t *pool,
851 const char *name,
852 pj_mutex_t **mutex )
853 {
854 return pj_mutex_create(pool, name, PJ_MUTEX_SIMPLE, mutex);
855 }
856
857
858 PJ_DEF(pj_status_t) pj_mutex_create_recursive( pj_pool_t *pool,
859 const char *name,
860 pj_mutex_t **mutex )
861 {
862 return pj_mutex_create(pool, name, PJ_MUTEX_RECURSE, mutex);
863 }
864
865
866 /*
867 * pj_mutex_lock()
868 */
869 PJ_DEF(pj_status_t) pj_mutex_lock(pj_mutex_t *mutex)
870 {
871 pj_assert(mutex == DUMMY_MUTEX);
872 return PJ_SUCCESS;
873 }
874
875 /*
876 * pj_mutex_trylock()
877 */
878 PJ_DEF(pj_status_t) pj_mutex_trylock(pj_mutex_t *mutex)
879 {
880 pj_assert(mutex == DUMMY_MUTEX);
881 return PJ_SUCCESS;
882 }
883
884 /*
885 * pj_mutex_unlock()
886 */
887 PJ_DEF(pj_status_t) pj_mutex_unlock(pj_mutex_t *mutex)
888 {
889 pj_assert(mutex == DUMMY_MUTEX);
890 return PJ_SUCCESS;
891 }
892
893 /*
894 * pj_mutex_destroy()
895 */
896 PJ_DEF(pj_status_t) pj_mutex_destroy(pj_mutex_t *mutex)
897 {
898 pj_assert(mutex == DUMMY_MUTEX);
899 return PJ_SUCCESS;
900 }
901
902
903 /////////////////////////////////////////////////////////////////////////////
904 /*
905 * RW Mutex
906 */
907 #include "os_rwmutex.c"
908
909
910 /////////////////////////////////////////////////////////////////////////////
911
912 /*
913 * Enter critical section.
914 */
915 PJ_DEF(void) pj_enter_critical_section(void)
916 {
917 /* Nothing to do */
918 }
919
920
921 /*
922 * Leave critical section.
923 */
924 PJ_DEF(void) pj_leave_critical_section(void)
925 {
926 /* Nothing to do */
927 }
928
929
930 /////////////////////////////////////////////////////////////////////////////
931
932 /*
933 * Create semaphore.
934 */
935 PJ_DEF(pj_status_t) pj_sem_create( pj_pool_t *pool,
936 const char *name,
937 unsigned initial,
938 unsigned max,
939 pj_sem_t **p_sem)
940 {
941 pj_sem_t *sem;
942
943 PJ_UNUSED_ARG(name);
944
945 sem = (pj_sem_t*) pj_pool_zalloc(pool, sizeof(pj_sem_t));
946 sem->value = initial;
947 sem->max = max;
948
949 *p_sem = sem;
950
951 return PJ_SUCCESS;
952 }
953
954
955 /*
956 * Wait for semaphore.
957 */
958 PJ_DEF(pj_status_t) pj_sem_wait(pj_sem_t *sem)
959 {
960 if (sem->value > 0) {
961 sem->value--;
962 return PJ_SUCCESS;
963 } else {
964 pj_assert(!"Unexpected!");
965 return PJ_EINVALIDOP;
966 }
967 }
968
969
970 /*
971 * Try wait for semaphore.
972 */
973 PJ_DEF(pj_status_t) pj_sem_trywait(pj_sem_t *sem)
974 {
975 if (sem->value > 0) {
976 sem->value--;
977 return PJ_SUCCESS;
978 } else {
979 pj_assert(!"Unexpected!");
980 return PJ_EINVALIDOP;
981 }
982 }
983
984
985 /*
986 * Release semaphore.
987 */
988 PJ_DEF(pj_status_t) pj_sem_post(pj_sem_t *sem)
989 {
990 sem->value++;
991 return PJ_SUCCESS;
992 }
993
994
995 /*
996 * Destroy semaphore.
997 */
998 PJ_DEF(pj_status_t) pj_sem_destroy(pj_sem_t *sem)
999 {
1000 PJ_UNUSED_ARG(sem);
1001 return PJ_SUCCESS;
1002 }
1003
1004
1005 #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK != 0
1006 /*
1007 * The implementation of stack checking.
1008 */
1009 PJ_DEF(void) pj_thread_check_stack(const char *file, int line)
1010 {
1011 char stk_ptr;
1012 pj_uint32_t usage;
1013 pj_thread_t *thread = pj_thread_this();
1014
1015 pj_assert(thread);
1016
1017 /* Calculate current usage. */
1018 usage = (&stk_ptr > thread->stk_start) ? &stk_ptr - thread->stk_start :
1019 thread->stk_start - &stk_ptr;
1020
1021 /* Assert if stack usage is dangerously high. */
1022 pj_assert("STACK OVERFLOW!! " && (usage <= thread->stk_size - 128));
1023
1024 /* Keep statistic. */
1025 if (usage > thread->stk_max_usage) {
1026 thread->stk_max_usage = usage;
1027 thread->caller_file = file;
1028 thread->caller_line = line;
1029 }
1030 }
1031
1032 /*
1033 * Get maximum stack usage statistic.
1034 */
1035 PJ_DEF(pj_uint32_t) pj_thread_get_stack_max_usage(pj_thread_t *thread)
1036 {
1037 return thread->stk_max_usage;
1038 }
1039
1040 /*
1041 * Dump thread stack status.
1042 */
1043 PJ_DEF(pj_status_t) pj_thread_get_stack_info(pj_thread_t *thread,
1044 const char **file,
1045 int *line)
1046 {
1047 pj_assert(thread);
1048
1049 *file = thread->caller_file;
1050 *line = thread->caller_line;
1051 return 0;
1052 }
1053
1054 #endif /* PJ_OS_HAS_CHECK_STACK */
1055
1056 /*
1057 * pj_run_app()
1058 */
1059 PJ_DEF(int) pj_run_app(pj_main_func_ptr main_func, int argc, char *argv[],
1060 unsigned flags)
1061 {
1062 return (*main_func)(argc, argv);
1063 }