• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*!
2  * \copy
3  *     Copyright (c)  2009-2013, Cisco Systems
4  *     All rights reserved.
5  *
6  *     Redistribution and use in source and binary forms, with or without
7  *     modification, are permitted provided that the following conditions
8  *     are met:
9  *
10  *        * Redistributions of source code must retain the above copyright
11  *          notice, this list of conditions and the following disclaimer.
12  *
13  *        * Redistributions in binary form must reproduce the above copyright
14  *          notice, this list of conditions and the following disclaimer in
15  *          the documentation and/or other materials provided with the
16  *          distribution.
17  *
18  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *     POSSIBILITY OF SUCH DAMAGE.
30  *
31  *
32  * \file    WelsThreadLib.h
33  *
34  * \brief   Interfaces introduced in thread programming
35  *
36  * \date    11/17/2009 Created
37  *
38  *************************************************************************************
39  */
40 
41 #ifndef   _WELS_THREAD_API_H_
42 #define   _WELS_THREAD_API_H_
43 
44 #include "typedefs.h"
45 
46 #ifdef  __cplusplus
47 extern "C" {
48 #endif
49 
50 #if defined(_WIN32) || defined(__CYGWIN__)
51 
52 #include <windows.h>
53 
54 typedef    HANDLE                    WELS_THREAD_HANDLE;
55 typedef    LPTHREAD_START_ROUTINE    LPWELS_THREAD_ROUTINE;
56 
57 typedef    CRITICAL_SECTION          WELS_MUTEX;
58 typedef    HANDLE                    WELS_EVENT;
59 
60 #define    WELS_THREAD_ROUTINE_TYPE         DWORD  WINAPI
61 #define    WELS_THREAD_ROUTINE_RETURN(rc)   return (DWORD)rc;
62 
63 #ifdef WINAPI_FAMILY
64 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
65 #define WP80
66 
67 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
68 #define GetSystemInfo(x) GetNativeSystemInfo(x)
69 #define CreateEvent(attr, reset, init, name) CreateEventEx(attr, name, ((reset) ? CREATE_EVENT_MANUAL_RESET : 0) | ((init) ? CREATE_EVENT_INITIAL_SET : 0), EVENT_ALL_ACCESS)
70 #define CreateSemaphore(a, b, c, d) CreateSemaphoreEx(a, b, c, d, 0, SEMAPHORE_ALL_ACCESS)
71 #define WaitForSingleObject(a, b) WaitForSingleObjectEx(a, b, FALSE)
72 #define WaitForMultipleObjects(a, b, c, d) WaitForMultipleObjectsEx(a, b, c, d, FALSE)
73 #endif
74 #endif
75 
76 #else // NON-WINDOWS
77 
78 #include <stdlib.h>
79 #include <unistd.h>
80 #include <string.h>
81 #include <pthread.h>
82 #include <semaphore.h>
83 #include <signal.h>
84 #include <errno.h>
85 #include <time.h>
86 #include <sys/time.h>
87 
88 #include <sys/stat.h>
89 #include <fcntl.h>
90 
91 typedef   pthread_t    WELS_THREAD_HANDLE;
92 typedef  void* (*LPWELS_THREAD_ROUTINE) (void*);
93 
94 typedef   pthread_mutex_t           WELS_MUTEX;
95 
96 #ifdef __APPLE__
97 typedef   pthread_cond_t            WELS_EVENT;
98 #else
99 typedef   sem_t*                    WELS_EVENT;
100 #endif
101 
102 #define   WELS_THREAD_ROUTINE_TYPE         void *
103 #define   WELS_THREAD_ROUTINE_RETURN(rc)   return (void*)(intptr_t)rc;
104 
105 #endif//_WIN32
106 
107 typedef    int32_t        WELS_THREAD_ERROR_CODE;
108 typedef    int32_t        WELS_THREAD_ATTR;
109 
110 typedef  struct _WelsLogicalProcessorInfo {
111   int32_t    ProcessorCount;
112 } WelsLogicalProcessInfo;
113 
114 #define    WELS_THREAD_ERROR_OK                                 0
115 #define    WELS_THREAD_ERROR_GENERAL                    ((uint32_t)(-1))
116 #define    WELS_THREAD_ERROR_WAIT_OBJECT_0              0
117 #define    WELS_THREAD_ERROR_WAIT_TIMEOUT               ((uint32_t)0x00000102L)
118 #define    WELS_THREAD_ERROR_WAIT_FAILED                WELS_THREAD_ERROR_GENERAL
119 
120 WELS_THREAD_ERROR_CODE    WelsMutexInit (WELS_MUTEX*    mutex);
121 WELS_THREAD_ERROR_CODE    WelsMutexLock (WELS_MUTEX*    mutex);
122 WELS_THREAD_ERROR_CODE    WelsMutexUnlock (WELS_MUTEX* mutex);
123 WELS_THREAD_ERROR_CODE    WelsMutexDestroy (WELS_MUTEX* mutex);
124 
125 WELS_THREAD_ERROR_CODE    WelsEventOpen (WELS_EVENT* p_event, const char* event_name = NULL);
126 WELS_THREAD_ERROR_CODE    WelsEventClose (WELS_EVENT* event, const char* event_name = NULL);
127 
128 WELS_THREAD_ERROR_CODE    WelsEventSignal (WELS_EVENT* event,WELS_MUTEX *pMutex, int* iCondition);
129 WELS_THREAD_ERROR_CODE    WelsEventWait (WELS_EVENT* event,WELS_MUTEX *pMutex, int& iCondition);
130 WELS_THREAD_ERROR_CODE    WelsEventWaitWithTimeOut (WELS_EVENT* event, uint32_t dwMilliseconds,WELS_MUTEX *pMutex = NULL);
131 WELS_THREAD_ERROR_CODE    WelsMultipleEventsWaitSingleBlocking (uint32_t nCount, WELS_EVENT* event_list,
132     WELS_EVENT* master_event = NULL,WELS_MUTEX *pMutex = NULL);
133 
134 WELS_THREAD_ERROR_CODE    WelsThreadCreate (WELS_THREAD_HANDLE* thread,  LPWELS_THREAD_ROUTINE  routine,
135     void* arg, WELS_THREAD_ATTR attr);
136 
137 WELS_THREAD_ERROR_CODE    WelsThreadSetName (const char* thread_name);
138 
139 WELS_THREAD_ERROR_CODE    WelsThreadJoin (WELS_THREAD_HANDLE  thread);
140 
141 WELS_THREAD_HANDLE        WelsThreadSelf();
142 
143 WELS_THREAD_ERROR_CODE    WelsQueryLogicalProcessInfo (WelsLogicalProcessInfo* pInfo);
144 
145 void WelsSleep (uint32_t dwMilliSecond);
146 
147 #ifdef  __cplusplus
148 }
149 #endif
150 
151 #endif
152