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