source: libcf/trunk/src/cf_mutex.c@ 151

Last change on this file since 151 was 151, checked in by cheese, 11 years ago

#1 fix interface and add util module

File size: 2.6 KB
Line 
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_CTX(__ctx) \
24 if (__ctx == NULL) \
25 return CF_ERROR_MUTEX_INVALID_CTX
26
27typedef struct __cf_ctx_ctx__
28{
29 MUTEX_TYPE mid;
30} CF_MUTEX_CONTEXT;
31
32/**
33 * 뮤텍스 컨텍스트를 생성
34 *
35 * \return 성공 시, CF_OK; 실패 시, 오류 코드
36 *
37 * \param ctx 뮤텍스 컨텍스트 주소
38 */
39int
40CF_Mutex_Create (cf_ctx * ctx)
41{
42 int result = 0;
43
44 CF_MUTEX_CONTEXT * context = NULL;
45
46 ASSERT_CTX (ctx);
47
48 context = NEWCTX (CF_MUTEX_CONTEXT);
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 {
74 CF_Mutex_Destory ((cf_ctx) context);
75 }
76
77 return result;
78}
79
80/**
81 * 뮤텍스 컨텍스트 해제
82 *
83 * \return 성공 시, CF_OK; 실패 시, 오류 코드
84 *
85 * \param ctx 뮤텍스 컨텍스트
86 */
87int
88CF_Mutex_Destory (cf_ctx ctx)
89{
90 CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
91
92 ASSERT_CTX (ctx);
93
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
103 free (context);
104
105 return CF_OK;
106}
107
108/**
109 * 뮤텍스 잠금
110 *
111 * \return 성공 시, CF_OK; 실패 시, 오류 코드
112 *
113 * \param ctx 뮤텍스 컨텍스트
114 */
115int
116CF_Mutex_Lock (cf_ctx ctx)
117{
118 CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
119
120 ASSERT_CTX (ctx);
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 */
138int
139CF_Mutex_Unlock (cf_ctx ctx)
140{
141 CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
142
143 ASSERT_CTX (ctx);
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}
Note: See TracBrowser for help on using the repository browser.