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 <stdio.h>
|
---|
13 | #include <stdlib.h>
|
---|
14 |
|
---|
15 | #if defined(_WIN32) || defined(_WIN64)
|
---|
16 | # include <windows.h>
|
---|
17 | # include <process.h>
|
---|
18 | # define MUTEX_TYPE CRITICAL_SECTION
|
---|
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 |
|
---|
24 | #define ASSERT_CTX(__ctx) \
|
---|
25 | if (__ctx == NULL) \
|
---|
26 | return CF_ERROR_MUTEX_INVALID_CTX
|
---|
27 |
|
---|
28 | typedef struct __cf_ctx_ctx__
|
---|
29 | {
|
---|
30 | MUTEX_TYPE mid;
|
---|
31 | } CF_MUTEX_CONTEXT;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * 뮤텍스 컨텍스트를 생성
|
---|
35 | *
|
---|
36 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
37 | *
|
---|
38 | * \param ctx 뮤텍스 컨텍스트 주소
|
---|
39 | */
|
---|
40 | int
|
---|
41 | CF_Mutex_Create (cf_ctx * ctx)
|
---|
42 | {
|
---|
43 | int result = 0;
|
---|
44 |
|
---|
45 | CF_MUTEX_CONTEXT * context = NULL;
|
---|
46 |
|
---|
47 | ASSERT_CTX (ctx);
|
---|
48 |
|
---|
49 | context = NEWCTX (CF_MUTEX_CONTEXT);
|
---|
50 | if (context == NULL)
|
---|
51 | return CF_ERROR_MUTEX_CREATE_CTX;
|
---|
52 |
|
---|
53 | TRY
|
---|
54 | {
|
---|
55 | #if defined(_WIN32) || defined(_WIN64)
|
---|
56 | InitializeCriticalSection (&context->mid);
|
---|
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 | {
|
---|
70 | CF_Mutex_Destory ((cf_ctx) context);
|
---|
71 | }
|
---|
72 |
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * 뮤텍스 컨텍스트 해제
|
---|
78 | *
|
---|
79 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
80 | *
|
---|
81 | * \param ctx 뮤텍스 컨텍스트
|
---|
82 | */
|
---|
83 | int
|
---|
84 | CF_Mutex_Destory (cf_ctx ctx)
|
---|
85 | {
|
---|
86 | CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
|
---|
87 |
|
---|
88 | ASSERT_CTX (ctx);
|
---|
89 |
|
---|
90 | #if defined(_WIN32) || defined(_WIN64)
|
---|
91 | DeleteCriticalSection (&context->mid);
|
---|
92 | #else
|
---|
93 | pthread_mutex_destroy (&context->mid);
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | free (context);
|
---|
97 |
|
---|
98 | return CF_OK;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * 뮤텍스 잠금
|
---|
103 | *
|
---|
104 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
105 | *
|
---|
106 | * \param ctx 뮤텍스 컨텍스트
|
---|
107 | */
|
---|
108 | int
|
---|
109 | CF_Mutex_Lock (cf_ctx ctx)
|
---|
110 | {
|
---|
111 | CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
|
---|
112 | BOOL st;
|
---|
113 |
|
---|
114 | ASSERT_CTX (ctx);
|
---|
115 |
|
---|
116 | #if defined(_WIN32) || defined(_WIN64)
|
---|
117 | st = TryEnterCriticalSection (&context->mid);
|
---|
118 | fprintf (stderr, "%d ", st);
|
---|
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
|
---|
134 | CF_Mutex_Unlock (cf_ctx ctx)
|
---|
135 | {
|
---|
136 | CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
|
---|
137 |
|
---|
138 | ASSERT_CTX (ctx);
|
---|
139 |
|
---|
140 | #if defined(_WIN32) || defined(_WIN64)
|
---|
141 | LeaveCriticalSection (&context->mid);
|
---|
142 | #else
|
---|
143 | pthread_mutex_unlock (&context->mid);
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | return CF_OK;
|
---|
147 | }
|
---|