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