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