Changeset 126 in libcf
- Timestamp:
- 06/14/13 13:41:55 (11 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/cf_thread.h
r122 r126 3 3 * \author myusgun <myusgun@gmail.com> 4 4 * 5 * \brief 멀티스레드 및 뮤텍스지원5 * \brief 멀티스레드 지원 6 6 * 7 7 * \example thread.c … … 12 12 #include "cf_base.h" 13 13 14 #include "cf_mutex.h" 15 14 16 /** 스레드 워커 함수 프로토타입 */ 15 17 typedef int (* CF_Thread_Function) (void *); … … 17 19 /** 스레드 컨텍스트 */ 18 20 typedef void * CF_Thread_Ctx; 19 20 /** 뮤텍스 컨텍스트 */21 typedef void * CF_Mutex_Ctx;22 21 23 22 #ifdef __cplusplus … … 39 38 CF_Thread_Join (CF_Thread_Ctx ctx); 40 39 41 CF_EXPORT int42 CF_Mutex_CreateCtx (CF_Mutex_Ctx * ctx);43 44 CF_EXPORT int45 CF_Mutex_DestoryCtx (CF_Mutex_Ctx ctx);46 47 CF_EXPORT int48 CF_Mutex_Lock (CF_Mutex_Ctx ctx);49 50 CF_EXPORT int51 CF_Mutex_Unlock (CF_Mutex_Ctx ctx);52 53 40 #ifdef __cplusplus 54 41 } -
trunk/src/cf_thread.c
r123 r126 3 3 * \author myusgun <myusgun@gmail.com> 4 4 * 5 * \brief 멀티 스레드 및 뮤텍스구현5 * \brief 멀티 스레드 구현 6 6 */ 7 7 #include "cf_thread.h" 8 #include "cf_local.h"9 8 #include "cf_error.h" 10 9 … … 30 29 return CF_ERROR_THREAD_INVALID_CTX 31 30 32 #define ASSERT_MUTEX_CTX(__ctx) \33 if (__ctx == NULL) \34 return CF_ERROR_MUTEX_INVALID_CTX35 36 31 typedef THREAD_RETURN (THREAD_CALL * THREAD_WORKER) (void *); 37 32 … … 43 38 } CF_THREAD_CTX; 44 39 45 typedef struct __cf_ctx_ctx__46 {47 MUTEX_TYPE mid;48 } CF_MUTEX_CTX;49 50 40 static int 51 41 CF_Thread_Local_Close (THREAD_TYPE tid) … … 56 46 57 47 CloseHandle (tid); 58 #endif59 60 return CF_OK;61 }62 63 static int64 CF_Mutex_Local_Close (MUTEX_TYPE mid)65 {66 #if defined(_WIN32) || defined(_WIN64)67 if (mid == NULL)68 return CF_ERROR_MUTEX_INVALID_ARGS;69 70 CloseHandle (mid);71 #else72 pthread_mutex_destroy (&mid);73 48 #endif 74 49 … … 213 188 return CF_OK; 214 189 } 215 216 /**217 * 뮤텍스 컨텍스트를 생성218 *219 * \return 성공 시, CF_OK; 실패 시, 오류 코드220 *221 * \param ctx 뮤텍스 컨텍스트 주소222 */223 int224 CF_Mutex_CreateCtx (CF_Mutex_Ctx * ctx)225 {226 int result = 0;227 228 CF_MUTEX_CTX * context = NULL;229 230 context = (CF_MUTEX_CTX *) calloc (sizeof (CF_MUTEX_CTX), 1);231 if (context == NULL)232 return CF_ERROR_MUTEX_CREATE_CTX;233 234 TRY235 {236 #if defined(_WIN32) || defined(_WIN64)237 context->mid = CreateMutexA (NULL, FALSE, NULL);238 if (context->mid == NULL)239 {240 result = CF_ERROR_MUTEX_CREATE;241 TRY_BREAK;242 }243 #else244 result = pthread_mutex_init (&context->mid, NULL);245 if (result)246 {247 result = CF_ERROR_MUTEX_CREATE;248 TRY_BREAK;249 }250 #endif251 252 *ctx = context;253 }254 CATCH_IF (result < 0)255 {256 CF_Mutex_DestoryCtx ((CF_Mutex_Ctx) context);257 }258 259 return result;260 }261 262 /**263 * 뮤텍스 컨텍스트 해제264 *265 * \return 성공 시, CF_OK; 실패 시, 오류 코드266 *267 * \param ctx 뮤텍스 컨텍스트268 */269 int270 CF_Mutex_DestoryCtx (CF_Mutex_Ctx ctx)271 {272 CF_MUTEX_CTX * context = ctx;273 274 ASSERT_MUTEX_CTX (ctx);275 276 CF_Mutex_Local_Close (context->mid);277 free (context);278 279 return CF_OK;280 }281 282 /**283 * 뮤텍스 잠금284 *285 * \return 성공 시, CF_OK; 실패 시, 오류 코드286 *287 * \param ctx 뮤텍스 컨텍스트288 */289 int290 CF_Mutex_Lock (CF_Mutex_Ctx ctx)291 {292 CF_MUTEX_CTX * context = ctx;293 294 ASSERT_MUTEX_CTX (ctx);295 296 #if defined(_WIN32) || defined(_WIN64)297 WaitForSingleObject (context->mid, INFINITE);298 #else299 pthread_mutex_lock (&context->mid);300 #endif301 302 return CF_OK;303 }304 305 /**306 * 뮤텍스 잠금 해제307 *308 * \return 성공 시, CF_OK; 실패 시, 오류 코드309 *310 * \param ctx 뮤텍스 컨텍스트311 */312 int313 CF_Mutex_Unlock (CF_Mutex_Ctx ctx)314 {315 CF_MUTEX_CTX * context = ctx;316 317 ASSERT_MUTEX_CTX (ctx);318 319 #if defined(_WIN32) || defined(_WIN64)320 ReleaseMutex (context->mid);321 #else322 pthread_mutex_unlock (&context->mid);323 #endif324 325 return CF_OK;326 } -
trunk/src/makefile
r119 r126 12 12 cf_socket \ 13 13 cf_thread \ 14 cf_mutex \ 14 15 cf_debug \ 15 16 cf_log \
Note:
See TracChangeset
for help on using the changeset viewer.