1 | /**
|
---|
2 | * \file cf_thread.c
|
---|
3 | *
|
---|
4 | * \author myusgun <myusgun@gmail.com>
|
---|
5 | *
|
---|
6 | * \brief 멀티 스레드 구현
|
---|
7 | */
|
---|
8 | #include "cf_thread.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 THREAD_TYPE HANDLE
|
---|
17 | # define MUTEX_TYPE HANDLE
|
---|
18 | # define THREAD_RETURN unsigned long /**< 스레드 워커 함수 반환 형 */
|
---|
19 | # define THREAD_CALL __stdcall
|
---|
20 | #else // #if defined(_WIN32) || defined(_WIN64)
|
---|
21 | # include <pthread.h>
|
---|
22 | # define THREAD_TYPE pthread_t
|
---|
23 | # define MUTEX_TYPE pthread_mutex_t
|
---|
24 | # define THREAD_RETURN void * /**< 스레드 워커 함수 반환 형 */
|
---|
25 | # define THREAD_CALL
|
---|
26 | #endif // #if defined(_WIN32) || defined(_WIN64)
|
---|
27 |
|
---|
28 | #define ASSERT_THREAD_CTX(__ctx) \
|
---|
29 | if (__ctx == NULL) \
|
---|
30 | return CF_ERROR_THREAD_INVALID_CTX
|
---|
31 |
|
---|
32 | typedef THREAD_RETURN (THREAD_CALL * THREAD_WORKER) (void *);
|
---|
33 |
|
---|
34 | typedef struct __cf_thread_ctx__
|
---|
35 | {
|
---|
36 | THREAD_TYPE tid;
|
---|
37 | THREAD_WORKER callback;
|
---|
38 | void * arg;
|
---|
39 | } CF_THREAD_CTX;
|
---|
40 |
|
---|
41 | static int
|
---|
42 | CF_Thread_Local_Close (THREAD_TYPE tid)
|
---|
43 | {
|
---|
44 | #if defined(_WIN32) || defined(_WIN64)
|
---|
45 | if (tid == NULL)
|
---|
46 | return CF_ERROR_THREAD_INVALID_ARGS;
|
---|
47 |
|
---|
48 | CloseHandle (tid);
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | return CF_OK;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * 스레드 컨텍스트를 생성
|
---|
56 | *
|
---|
57 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
58 | *
|
---|
59 | * \param ctx 스레드 컨텍스트 주소
|
---|
60 | * \param callback 스레드 워커 함수 이름
|
---|
61 | * \param arg 스레드 함수로 전달할 인자
|
---|
62 | */
|
---|
63 | int
|
---|
64 | CF_Thread_CreateCtx (CF_Thread_Ctx * ctx,
|
---|
65 | CF_Thread_Function callback,
|
---|
66 | void * arg)
|
---|
67 | {
|
---|
68 | CF_THREAD_CTX * context = NULL;
|
---|
69 |
|
---|
70 | context = (CF_THREAD_CTX *) calloc (sizeof (CF_THREAD_CTX), 1);
|
---|
71 | if (context == NULL)
|
---|
72 | return CF_ERROR_THREAD_CREATE_CTX;
|
---|
73 |
|
---|
74 | context->callback = (THREAD_WORKER) callback;
|
---|
75 | context->arg = arg;
|
---|
76 |
|
---|
77 | *ctx = (CF_Thread_Ctx) context;
|
---|
78 |
|
---|
79 | return CF_OK;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * 스레드를 실행
|
---|
84 | *
|
---|
85 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
86 | *
|
---|
87 | * \param ctx 스레드 컨텍스트
|
---|
88 | *
|
---|
89 | * \remarks
|
---|
90 | * pthread에서 지원되는 스케줄링 정책은 SCHED_OTHER, SCHED_FIFO, SCHED_RR 등이 존재 <br />
|
---|
91 | * 일반적으로 설정되는 스케줄링 정책의 기본값은 SCHED_OTHER이며, 솔라리스 환경에서 SCHED_OTHER는 TS(timesharing) 방식으로 명시되어 있음 <br />
|
---|
92 | * 그러나 개발 단계에서 테스트된 동작은 SCHED_FIFO와 동일하였으며, 때문에 솔라리스 환경에서는 스케줄링 정책을 SCHED_RR로 명시하도록 함 <br />
|
---|
93 | * <br />
|
---|
94 | * 참고 url <br />
|
---|
95 | * - http://kldp.org/node/18841 , "SCHED_OTHER, SCHED_FIFO, SCHED_RR에 대해..." <br />
|
---|
96 | * - http://blog.naver.com/popjunior/80021646476 , "AIX, CPU 모니터링과 튜닝" <br />
|
---|
97 | */
|
---|
98 | int
|
---|
99 | CF_Thread_Start (CF_Thread_Ctx ctx)
|
---|
100 | {
|
---|
101 | int result = 0;
|
---|
102 |
|
---|
103 | CF_THREAD_CTX * context = (CF_THREAD_CTX *) ctx;
|
---|
104 |
|
---|
105 | ASSERT_THREAD_CTX (ctx);
|
---|
106 |
|
---|
107 | #if defined(_WIN32) || defined(_WIN64)
|
---|
108 | context->tid = CreateThread (NULL, 0, context->callback, context->arg, 0, NULL);
|
---|
109 | if (context->tid == NULL)
|
---|
110 | return CF_ERROR_THREAD_START;
|
---|
111 | #else
|
---|
112 |
|
---|
113 | pthread_attr_t * attr = NULL;
|
---|
114 | # if defined(_SOLARIS)
|
---|
115 | pthread_attr_t solarisAttr;
|
---|
116 | struct sched_param sched;
|
---|
117 |
|
---|
118 | attr = &solarisAttr;
|
---|
119 |
|
---|
120 | result = pthread_attr_init (attr);
|
---|
121 | if (result)
|
---|
122 | return CF_ERROR_THREAD_INIT_ATTR;
|
---|
123 |
|
---|
124 | result = pthread_attr_setinheritsched (attr, PTHREAD_EXPLICIT_SCHED);
|
---|
125 | if (result)
|
---|
126 | return CF_ERROR_THREAD_SET_INHERIT_SCHED;
|
---|
127 |
|
---|
128 | result = pthread_attr_setschedpolicy (attr, SCHED_RR);
|
---|
129 | if (result)
|
---|
130 | return CF_ERROR_THREAD_SET_SCHED_POLICY;
|
---|
131 |
|
---|
132 | sched.sched_priority = 1;
|
---|
133 | result = pthread_attr_setschedparam (attr, &sched);
|
---|
134 | if (result)
|
---|
135 | return CF_ERROR_THREAD_SET_SCHED_PARAM;
|
---|
136 | # endif // # if defined(_SOLARIS)
|
---|
137 |
|
---|
138 | result = pthread_create (&context->tid, attr, context->callback, context->arg);
|
---|
139 | if (result)
|
---|
140 | return CF_ERROR_THREAD_START;
|
---|
141 | #endif
|
---|
142 | return result;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * 스레드 컨텍스트를 해제
|
---|
147 | *
|
---|
148 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
149 | *
|
---|
150 | * \param ctx 스레드 컨텍스트
|
---|
151 | *
|
---|
152 | * \remarks 스레드 컨텍스트를 해제하는 것이며 워커 스레드가 종료되지 않음
|
---|
153 | */
|
---|
154 | int
|
---|
155 | CF_Thread_DestroyCtx (CF_Thread_Ctx ctx)
|
---|
156 | {
|
---|
157 | CF_THREAD_CTX * context = (CF_THREAD_CTX *) ctx;
|
---|
158 |
|
---|
159 | ASSERT_THREAD_CTX (ctx);
|
---|
160 |
|
---|
161 | CF_Thread_Local_Close (context->tid);
|
---|
162 | free (context);
|
---|
163 |
|
---|
164 | return CF_OK;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * 스레드가 종료될 때 까지 대기
|
---|
169 | *
|
---|
170 | * \return CF_OK 반환
|
---|
171 | *
|
---|
172 | * \param ctx 스레드 컨텍스트
|
---|
173 | */
|
---|
174 | int
|
---|
175 | CF_Thread_Join (CF_Thread_Ctx ctx)
|
---|
176 | {
|
---|
177 | CF_THREAD_CTX * context = (CF_THREAD_CTX *) ctx;
|
---|
178 |
|
---|
179 | char status[16] = {0x00,};
|
---|
180 |
|
---|
181 | ASSERT_THREAD_CTX (ctx);
|
---|
182 |
|
---|
183 | #if defined(_WIN32) || defined(_WIN64)
|
---|
184 | WaitForSingleObject (context->tid, INFINITE);
|
---|
185 | #else
|
---|
186 | pthread_join (context->tid, (void **)status);
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | return CF_OK;
|
---|
190 | }
|
---|