hostsupport/hostthreadadapter/src/platformthreading.cpp
branchbug235_bringup_0
changeset 55 09263774e342
parent 54 067180f57b12
child 56 40cc73c24bf8
equal deleted inserted replaced
54:067180f57b12 55:09263774e342
     1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Cross platform threading support library
       
    15 
       
    16 #include "hostthreadadapter.h"
       
    17 
       
    18 #ifdef WIN32
       
    19 #include <windows.h>
       
    20 #include <WinBase.h>
       
    21 #else
       
    22 #include <unistd.h>
       
    23 #include <time.h>
       
    24 #include <signal.h>
       
    25 #endif
       
    26 
       
    27 #include <stdio.h>
       
    28 
       
    29 #ifdef WIN32
       
    30 #ifdef __GNUG__
       
    31 static void CALLBACK default_interrupt_handler(ULONG_PTR)
       
    32 #else // MSVC
       
    33 static VOID CALLBACK default_interrupt_handler(__in  ULONG_PTR)
       
    34 #endif
       
    35 #else // LINUX
       
    36 static void default_interrupt_handler(int)
       
    37 #endif
       
    38 {
       
    39     // does nothing
       
    40     ;
       
    41 }
       
    42 
       
    43 namespace Psu
       
    44 {
       
    45 
       
    46 #ifndef WIN32
       
    47     const int MicrosecsInMillisec = 1000;
       
    48 #endif
       
    49 
       
    50 
       
    51     int platform_create_simple_thread(PLATFORM_THREAD_T * pThreadHandle,
       
    52                                       PLATFORM_THREADFUNC pFunc,
       
    53                                       void * param)
       
    54     {
       
    55         int
       
    56             rv = 0;
       
    57 
       
    58 #ifdef WIN32
       
    59         DWORD
       
    60             threadId,
       
    61             stackSize = 0,
       
    62             creationFlags = 0;
       
    63 
       
    64         PLATFORM_THREAD_T
       
    65             tmp =  CreateThread(NULL,
       
    66                                 stackSize,
       
    67                                 pFunc,
       
    68                                 param,
       
    69                                 creationFlags,
       
    70                                 &threadId);
       
    71         if (tmp == NULL)
       
    72             {
       
    73                 rv = GetLastError();
       
    74             }
       
    75         else
       
    76             {
       
    77                 *pThreadHandle = tmp;
       
    78             }
       
    79 #else
       
    80         rv = pthread_create(pThreadHandle,
       
    81                             0,
       
    82                             pFunc,
       
    83                             param);
       
    84 #endif
       
    85 
       
    86         return rv;
       
    87     }
       
    88 
       
    89 
       
    90     void platform_release_thread(PLATFORM_THREAD_T threadHandle)
       
    91     {
       
    92 #ifdef WIN32
       
    93         CloseHandle(threadHandle);
       
    94 #else
       
    95         pthread_detach(threadHandle);
       
    96 #endif
       
    97     }
       
    98 
       
    99 
       
   100     STATUS_T platform_join_thread(PLATFORM_THREAD_T threadHandle)
       
   101     {
       
   102 		STATUS_T retVal;
       
   103 #ifdef WIN32
       
   104 
       
   105         retVal = WaitForSingleObject(threadHandle,INFINITE);
       
   106 #else
       
   107         retVal = pthread_join(threadHandle, NULL);
       
   108 #endif
       
   109 		return retVal;
       
   110     }
       
   111 
       
   112 
       
   113     void platform_mutex_init(PLATFORM_MUTEX_T * pMutex)
       
   114     {
       
   115 #ifdef WIN32
       
   116         InitializeCriticalSection(pMutex);
       
   117 #else
       
   118         pthread_mutex_init(pMutex,
       
   119                            0);
       
   120 #endif
       
   121     }
       
   122 
       
   123 
       
   124     void platform_mutex_destroy(PLATFORM_MUTEX_T * pMutex)
       
   125     {
       
   126 #ifdef WIN32
       
   127         DeleteCriticalSection(pMutex);
       
   128 #else
       
   129         pthread_mutex_destroy(pMutex);
       
   130 #endif
       
   131     }
       
   132 
       
   133 
       
   134     void platform_mutex_lock(PLATFORM_MUTEX_T * pMutex)
       
   135     {
       
   136 #ifdef WIN32
       
   137         EnterCriticalSection(pMutex);
       
   138 #else
       
   139         pthread_mutex_lock(pMutex);
       
   140 #endif
       
   141     }
       
   142 
       
   143 
       
   144     void platform_mutex_unlock(PLATFORM_MUTEX_T * pMutex)
       
   145     {
       
   146 #ifdef WIN32
       
   147         LeaveCriticalSection(pMutex);
       
   148 #else
       
   149         pthread_mutex_unlock(pMutex);
       
   150 #endif
       
   151     }
       
   152 
       
   153 
       
   154     void platform_sleep(int millisecs)
       
   155     {
       
   156 #ifdef WIN32
       
   157         Sleep(millisecs);
       
   158 #else
       
   159         usleep(millisecs * MicrosecsInMillisec);
       
   160 #endif
       
   161     }
       
   162 
       
   163 
       
   164     STATUS_T platform_create_semaphore(PLATFORM_SEMAPHORE_T& semHandle,
       
   165     					int initialCount,int maximumCount)
       
   166     {
       
   167 
       
   168  		STATUS_T rv =0;
       
   169 #ifdef WIN32
       
   170 
       
   171         PLATFORM_SEMAPHORE_T
       
   172             tmp =  CreateSemaphore(NULL,
       
   173                                 initialCount,
       
   174                                 maximumCount,
       
   175                                 NULL);
       
   176         if (tmp == NULL)
       
   177             {
       
   178                 rv = GetLastError();
       
   179             }
       
   180         else
       
   181             {
       
   182                 semHandle = tmp;
       
   183             }
       
   184 #else
       
   185         rv = sem_init(&semHandle,
       
   186                             0,
       
   187                             initialCount);
       
   188 #endif
       
   189 
       
   190         return rv;
       
   191     }
       
   192 
       
   193 
       
   194     STATUS_T platform_wait_for_signal(PLATFORM_SEMAPHORE_T& semHandle)
       
   195     {
       
   196 		STATUS_T retVal;
       
   197 #ifdef WIN32
       
   198 
       
   199         retVal = WaitForSingleObject(semHandle,	INFINITE);
       
   200 #else
       
   201         retVal = sem_wait(&semHandle);
       
   202 #endif
       
   203 		return retVal;
       
   204     }
       
   205 
       
   206 
       
   207     void platform_signal_semaphore(PLATFORM_SEMAPHORE_T& semHandle)
       
   208     {
       
   209 #ifdef WIN32
       
   210         ReleaseSemaphore(semHandle, 1, NULL);
       
   211 #else
       
   212         sem_post(&semHandle);
       
   213 #endif
       
   214     }
       
   215 
       
   216     void platform_release_semaphore(PLATFORM_SEMAPHORE_T& semHandle)
       
   217     {
       
   218 #ifdef WIN32
       
   219 
       
   220         CloseHandle(semHandle);
       
   221 #else
       
   222 
       
   223         sem_destroy(&semHandle);
       
   224 #endif
       
   225     }
       
   226 
       
   227     int32_t platform_interruptable_sleep(int64_t length)
       
   228     {
       
   229 #ifdef WIN32
       
   230         return SleepEx(static_cast<DWORD>(length), TRUE);
       
   231 #else
       
   232         struct timespec req = {0};
       
   233         time_t sec = (int)(length/1000);
       
   234         length = length - (sec*1000);
       
   235         req.tv_sec = sec;
       
   236         req.tv_nsec = length*1000000L;
       
   237 
       
   238         int ret = nanosleep(&req, NULL);
       
   239 
       
   240         return ret;
       
   241 #endif
       
   242     }
       
   243 
       
   244     bool platform_interrupt_sleep(PLATFORM_THREAD_T handle)
       
   245     {
       
   246 #ifdef WIN32
       
   247         int32_t result = QueueUserAPC(default_interrupt_handler, handle, 0);
       
   248 
       
   249         return (result!=0);
       
   250 #else
       
   251         return (0 == pthread_kill(handle, SIGUSR1));
       
   252 #endif
       
   253     }
       
   254 
       
   255 #ifndef WIN32
       
   256     SignalUSR1Handler SignalUSR1Handler::instance;
       
   257 
       
   258     SignalUSR1Handler::SignalUSR1Handler()
       
   259     {
       
   260         //sa = {0}, old = {0};
       
   261         sa.sa_handler = &default_interrupt_handler;
       
   262         sigaction(SIGUSR1, &sa, &old);
       
   263     }
       
   264 
       
   265     SignalUSR1Handler::~SignalUSR1Handler()
       
   266     {
       
   267         sigaction(SIGUSR1, &old, NULL);
       
   268     }
       
   269 #endif
       
   270 
       
   271 }