1 | /**
|
---|
2 | * \file cf_mutex.c
|
---|
3 | *
|
---|
4 | * \author myusgun <myusgun@gmail.com>
|
---|
5 | *
|
---|
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 |
|
---|
23 | #define ASSERT_MUTEX_CTX(__ctx) \
|
---|
24 | if (__ctx == NULL) \
|
---|
25 | return CF_ERROR_MUTEX_INVALID_CTX
|
---|
26 |
|
---|
27 | typedef struct __cf_ctx_ctx__
|
---|
28 | {
|
---|
29 | MUTEX_TYPE mid;
|
---|
30 | } CF_MUTEX_CTX;
|
---|
31 |
|
---|
32 | static int
|
---|
33 | CF_Mutex_Local_Close (MUTEX_TYPE mid)
|
---|
34 | {
|
---|
35 | #if defined(_WIN32) || defined(_WIN64)
|
---|
36 | if (mid == NULL)
|
---|
37 | return CF_ERROR_MUTEX_INVALID_ARGS;
|
---|
38 |
|
---|
39 | CloseHandle (mid);
|
---|
40 | #else
|
---|
41 | pthread_mutex_destroy (&mid);
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | return CF_OK;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * 뮤텍스 컨텍스트를 생성
|
---|
49 | *
|
---|
50 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
51 | *
|
---|
52 | * \param ctx 뮤텍스 컨텍스트 주소
|
---|
53 | */
|
---|
54 | int
|
---|
55 | CF_Mutex_CreateCtx (CF_Mutex_Ctx * ctx)
|
---|
56 | {
|
---|
57 | int result = 0;
|
---|
58 |
|
---|
59 | CF_MUTEX_CTX * context = NULL;
|
---|
60 |
|
---|
61 | context = (CF_MUTEX_CTX *) calloc (sizeof (CF_MUTEX_CTX), 1);
|
---|
62 | if (context == NULL)
|
---|
63 | return CF_ERROR_MUTEX_CREATE_CTX;
|
---|
64 |
|
---|
65 | TRY
|
---|
66 | {
|
---|
67 | #if defined(_WIN32) || defined(_WIN64)
|
---|
68 | context->mid = CreateMutexA (NULL, FALSE, NULL);
|
---|
69 | if (context->mid == NULL)
|
---|
70 | {
|
---|
71 | result = CF_ERROR_MUTEX_CREATE;
|
---|
72 | TRY_BREAK;
|
---|
73 | }
|
---|
74 | #else
|
---|
75 | result = pthread_mutex_init (&context->mid, NULL);
|
---|
76 | if (result)
|
---|
77 | {
|
---|
78 | result = CF_ERROR_MUTEX_CREATE;
|
---|
79 | TRY_BREAK;
|
---|
80 | }
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | *ctx = context;
|
---|
84 | }
|
---|
85 | CATCH_IF (result < 0)
|
---|
86 | {
|
---|
87 | CF_Mutex_DestoryCtx ((CF_Mutex_Ctx) context);
|
---|
88 | }
|
---|
89 |
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * 뮤텍스 컨텍스트 해제
|
---|
95 | *
|
---|
96 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
97 | *
|
---|
98 | * \param ctx 뮤텍스 컨텍스트
|
---|
99 | */
|
---|
100 | int
|
---|
101 | CF_Mutex_DestoryCtx (CF_Mutex_Ctx ctx)
|
---|
102 | {
|
---|
103 | CF_MUTEX_CTX * context = ctx;
|
---|
104 |
|
---|
105 | ASSERT_MUTEX_CTX (ctx);
|
---|
106 |
|
---|
107 | CF_Mutex_Local_Close (context->mid);
|
---|
108 | free (context);
|
---|
109 |
|
---|
110 | return CF_OK;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * 뮤텍스 잠금
|
---|
115 | *
|
---|
116 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
117 | *
|
---|
118 | * \param ctx 뮤텍스 컨텍스트
|
---|
119 | */
|
---|
120 | int
|
---|
121 | CF_Mutex_Lock (CF_Mutex_Ctx ctx)
|
---|
122 | {
|
---|
123 | CF_MUTEX_CTX * context = ctx;
|
---|
124 |
|
---|
125 | ASSERT_MUTEX_CTX (ctx);
|
---|
126 |
|
---|
127 | #if defined(_WIN32) || defined(_WIN64)
|
---|
128 | WaitForSingleObject (context->mid, INFINITE);
|
---|
129 | #else
|
---|
130 | pthread_mutex_lock (&context->mid);
|
---|
131 | #endif
|
---|
132 |
|
---|
133 | return CF_OK;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * 뮤텍스 잠금 해제
|
---|
138 | *
|
---|
139 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
140 | *
|
---|
141 | * \param ctx 뮤텍스 컨텍스트
|
---|
142 | */
|
---|
143 | int
|
---|
144 | CF_Mutex_Unlock (CF_Mutex_Ctx ctx)
|
---|
145 | {
|
---|
146 | CF_MUTEX_CTX * context = ctx;
|
---|
147 |
|
---|
148 | ASSERT_MUTEX_CTX (ctx);
|
---|
149 |
|
---|
150 | #if defined(_WIN32) || defined(_WIN64)
|
---|
151 | ReleaseMutex (context->mid);
|
---|
152 | #else
|
---|
153 | pthread_mutex_unlock (&context->mid);
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | return CF_OK;
|
---|
157 | }
|
---|