[126] | 1 | /**
|
---|
[128] | 2 | * \file cf_mutex.c
|
---|
[126] | 3 | *
|
---|
[128] | 4 | * \author myusgun <myusgun@gmail.com>
|
---|
| 5 | *
|
---|
[126] | 6 | * \brief 뮤텍스 구현
|
---|
| 7 | */
|
---|
| 8 | #include "cf_mutex.h"
|
---|
| 9 | #include "cf_local.h"
|
---|
| 10 | #include "cf_error.h"
|
---|
| 11 |
|
---|
[164] | 12 | #include <stdio.h>
|
---|
[126] | 13 | #include <stdlib.h>
|
---|
| 14 |
|
---|
| 15 | #if defined(_WIN32) || defined(_WIN64)
|
---|
| 16 | # include <windows.h>
|
---|
| 17 | # include <process.h>
|
---|
[164] | 18 | # define MUTEX_TYPE CRITICAL_SECTION
|
---|
[126] | 19 | #else // #if defined(_WIN32) || defined(_WIN64)
|
---|
| 20 | # include <pthread.h>
|
---|
| 21 | # define MUTEX_TYPE pthread_mutex_t
|
---|
| 22 | #endif // #if defined(_WIN32) || defined(_WIN64)
|
---|
| 23 |
|
---|
[151] | 24 | #define ASSERT_CTX(__ctx) \
|
---|
| 25 | if (__ctx == NULL) \
|
---|
[126] | 26 | return CF_ERROR_MUTEX_INVALID_CTX
|
---|
| 27 |
|
---|
| 28 | typedef struct __cf_ctx_ctx__
|
---|
| 29 | {
|
---|
| 30 | MUTEX_TYPE mid;
|
---|
[149] | 31 | } CF_MUTEX_CONTEXT;
|
---|
[126] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * 뮤텍스 컨텍스트를 생성
|
---|
| 35 | *
|
---|
| 36 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 37 | *
|
---|
| 38 | * \param ctx 뮤텍스 컨텍스트 주소
|
---|
| 39 | */
|
---|
| 40 | int
|
---|
[151] | 41 | CF_Mutex_Create (cf_ctx * ctx)
|
---|
[126] | 42 | {
|
---|
| 43 | int result = 0;
|
---|
| 44 |
|
---|
[149] | 45 | CF_MUTEX_CONTEXT * context = NULL;
|
---|
[126] | 46 |
|
---|
[151] | 47 | ASSERT_CTX (ctx);
|
---|
| 48 |
|
---|
| 49 | context = NEWCTX (CF_MUTEX_CONTEXT);
|
---|
[126] | 50 | if (context == NULL)
|
---|
| 51 | return CF_ERROR_MUTEX_CREATE_CTX;
|
---|
| 52 |
|
---|
| 53 | TRY
|
---|
| 54 | {
|
---|
| 55 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[164] | 56 | InitializeCriticalSection (&context->mid);
|
---|
[126] | 57 | #else
|
---|
| 58 | result = pthread_mutex_init (&context->mid, NULL);
|
---|
| 59 | if (result)
|
---|
| 60 | {
|
---|
| 61 | result = CF_ERROR_MUTEX_CREATE;
|
---|
| 62 | TRY_BREAK;
|
---|
| 63 | }
|
---|
| 64 | #endif
|
---|
| 65 |
|
---|
| 66 | *ctx = context;
|
---|
| 67 | }
|
---|
| 68 | CATCH_IF (result < 0)
|
---|
| 69 | {
|
---|
[151] | 70 | CF_Mutex_Destory ((cf_ctx) context);
|
---|
[126] | 71 | }
|
---|
| 72 |
|
---|
| 73 | return result;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * 뮤텍스 컨텍스트 해제
|
---|
| 78 | *
|
---|
| 79 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 80 | *
|
---|
| 81 | * \param ctx 뮤텍스 컨텍스트
|
---|
| 82 | */
|
---|
| 83 | int
|
---|
[151] | 84 | CF_Mutex_Destory (cf_ctx ctx)
|
---|
[126] | 85 | {
|
---|
[151] | 86 | CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
|
---|
[126] | 87 |
|
---|
[151] | 88 | ASSERT_CTX (ctx);
|
---|
[126] | 89 |
|
---|
[142] | 90 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[164] | 91 | DeleteCriticalSection (&context->mid);
|
---|
[142] | 92 | #else
|
---|
| 93 | pthread_mutex_destroy (&context->mid);
|
---|
| 94 | #endif
|
---|
| 95 |
|
---|
[126] | 96 | free (context);
|
---|
| 97 |
|
---|
| 98 | return CF_OK;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * 뮤텍스 잠금
|
---|
| 103 | *
|
---|
| 104 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 105 | *
|
---|
| 106 | * \param ctx 뮤텍스 컨텍스트
|
---|
| 107 | */
|
---|
| 108 | int
|
---|
[151] | 109 | CF_Mutex_Lock (cf_ctx ctx)
|
---|
[126] | 110 | {
|
---|
[151] | 111 | CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
|
---|
[164] | 112 | BOOL st;
|
---|
[126] | 113 |
|
---|
[151] | 114 | ASSERT_CTX (ctx);
|
---|
[126] | 115 |
|
---|
| 116 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[164] | 117 | st = TryEnterCriticalSection (&context->mid);
|
---|
| 118 | fprintf (stderr, "%d ", st);
|
---|
[126] | 119 | #else
|
---|
| 120 | pthread_mutex_lock (&context->mid);
|
---|
| 121 | #endif
|
---|
| 122 |
|
---|
| 123 | return CF_OK;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * 뮤텍스 잠금 해제
|
---|
| 128 | *
|
---|
| 129 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 130 | *
|
---|
| 131 | * \param ctx 뮤텍스 컨텍스트
|
---|
| 132 | */
|
---|
| 133 | int
|
---|
[151] | 134 | CF_Mutex_Unlock (cf_ctx ctx)
|
---|
[126] | 135 | {
|
---|
[151] | 136 | CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
|
---|
[126] | 137 |
|
---|
[151] | 138 | ASSERT_CTX (ctx);
|
---|
[126] | 139 |
|
---|
| 140 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[164] | 141 | LeaveCriticalSection (&context->mid);
|
---|
[126] | 142 | #else
|
---|
| 143 | pthread_mutex_unlock (&context->mid);
|
---|
| 144 | #endif
|
---|
| 145 |
|
---|
| 146 | return CF_OK;
|
---|
| 147 | }
|
---|